简体   繁体   中英

pandas dataframe loc multiple condition another variation

I have one dataframe df and a date

last_date_pm = datetime.date(2022,12,30)

RiskDate,valStart,valEnd columns are also date of same format as of last_date_pm

I want to call

df_s = df.loc[((df['RiskDate'] == df['valStart']) & (df['valEnd'] != last_date_pm)), ['Start'] + selected_cols]

result for this comes as empty as seemingly this 'and' logic is not working as expected. I tried to print df['RiskDate'] == df['valStart'] it comes as

0 False
1 True
2 False
dtype: bool

but this df['valEnd'] != last_date_pm comes as

0 True
1 True
2 True
Name: valEnd, dtype: bool

Could the extra Name in the second condition be an issue?

It seems compare dates with datetimes , so get always False .

One idea should by compare by Timestamp or datetime:

last_date_pm = pd.Timestamp('2022-12-30')

Or compare by dates:

last_date_pm = datetime.date(2022,12,30)
(df['valEnd'].dt.date != last_date_pm)

EDIT: Convert columns to datetimes and compare by string 2022-12-31 :

cols = ['RiskDate','valStart','valEnd']
df[cols] = df[cols].apply(pd.to_datetime)

last_date_pm = '2022-12-31'
df_s = df.loc[((df['RiskDate'] == df['valStart']) & 
               (df['valEnd'] != last_date_pm)), ['Start'] + selected_cols]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM