Python Pandas – 返回给定 DateOffset 对象上应用的递增计数

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

# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

# 显示时间戳
print("Timestamp...\n",timestamp)

# 创建 DateOffset
# 这里使用“M”频率递增月份
offset = to_offset("5M")

# 显示 DateOffset
print("\nDateOffset...\n",offset)

# 显示更新后的时间戳
print("\nUpdated Timestamp...\n",timestamp + offset)

# 返回给定 DateOffset 对象上的递增计数
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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程