Python Pandas - 返回应用于给定 DateOffset 对象的频率


若要返回应用于给定 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("\nUpdated Timestamp...\n",timestamp + offset)

返回应用于给定 DateOffset 对象的频率 −

print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

示例

代码如下 −

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

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-26 03:25: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 frequency applied on the given DateOffset object
print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

输出

这将产生以下代码 −

Timestamp...
 2021-09-26 03:25:02.000045

DateOffset...
 <5 * Days>

Updated Timestamp...
 2021-10-01 03:25:02.000045

The frequency on the DateOffset object..
 5D

更新于: 2021-10-21

84 浏览次数

开启你的 职业生涯

通过完成本课程获得认证

开始学习
广告
© . All rights reserved.