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

pythonserver side programmingprogramming更新于 2026/1/28 4:28:17

要检查给定的 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')

创建 DateOffset。我们在这里使用 Anchored offset,即每周频率,表示星期二 −

offset = to_offset("W-TUE")

显示更新的时间戳 −

print("\n更新时间戳...\n",timestamp + offset)

检查 DateOffset 是否已锚定 −

print("\n检查 DateOffset 是否已锚定...\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)

# 创建 DateOffset
# 我们在这里使用 Anchored offset,即周二的每周频率
offset = to_offset("W-TUE")

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

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

# 检查 DateOffset 是否已锚定
print("\n检查 DateOffset 是否已锚定...\n", offset.is_anchored())

输出

将产生以下代码 −

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

DateOffset...
<Week: weekday=1>

更新时间戳...
2021-09-28 03:25:02.000045

检查 DateOffset 是否已锚定...
True

相关文章


有用资源