1

我得到一个空的df,虽然我知道里面应该有一些行

任何想法如何解决这个问题?

第 7 行运行后的 df 如下所示:

长日期 国家
2020-11-07 葡萄牙
2020-01-01 葡萄牙

Holy_date 看起来像这样:['2020-01-01','2020-01-06']

from numpy.ma.extras import isin
import holidays
df = df[(df['country'] == 'Portugal')]
min_year = (pd.DatetimeIndex(df.long_date).year.min())
max_year = (pd.DatetimeIndex(df.long_date).year.max())+1
holy_date = [i.strftime('%Y-%m-%d') for i in [*holidays.CountryHoliday('Portugal',years = np.arange(min_year,max_year,1)).keys()]]
df.long_date= pd.to_datetime(df.long_date).dt.date
df = pd.concat([df,df.long_date.isin(holy_date).rename('bh')],axis =1)
df[df['bh']==True]
4

1 回答 1

0

问题来自您试图识别日期时间列中的字符串。你应该做的是消除行

df.long_date= pd.to_datetime(df.long_date).dt.date

并改用它:我在您的数据中添加了一些日期

long_date   country
0  2020-11-07  Portugal
1  2020-11-01  Portugal
2  2020-10-01  Portugal
3  2020-06-11  Portugal

from numpy.ma.extras import isin
import holidays
import pandas as pd
df = pd.read_csv('holyday.csv', sep=";")
print(df)
df = df[(df['country'] == 'Portugal')]
min_year = (pd.DatetimeIndex(df.long_date).year.min())
max_year = (pd.DatetimeIndex(df.long_date).year.max())+1

holy_date = [i.strftime('%Y-%m-%d') for i in [*holidays.CountryHoliday('Portugal',years = np.arange(min_year,max_year,1)).keys()]]
holy_date = list(holy_date)
#df.long_date= pd.to_datetime(df.long_date).dt.date
df = pd.concat([df,df['long_date'].isin(holy_date).rename('bh')],axis =1)
print(df)
df[df['bh']==True]

产生这个:

    long_date   country
0  2020-11-07  Portugal
1  2020-11-01  Portugal
2  2020-10-01  Portugal
3  2020-06-11  Portugal
    long_date   country     bh
0  2020-11-07  Portugal  False
1  2020-11-01  Portugal   True
2  2020-10-01  Portugal  False
3  2020-06-11  Portugal   True



    long_date   country    bh
1  2020-11-01  Portugal  True
3  2020-06-11  Portugal  True
于 2022-01-04T08:52:01.507 回答