Python Pandas——返回对所给 DateOffset 对象应用的增量计数


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

返回对给定 DateOffset 对象的增量计数 −

print("\nThe count of increments on the DateOffset object..\n", offset.n)

实例

以下是代码 −

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 months here using the "M" frequency
offset = to_offset("5M")

# Display the DateOffset
print("\nDateOffset...\n",offset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)

# return the count of increments on the given DateOffset object
print("\nThe count of increments on the DateOffset object..\n", offset.n)

输出

将生成以下代码 −

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

DateOffset...
 <5 * MonthEnds>

Updated Timestamp...
 2022-01-31 03:25:02.000045

The count of increments on the DateOffset object..
 5

更新日期:2021-10-21

63 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始吧
广告