# Python Numpy DateTime

NumPy is a kind of library that is used for the Python Programming language. Python NumPy DateTime adds support for the large, multi-dimensional arrays and matrices. Along with this, also a large collection of very high-level mathematical functions to operate on these arrays. The ancestor of NumPy has been created by Jim Hugunin with contributions from several other developers.

## DateTime64

Let us start with DateTime64. Starting in NumPy 1.7, there are detailed array data types. These data types natively support DateTime functionality. This particular data type is called “datetime64”. It is named “datetime64” because “DateTime” has already been taken by the DateTime library in Python.

## Creating DateTime

The very basic way to create DateTime is from the strings in ISO 8601 date or DateTime format. To create the DateTimes from an integer by offset relative, it is also possible to the Unix epoch (00:00:00 UTC on 1 January 1970). The unit for internal storage is automatically selected from the form of a string, and it can be either a date unit or a time unit. The date units are years (‘Y’), months (‘M’), weeks (‘W’), and days (‘D’), while the time units are hours (‘h’), minutes (‘m’), seconds (‘s’), milliseconds (‘ms’), and some additional SI-prefix seconds-based units. The datetime64 data type also accepts the string “NAT”, in any combination of lowercase/uppercase letters, for a “Not A Time” value

## DateTime and TimeDelta Arithmetic

NumPy generally allows the subtraction of the two DateTime values. It is an operation that produces a number with a particular time unit. As NumPy does not have any physical quantities system in the core. Therefore, the timedelta64 data type is to enhance datetime64. The arguments for timedelta64 are a number, that represents the number of units, and a date/time unit. For example- (D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, or (s)econds.  The timedelta64 data type also accepts the string “NAT” in place of the number for a “Not A Time” value.

## Example

Here’s a code below to convert an array of DateTime into strings.

``````import datetime

today = datetime.datetime.now()
date_time = today.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)
from datetime import datetime

now = datetime.now() # current date and time

year = now.strftime("%Y")
print("year:", year)

month = now.strftime("%m")
print("month:", month)

day = now.strftime("%d")
print("day:", day)

time = now.strftime("%H:%M:%S")
print("time:", time)

date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)``````

### Output

``````date and time: 11/03/2021, 07:45:00
year: 2021
month: 11
day: 03
time: 07:45:00
date and time: 11/03/2021, 07:45:00``````

## Difference between timestamp and NumPy DateTime

When we talk about the Time Stamps, the date and time functionality is provided by Python in the DateTime module that contains three different types.

I.e.

• Date- day, month, year
• Time- hours, minutes, seconds, microseconds
• DateTime- components of both the date and the time.

On the other hand, the DateTime in python has both the date and time together. Pandas’ alternative is the Timestamp object that encapsulates the date and time together. It is a counterpart for DateTime of python but it is based on the more efficient NumPy.DateTime64 data type. Pandas Timestamp references at a specific instant in the time that has some nanosecond precision

Though, it is the most basic type of time series of data that associates the values with the specific instants in time. The Timestamp constructor is very flexible, it can handle several inputs like strings, floats, ints, and much more.

## End Note

Python is an amazing language for doing data analysis, primarily because of the great ecosystem of data-centric python packages