0

How do I get all rows in a dataframe (DateTimeIndex) whose index belongs in the index of another dataframe (DateTimeIndex with no time specified) ?

For example,

I have a dataframe (df1) which contains data for multiple days but only in the hours of 2pm-6pm. I have another dataframe (df2) which contains data for multiple days but no hour is specified. How do I access all rows in df1 that are in df2?

Below are snapshots of part of df1 and df2,

enter image description here

enter image description here

pranavhgupta
  • 143
  • 1
  • 9
  • Please see [How to ask](https://stackoverflow.com/help/how-to-ask) and [How to create a MCVE](https://stackoverflow.com/help/mcve). For `pandas`, see [How to ask a good pandas question](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Evan Dec 07 '18 at 04:41
  • 1
    Possible duplicate of [Pandas DataFrame get rows where index matches a certain condition](https://stackoverflow.com/questions/45644857/pandas-dataframe-get-rows-where-index-matches-a-certain-condition) – Evan Dec 07 '18 at 04:42

1 Answers1

0

IIUC, you can isin on index:

df1.index = pd.to_datetime(df1.index)
df2.index = pd.to_datetime(df2.index)

df1 = df1.loc[df1.index.isin(df2.index),:]

Or:

df1 = df1.loc[df1.index.floor('D').isin(df2.index),:]
Space Impact
  • 13,085
  • 23
  • 48