Python Pandas - Return the number of nanoseconds in the given DateOffset object

PythonServer Side ProgrammingProgramming

To return the number of nanoseconds in the given DateOffset object, use the offset.nanos property in Pandas.

At first, import the required libraries −

from pandas.tseries.frequencies import to_offset
import pandas as pd

Set the timestamp object in Pandas −

timestamp = pd.Timestamp('2021-08-30 03:08:02.000045')

Create the DateOffset. We are incrementing the days here using the "D" frequency −

offset = to_offset("5D")

Display the Updated Timestamp −

print("\nUpdated Timestamp...\n",timestamp + offset)

Return the nanoseconds in the given DateOffset object −

print("\nThe number of nanoseconds in the DateOffset object..\n", offset.nanos)

Example

Following is the code −

from pandas.tseries.frequencies import to_offset
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-08-30 03:08:02.000045')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the DateOffset
# We are incrementing the days here using the "D" frequency
offset = to_offset("5D")

# Display the DateOffset
print("\nDateOffset...\n",offset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)

# return the nanoseconds in the given DateOffset object
print("\nThe number of nanoseconds in the DateOffset object..\n", offset.nanos)

Output

This will produce the following code −

Timestamp...
 2021-08-30 03:08:02.000045

DateOffset...
 <5 * Days>

Updated Timestamp...
 2021-09-04 03:08:02.000045

The number of nanoseconds in the DateOffset object..
 432000000000000
raja
Updated on 21-Oct-2021 07:47:28

Advertisements