Python Pandas – 返回给定 DateOffset 对象中的纳秒数
若要返回给定 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("\nDateOffset 对象中的纳秒数...\n", offset.nanos)
输出
这将产生以下代码 −
时间戳...
2021-08-30 03:08:02.000045
DateOffset...
<5 * Days>
更新后的时间戳...
2021-09-04 03:08:02.000045
DateOffset 对象中的纳秒数...
432000000000000
极客教程