我有一个pandas数据帧对象df
,对应以下日期:
>> df.index
DatetimeIndex(['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04',
'2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08',
'2015-01-09', '2015-01-10'],
dtype='datetime64[ns]', name='Date', length=10, freq=None)
我希望在df
中添加一列,用于显示当前是否为公共假日:
import pandas as pd
import holidays
df['hols'] = holidays.CountryHoliday('AUS',prov='NSW').get(df.index.to_datetime())
我尝试运行代码并出现错误: AttributeError: 'DatetimeIndex' object has no attribute 'to_datetime'
.
如果我尝试使用
df['hols'] = holidays.CountryHoliday('AUS',prov='NSW').get(pd.to_datetime(df.index))
我遇到了错误TypeError: 无法将类型 '<class 'pandas.core.indexes.datetimes.DatetimeIndex'>' 转换为日期。
我知道有一个名叫workalendar
的软件包,可以从在Python中向数据框添加节假日列获得信息,但是我无法在我的大学电脑上安装该软件包。
1
(假期)和0
(非假期)? - Medulla Oblongatadf ['hols'] = df ['hols']。astype('bool')。astype('int')
- ipj