Python Pandas - 检查给定的 DateOffset 是否已锚定

要检查给定的 DateOffset 是否已锚定,请使用Pandas 中的方法。首先,导入所需的库 -offset.is_anchored()

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

在 Pandas 中设置时间戳对象 -

timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

创建日期偏移量。我们在周二使用锚定偏移,即每周频率 -

offset = to_offset("W-TUE")

显示更新的时间戳 -

print("\nUpdated Timestamp...\n",timestamp + offset)

检查 DateOffset 是否已锚定 -

print("\nCheck whether the DateOffset is anchored...\n", offset.is_anchored())

示例

以下是代码 -

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)

# 创建日期偏移
# 我们在周二使用锚定偏移,即每周频率
offset = to_offset("W-TUE")

# 显示日期偏移
print("\nDateOffset...\n",offset)

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

# 检查 DateOffset 是否被锚定
print("\nCheck whether the DateOffset is anchored...\n", offset.is_anchored())
输出结果

这将产生以下代码 -

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

DateOffset...
 <Week: weekday=1>

Updated Timestamp...
 2021-09-28 03:25:02.000045

Check whether the DateOffset is anchored...
 True