How to return a NumPy datetime64 format in nanoseconds in pandas
Overview
To return a numpy.datetime64 format in nanoseconds in python, we make use of the .asm8 attribute in Pandas' Timestamp, which is an equivalent of Python's Datetime.
Note: This link tells more about the
datetime64datatype in NumPy.
Syntax
Timestamp.asm8
Syntax for the Timestamp.asm8 attribute in Pandas
Parameters
This attribute takes no parameter value.
Return value
This attribute returns the numpy.datetime64 format in nanoseconds of the input Timestamp object.
Example
# A code to illustrate the .asm8 attribute in Pandas# importing the pandas libraryimport pandas as pd# creating a Timestamp objecta = pd.Timestamp(2022, 4, 19, 8, 30)# applying the .asm8 attributeprint(a.asm8)
Explanation
- Line 3: We import the
pandaslibrary. - Line 6: We create a
Timestampobject,a. - Line 9: We obtain and print the nanoseconds
numpy.datetime64format of the input object using the.asmattribute.