我有一个包含日期 - 值对的数据集,我正在使用日期进行索引,如下所示:
import pandas as pd
dataf = pd.read_csv('dataset.csv')
dataf.index = pd.date_range(start='2010-01-01 00:00', periods=df.shape[0], freq='H')
我想对这个数据集进行Holiday colmn,其中conatin的真/假值取决于假期与否。
编辑:
from pandas.tseries.holiday import Holiday, AbstractHolidayCalendar
import pandas as pd
dataf = pd.read_csv('dataset.csv')
dataf.index = pd.date_range(start='2010-01-01 00:00', periods=df.shape[0], freq='H')
dataf['DATE'] = dataf.index.map(lambda x: x.date)
dater = pd.date_range(start='2010-01-01', end='2011-12-31')
newdater= pd.DataFrame()
newdater['Date']=dater
class ExampleCalendar(AbstractHolidayCalendar):
rules = [
Holiday('asd1',month=1, day=1),
Holiday('asd2',month=1, day=2),
Holiday('asd3',month=12, day=30),
Holiday('asd4',month=12, day=31)
]
# So I want 4 days as Holiday in a year
cal = ExampleCalendar
holidays = cal.holidays(start='2010-01-01',end='2011-12-31')
dataf['HOLIDAY'] = dataf['DATE'].isin(holidays)
print(dataf)
输出:
DATE LOAD HOLIDAY
2010-01-01 00:00:00 2010-01-01 964.353 False
2010-01-01 01:00:00 2010-01-01 954.353 False
2010-01-01 02:00:00 2010-01-01 932.856 False
2010-01-01 03:00:00 2010-01-01 926.750 False
2010-01-01 04:00:00 2010-01-01 907.179 False
2010-01-01 05:00:00 2010-01-01 904.692 False
2010-01-01 06:00:00 2010-01-01 909.452 False
2010-01-01 07:00:00 2010-01-01 922.646 False
2010-01-01 08:00:00 2010-01-01 927.956 False
2010-01-01 09:00:00 2010-01-01 963.113 False
2010-01-01 10:00:00 2010-01-01 987.948 False
2010-01-01 11:00:00 2010-01-01 1000.232 False
2010-01-01 12:00:00 2010-01-01 1000.317 False
2010-01-01 13:00:00 2010-01-01 1006.021 False
2010-01-01 14:00:00 2010-01-01 1041.776 False
2010-01-01 15:00:00 2010-01-01 1134.506 False
2010-01-01 16:00:00 2010-01-01 1151.931 False
2010-01-01 17:00:00 2010-01-01 1147.689 False
2010-01-01 18:00:00 2010-01-01 1147.292 False
2010-01-01 19:00:00 2010-01-01 1129.979 False
2010-01-01 20:00:00 2010-01-01 1099.994 False
2010-01-01 21:00:00 2010-01-01 1059.531 False
2010-01-01 22:00:00 2010-01-01 1016.890 False
2010-01-01 23:00:00 2010-01-01 965.563 False