0

I have a DatetimeIndex like this.

DatetimeIndex(['2015-04-29 00:00:00', '2016-02-22 16:00:00',
               '2016-02-12 04:00:00', '2016-02-16 04:00:00',
               '2016-02-09 12:00:00', '2016-02-11 07:00:00',
               '2015-08-06 00:00:00', '2014-10-31 00:00:00',
               '2014-11-14 00:00:00', '2015-09-24 00:00:00',
               ...
               '2015-12-30 00:00:00', '2015-11-30 00:00:00',
               '2016-02-19 20:00:00', '2014-11-13 00:00:00',
               '2014-12-15 00:00:00', '2015-06-16 00:00:00',
               '2015-12-31 00:00:00', '2015-01-09 00:00:00',
               '2015-11-25 00:00:00', '2015-03-12 00:00:00'],
              dtype='datetime64[ns]', length=604, freq=None)

and a (longer) Dataframe like this:

Df: 
                          Val       
Time                                                          
2014-10-10 00:00:00       ...  
2014-10-13 00:00:00       ...
2014-10-14 00:00:00       ...  
2014-10-15 00:00:00       ...
2014-10-16 00:00:00       ...  
                      ...       ...  ...            ...            ...
2016-02-23 16:00:00       ...
2016-02-23 17:00:00       ...   
2016-02-23 18:00:00       ... 
2016-02-23 19:00:00       ...   
2016-02-23 20:00:00       ...

How can I get the Dataframe with only the rows of it that are located on these indices ?

Benoid
  • 209
  • 1
  • 4
  • 11

1 Answers1

1

If possible some values not matched use Index.isin inboolean indexing :

df1 = df[df.index.isin(idx)]

Or Index.intersection with select by DataFrame.loc:

df1 = df.loc[df.index.intersection(idx)]

If all values matched is possible simplify answer with selecting only by loc, but it failed in new versions of pandas if at least one value not match:

df2 = df.loc[idx]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252