Python 工作日日期范围的频率
我正在尝试将一组常见的日期相关列添加到我的数据框中,而我构建这些日期列的方法不使用将具有我的数据框的日期范围的Python 工作日日期范围的频率,python,pandas,Python,Pandas,我正在尝试将一组常见的日期相关列添加到我的数据框中,而我构建这些日期列的方法不使用将具有我的数据框的日期范围的.date\u range()pandas方法 虽然我可以对常规日期列使用.index.day或.index.weekday\u name等方法,但我想根据我构建的日期范围设置一个工作日列,但不确定我是否可以使用freq属性昵称'B'或是否需要创建一个新的日期范围 此外,我希望不要根据我拥有的假期日期列表来计算这些工作日 以下是我的设置: 假日餐桌 holiday_table = hol
.date\u range()
pandas方法
虽然我可以对常规日期列使用.index.day
或.index.weekday\u name
等方法,但我想根据我构建的日期范围设置一个工作日列,但不确定我是否可以使用freq
属性昵称'B'
或是否需要创建一个新的日期范围
此外,我希望不要根据我拥有的假期日期列表来计算这些工作日
以下是我的设置:
假日餐桌
holiday_table = holiday_table.set_index('date')
holiday_table_dates = holiday_table.index.to_list() # ['2019-12-31', etc..]
基准日期表
data_date_range = pd.date_range(start=date_range_start, end=date_range_end)
df = pd.DataFrame({'date': data_date_range}).set_index('date')
df['day_index'] = df.index.day
# Weekday Name
df['weekday_name'] = df.index.weekday_name
# Business day
df['business_day'] = data_date_range.freq("B")
df['business\u day']=data\u date\u range.freq(“B”)
处出错:
好的,我想我现在明白你的问题了。您希望创建一个新的工作日列(不包括自定义假日)。在我的示例中,我刚刚使用了pandas的常规美国假日,但您已经在
假日表中列出了您的假日日期
,但您仍然可以按照我示例的总体布局进行具体使用。我还假设您对business\u day
列的布尔值满意:
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar as h_cal
# sample data
data_date_range = pd.date_range(start='1/1/2019', end='12/31/2019')
df = pd.DataFrame({'date': data_date_range}).set_index('date')
df['day_index'] = df.index.day
# Weekday Name
df['weekday_name'] = df.index.weekday_name
# this is just a sample using US holidays
hday = h_cal().holidays(df.index.min(), df.index.max())
# b is the same date range as bove just with the freq set to business days
b = pd.date_range(start='1/1/2019', end='12/31/2019', freq='B')
# find all the working business day where b is not a holiday
bday = b[~b.isin(hday)]
# create a boolean col where the date index is in your custom business day we just created
df['bday'] = df.index.isin(bday)
day_index weekday_name bday
date
2019-01-01 1 Tuesday False
2019-01-02 2 Wednesday True
2019-01-03 3 Thursday True
2019-01-04 4 Friday True
2019-01-05 5 Saturday False
data\u date\u range.freq(“B”)
的输出是什么?@Yuca我在问题末尾提到的错误<代码>未处理类型:str数据\日期\范围.index.dtype返回什么?它是一个对象吗?发生的事情是,.freq
不是DatetimeIndex的方法,而是构造函数的参数。检查文档。我怀疑您可能想更改频率,请查看asfreq
@ChrisAttributeError:'DatetimeIndex'对象没有属性'index'
很抱歉我的问题让您感到困惑,您完全正确!这非常有帮助,谢谢你介意链接到[~b.isin()]
?我不熟悉这个method@cphill它被称为。使用~
的意思正好相反,因此在本例中,它返回一个布尔值,其中b不在hday中
,您还应该阅读
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar as h_cal
# sample data
data_date_range = pd.date_range(start='1/1/2019', end='12/31/2019')
df = pd.DataFrame({'date': data_date_range}).set_index('date')
df['day_index'] = df.index.day
# Weekday Name
df['weekday_name'] = df.index.weekday_name
# this is just a sample using US holidays
hday = h_cal().holidays(df.index.min(), df.index.max())
# b is the same date range as bove just with the freq set to business days
b = pd.date_range(start='1/1/2019', end='12/31/2019', freq='B')
# find all the working business day where b is not a holiday
bday = b[~b.isin(hday)]
# create a boolean col where the date index is in your custom business day we just created
df['bday'] = df.index.isin(bday)
day_index weekday_name bday
date
2019-01-01 1 Tuesday False
2019-01-02 2 Wednesday True
2019-01-03 3 Thursday True
2019-01-04 4 Friday True
2019-01-05 5 Saturday False