Python Pandas - 返回在给定 DateOffset 对象上应用的增量计数
pythonserver side programmingprogramming更新于 2026/1/28 3:56:17
要返回在给定 DateOffset 对象上应用的增量计数,请使用 Pandas 中的 offset.n 属性。首先,导入所需的库 −
from pandas.tseries.frequencies import to_offset import pandas as pd
在 Pandas 中设置时间戳对象 −
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')
创建 DateOffset。我们在这里使用"M"来增加月份频率 −
offset = to_offset("5M")
显示更新的时间戳 −
print("\n更新时间戳...\n",timestamp + offset)
返回给定 DateOffset 对象上的增量计数 −
print("\nDateOffset 对象上的增量计数..\n", offset.n)
示例
以下是代码 −
from pandas.tseries.frequencies import to_offset
import pandas as pd
# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')
# 显示时间戳
print("时间戳...\n",timestamp)
# 创建 DateOffset
# 我们在这里使用"M" 频率增加月份
offset = to_offset("5M")
# 显示 DateOffset
print("\nDateOffset...\n",offset)
# 显示更新的时间戳
print("\n更新时间戳...\n",timestamp + offset)
# 返回给定 DateOffset 对象上的增量计数
print("\nDateOffset 对象的增量计数..\n", offset.n)
输出
这将产生以下代码 −
时间戳... 2021-09-26 03:25:02.000045 DateOffset... <5 * MonthEnds> 更新时间戳... 2022-01-31 03:25:02.000045 DateOffset 对象的增量计数.. 5
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

