-1

Say I have a df where time is in time format.

Time lat lon
1/10/2022 11:30 22.12 113.15
1/10/2022 11:32 22.13 113.14
1/10/2022 11:45 22.15 113.19
1/10/2022 12:01 22.11 113.13
1/10/2022 12:11 22.17 113.19

Here is the code

    df['Time']= pd.to_datetime(df['Time'])
    df['Time'] = df['Time'].dt.time

Now, I would like to filter those rows which contain the following time:

timeList = {'Time': ['1/10/2022 11:30', '1/10/2022 11:45', '1/10/2022 12:01']}
    dates = pd.DataFrame(data=timeList)
    dates['Time'] = pd.to_datetime(dates.stack(), format='%d/%m/%Y %H:%M').unstack()
    dates['Time'] = dates['Time'].dt.time

I tried to use isin but it only works with list. Is there a simple way I can do this?

The expected result

Time lat lon
1/10/2022 11:30 22.12 113.15
1/10/2022 11:45 22.15 113.19
1/10/2022 12:01 22.11 113.13
user16971617
  • 457
  • 3
  • 14

1 Answers1

1

If you want to use isin it is a better idea to use str:

df[df['Time'].astype(str).isin(dates['Time'].astype(str))]
Nuri Taş
  • 3,828
  • 2
  • 4
  • 22