从数据框中提取假期

时间:2020-08-13 23:05:19

标签: python pandas

我有一个数据框,其中日期作为索引,值作为第一列。 我想从该数据框中取出所有比利时假期,然后创建一个新的数据框。

我尝试过的事情:

be_holidays = holidays.BE()

#example of the data frame (same format)
index = pd.date_range(start='08/08/2018',end='08/09/2018',freq='1H')
df = pd.DataFrame([1,2,3,5,3,5,4,6,2,4,6,6,3,2,5,9,7,8,8,5,1,2,5,3,6],columns=['A'], index = index)


new_df = df.applymap(lambda x: str(df.index[x]).split()[0] in be_holidays)

new_df = df[~(str(df.index).split()[0]).isin(be_holidays)]

#for context 
type(df.index[0])
#results is 
pandas._libs.tslib.Timestamp


1 个答案:

答案 0 :(得分:0)

我认为这会起作用:

import pandas as pd

# index must be a datetime
df.index = pd.to_datetime(df.index)

# boolean mask, to identify holidays
mask = df.index.isin( set(be_holidays) )

# drop holidays, keep rest (~ is `not`)
df = df.loc[~mask]
相关问题