Python Pandas - 返回给定 DateOffset 对象中的纳秒数

pythonserver side programmingprogramming更新于 2026/1/28 1:16:17

要返回给定 DateOffset 对象中的纳秒数,请使用 Pandas 中的 offset.nanos 属性。

首先,导入所需的库 −

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

在 Pandas 中设置时间戳对象 −

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

创建 DateOffset。我们在这里使用"D"来增加天数频率 −

offset = to_offset("5D")

显示更新的时间戳 −

print("\n更新时间戳...\n",timestamp + offset)

返回给定 DateOffset 对象中的纳秒数 −

print("\nDateOffset 对象中的纳秒数...\n", offset.nanos)

示例

以下是代码 −


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

# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-08-30 03:08:02.000045')

# 显示时间戳
print("时间戳...\n",timestamp)

# 创建 DateOffset
# 我们在这里使用"D" 频率增加天数
offset = to_offset("5D")

# 显示 DateOffset
print("\nDateOffset...\n",offset)

# 显示更新的时间戳
print("\n更新时间戳...\n",timestamp + offset)

# 返回给定 DateOffset 对象中的纳秒数
print("\nThe number of nanoseconds in the DateOffset object..\n", offset.nanos)

输出

这将产生以下代码 −

时间戳...
 2021-08-30 03:08:02.000045

DateOffset...
 <5 * Days>

更新时间戳...
 2021-09-04 03:08:02.000045

The number of nanoseconds in the DateOffset object..
 432000000000000

相关文章


有用资源