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

