Home > other >  pandas loc by dates on intraday data
pandas loc by dates on intraday data

Time:10-21

I have intraday dataframe (index is DatetimeIndex). And days i wanna loc in intraday df (it's also DatetimeIndex, thus it's representation like a date, but it's YYYY-MM-DD hh:mm:ss and hh:mm:ss = 00:00:00).

For example:

intraday_df =

                       Column
Date
2005-01-03 09:30:00    0.9962
2005-01-03 09:31:00    0.9963
2005-01-03 09:32:00    0.9939
2005-01-03 09:33:00    0.9937
2005-01-03 09:34:00    0.9905
...                       ...
2021-09-16 15:56:00  148.7800
2021-09-16 15:57:00  148.7250
2021-09-16 15:58:00  148.7400
2021-09-16 15:59:00  148.8500
2021-09-16 16:00:00  148.7900

days = DatetimeIndex(['2005-03-09', '2005-03-24', '2005-03-31', '2005-04-14',
               '2005-04-18', '2005-04-22', '2005-05-24', '2005-05-25',
               '2005-06-06', '2005-06-10',
               ...
               '2020-09-10', '2020-09-15', '2020-09-18', '2020-09-28',
               '2020-10-12', '2020-10-14', '2020-12-22', '2021-01-11',
               '2021-01-28', '2021-05-07'],
              dtype='datetime64[ns]', length=200, freq=None)

I figured out something like this

intraday_df[pd.Series(intraday_df.index.date, index=intraday_df.index).isin(days.index.date)]

But it's not so good as i can see. I would like to make something like this:

intraday_df[intraday_df.index.to_series().agg(date).isin(days.index.date)]

But i don't know how to agg (or apply) date method...

Or is there is something better?

CodePudding user response:

You could simplify the solution you have

intraday_df[intraday_df.index.floor().isin(days)]

CodePudding user response:

Try this one too:

intraday_df[pd.Series(intraday_df.index.date).isin(days)]

CodePudding user response:

Use DatetimeIndex.floor or DatetimeIndex.normalize for remove times and ouput is datetimes, so possible match by days:

intraday_df[intraday_df.index.floor('d').isin(days)]

intraday_df[intraday_df.index.normalize().isin(days)]

Or convert both to dates by DatetimeIndex.date:

intraday_df[intraday_df.index.date.isin(days.date)]
  • Related