- NumPy Convert Datetime64 to Datetime.Datetime or Timestamp
- the Problem
- Convert datetime.datetime to np.datetime64 and pd.Timestamp
- Numpy datetime64 to datetime and Vice-Versa implementation
- Step by Step to Convert Numpy datetime64 to DateTime
- Step 1: Import all the necessary libraries.
- Step 2: Create a Sample date in the format datetime64.
- Conversion of an array of datetime64 type
- Step by Step to Convert datetime to Numpy datetime
- Step 1: Import all the necessary libraries.
- Step 2: Create a sample date in datetime format.
- Step 3: Convert datetime to NumPy datetime format.
- Other Questions
- Question: How to convert datetime64 [ns utc] to datetime
- Join our list
- Datetimes and Timedeltas#
- Datetime64 Conventions and Assumptions#
- Basic Datetimes#
- Datetime and Timedelta Arithmetic#
- Datetime Units#
NumPy Convert Datetime64 to Datetime.Datetime or Timestamp
- the Problem
- Convert datetime.datetime to np.datetime64 and pd.Timestamp
- Convert np.datetime64 to datetime.datetime and pd.Timestamp
- Convert Timestamp to datetime and datetime64
This article aims to demonstrate how to convert data between numPy.datetim64 , datetime.datetime and Timestamp .
the Problem
When dealing with data — be it ordered or unordered, coming across date and time is a fairly common occurrence. Depending on the situation, the time and date must be dealt with in a suitable manner to ensure that the processed data is in a desirable format.
import datetime import numpy as np import pandas as pd dt = datetime.datetime(2022, 5, 21) ts = pd.DatetimeIndex([dt])[0] dt64 = np.datetime64(dt)
In the code above, we have imported three modules named datetime , numpy , and pandas , each providing their implementation for storing and processing dates (each has its own distinct use cases).
Using datetime.datetime() , we create a datetime instance and store it in a variable named dt . As for pandas , we used the method pd.DatetimeIndex() , which creates an immutable array storing n number of datetime64 instances and accessing the first member of the array returns a Timestamp instance.
Coming to the pandas module, we use the datetime64() method to convert the datetime instance and store it in a variable named dt64 .
It is easy to convert datetime to Timestamp and datetime64 instances. In this article, we will now see how we can convert np.datetime64 to datetime and Timestamp instances.
Convert datetime.datetime to np.datetime64 and pd.Timestamp
import datetime import numpy as np import pandas as pd dt = datetime.datetime(year=2018, month=10, day=24, hour=4, minute=3, second=10, microsecond=7199) # convert datetime.datetime to np.datetim364 print(np.datetime64(dt)) # datetime.datetime to pd.Timestamp print(pd.Timestamp(dt))
Numpy datetime64 to datetime and Vice-Versa implementation
Suppose you have time-series data and want to manipulate it. Then it’s obvious that you have to use NumPy DateTime for conversion and manipulation. In this coding article, I will show you how to convert NumPy datatime64 to DateTime and DateTime to datetime64.
Before starting the same article I thought to introduce this amazing video course on numpy for such basics and doubts. See article reading will definitely solve your problem for the short term but taking this simple video course make your basics strong in numpy.
All the coding has been done on Pycharm. So make sure you have already installed Pycharm on your system. Just follow the steps to get the same output according to this tutorial.
Step by Step to Convert Numpy datetime64 to DateTime
Step 1: Import all the necessary libraries.
Here we are using two libraries one is NumPy and the other is datetime. Let’s import it using the import statement.
Step 2: Create a Sample date in the format datetime64.
First of all, I am creating a single datetime64 and converting it to datetime. Then an array of datetime64 type. After that, You can create datetime64 format using the numpy.datetime64() format.
To convert it to datetime format then you have to use astype() method and just pass the datetime as an argument.
Conversion of an array of datetime64 type
Let’s create an array of days using numpy.arange() method of the format datetime64. Just copy the code and execute the same.
To convert each of the dates in the date range you have to use the same astype() method and passing the datetime as an argument.
Step by Step to Convert datetime to Numpy datetime
In this section, you will know how to convert datetime to numpy datetime. Just follow all the steps given below.
Step 1: Import all the necessary libraries.
Step 2: Create a sample date in datetime format.
It will assign today’s date and time to the variable. If you print out the type of today then it will show in the format of datetime.
Step 3: Convert datetime to NumPy datetime format.
You can change the datetime to numpy datetime using the numpy.datetime64() method. Just pass the datetime object just like below.
To know the type of the numpy_date use the type() method.
Below is the full code for this section.
Output
That’s all, these are steps to convert datetime64 to datetime and vice-versa. Just follow it to understand it clearly. Even if you have any other queries then you can contact us for more information.
Other Questions
Question: How to convert datetime64 [ns utc] to datetime
To convert UTC datetime to datetime then you have to remove the timezone from the input DateTime. To do so you have to use the tz_localize() function. Use the following line in your code to convert to datetime.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.
Datetimes and Timedeltas#
Starting in NumPy 1.7, there are core array data types which natively support datetime functionality. The data type is called datetime64 , so named because datetime is already taken by the Python standard library.
Datetime64 Conventions and Assumptions#
Similar to the Python date class, dates are expressed in the current Gregorian Calendar, indefinitely extended both in the future and in the past. [ 1 ] Contrary to Python date , which supports only years in the 1 AD — 9999 AD range, datetime64 allows also for dates BC; years BC follow the Astronomical year numbering convention, i.e. year 2 BC is numbered −1, year 1 BC is numbered 0, year 1 AD is numbered 1.
Time instants, say 16:23:32.234, are represented counting hours, minutes, seconds and fractions from midnight: i.e. 00:00:00.000 is midnight, 12:00:00.000 is noon, etc. Each calendar day has exactly 86400 seconds. This is a “naive” time, with no explicit notion of timezones or specific time scales (UT1, UTC, TAI, etc.). [ 2 ]
The calendar obtained by extending the Gregorian calendar before its official adoption on Oct. 15, 1582 is called Proleptic Gregorian Calendar
The assumption of 86400 seconds per calendar day is not valid for UTC, the present day civil time scale. In fact due to the presence of leap seconds on rare occasions a day may be 86401 or 86399 seconds long. On the contrary the 86400s day assumption holds for the TAI timescale. An explicit support for TAI and TAI to UTC conversion, accounting for leap seconds, is proposed but not yet implemented. See also the shortcomings section below.
Basic Datetimes#
The most basic way to create datetimes is from strings in ISO 8601 date or datetime format. It is also possible to create datetimes from an integer by offset relative to the Unix epoch (00:00:00 UTC on 1 January 1970). The unit for internal storage is automatically selected from the form of the string, and 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.
>>> np.datetime64('2005-02-25') numpy.datetime64('2005-02-25')
From an integer and a date unit, 1 year since the UNIX epoch:
>>> np.datetime64(1, 'Y') numpy.datetime64('1971')
Using months for the unit:
>>> np.datetime64('2005-02') numpy.datetime64('2005-02')
Specifying just the month, but forcing a ‘days’ unit:
>>> np.datetime64('2005-02', 'D') numpy.datetime64('2005-02-01')
>>> np.datetime64('2005-02-25T03:30') numpy.datetime64('2005-02-25T03:30')
>>> np.datetime64('nat') numpy.datetime64('NaT')
When creating an array of datetimes from a string, it is still possible to automatically select the unit from the inputs, by using the datetime type with generic units.
>>> np.array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64') array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64[D]')
>>> np.array(['2001-01-01T12:00', '2002-02-03T13:56:03.172'], dtype='datetime64') array(['2001-01-01T12:00:00.000', '2002-02-03T13:56:03.172'], dtype='datetime64[ms]')
An array of datetimes can be constructed from integers representing POSIX timestamps with the given unit.
>>> np.array([0, 1577836800], dtype='datetime64[s]') array(['1970-01-01T00:00:00', '2020-01-01T00:00:00'], dtype='datetime64[s]')
>>> np.array([0, 1577836800000]).astype('datetime64[ms]') array(['1970-01-01T00:00:00.000', '2020-01-01T00:00:00.000'], dtype='datetime64[ms]')
The datetime type works with many common NumPy functions, for example arange can be used to generate ranges of dates.
All the dates for one month:
>>> np.arange('2005-02', '2005-03', dtype='datetime64[D]') array(['2005-02-01', '2005-02-02', '2005-02-03', '2005-02-04', '2005-02-05', '2005-02-06', '2005-02-07', '2005-02-08', '2005-02-09', '2005-02-10', '2005-02-11', '2005-02-12', '2005-02-13', '2005-02-14', '2005-02-15', '2005-02-16', '2005-02-17', '2005-02-18', '2005-02-19', '2005-02-20', '2005-02-21', '2005-02-22', '2005-02-23', '2005-02-24', '2005-02-25', '2005-02-26', '2005-02-27', '2005-02-28'], dtype='datetime64[D]')
The datetime object represents a single moment in time. If two datetimes have different units, they may still be representing the same moment of time, and converting from a bigger unit like months to a smaller unit like days is considered a ‘safe’ cast because the moment of time is still being represented exactly.
>>> np.datetime64('2005') == np.datetime64('2005-01-01') True
>>> np.datetime64('2010-03-14T15') == np.datetime64('2010-03-14T15:00:00.00') True
Deprecated since version 1.11.0: NumPy does not store timezone information. For backwards compatibility, datetime64 still parses timezone offsets, which it handles by converting to UTC±00:00 (Zulu time). This behaviour is deprecated and will raise an error in the future.
Datetime and Timedelta Arithmetic#
NumPy allows the subtraction of two datetime values, an operation which produces a number with a time unit. Because NumPy doesn’t have a physical quantities system in its core, the timedelta64 data type was created to complement datetime64 . The arguments for timedelta64 are a number, to represent the number of units, and a date/time unit, such as (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.
>>> np.timedelta64(1, 'D') numpy.timedelta64(1,'D')
>>> np.timedelta64(4, 'h') numpy.timedelta64(4,'h')
>>> np.timedelta64('nAt') numpy.timedelta64('NaT')
Datetimes and Timedeltas work together to provide ways for simple datetime calculations.
>>> np.datetime64('2009-01-01') - np.datetime64('2008-01-01') numpy.timedelta64(366,'D')
>>> np.datetime64('2009') + np.timedelta64(20, 'D') numpy.datetime64('2009-01-21')
>>> np.datetime64('2011-06-15T00:00') + np.timedelta64(12, 'h') numpy.datetime64('2011-06-15T12:00')
>>> np.timedelta64(1,'W') / np.timedelta64(1,'D') 7.0
>>> np.timedelta64(1,'W') % np.timedelta64(10,'D') numpy.timedelta64(7,'D')
>>> np.datetime64('nat') - np.datetime64('2009-01-01') numpy.timedelta64('NaT','D')
>>> np.datetime64('2009-01-01') + np.timedelta64('nat') numpy.datetime64('NaT')
There are two Timedelta units (‘Y’, years and ‘M’, months) which are treated specially, because how much time they represent changes depending on when they are used. While a timedelta day unit is equivalent to 24 hours, there is no way to convert a month unit into days, because different months have different numbers of days.
>>> np.timedelta64(a, 'M') numpy.timedelta64(12,'M')
>>> np.timedelta64(a, 'D') Traceback (most recent call last): File "", line 1, in TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'
Datetime Units#
The Datetime and Timedelta data types support a large number of time units, as well as generic units which can be coerced into any of the other units based on input data.
Datetimes are always stored with an epoch of 1970-01-01T00:00. This means the supported dates are always a symmetric interval around the epoch, called “time span” in the table below.
The length of the span is the range of a 64-bit integer times the length of the date or unit. For example, the time span for ‘W’ (week) is exactly 7 times longer than the time span for ‘D’ (day), and the time span for ‘D’ (day) is exactly 24 times longer than the time span for ‘h’ (hour).