Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
data science
communitycreator

How to return a NumPy datetime64 format in nanoseconds in pandas

Onyejiaku Theophilus Chidalu

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)
Implementing the .asm8 attribute

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.

RELATED TAGS

pandas
data science
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring