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')

创建 DateOffset。在这里,我们使用锚定偏移,即星期频率,具体为星期二 −

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

# 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 using the Anchored offset i.e. weekly frequency here for Tuesday
offset = to_offset("W-TUE")

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

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

# Check whether the DateOffset is anchored
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

更新于:21-Oct-2021

81 查看

开启您的 职业生涯

完成课程以获取认证

开始
广告
© . All rights reserved.