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 datetime64 datatype 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 library
import pandas as pd
# creating a Timestamp object
a = pd.Timestamp(2022, 4, 19, 8, 30)
# applying the .asm8 attribute
print(a.asm8)

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Timestamp object, a.
  • Line 9: We obtain and print the nanoseconds numpy.datetime64 format of the input object using the .asm attribute.

Free Resources