0

I have one dataframe like the following:

                       A     
2014-06-02 09:00:00   ...
2014-06-02 10:00:00   ...
2014-06-02 11:00:00   ...
2014-06-02 12:00:00   ...

2014-06-03 09:00:00   ...
2014-06-03 10:00:00   ...

2014-06-04 11:00:00   ...
2014-06-04 12:00:00   ...

2014-06-05 11:00:00   ...
2014-06-05 12:00:00   ...

And another like the following

                       A
2014-06-03 13:14:00   ...
2014-06-04 16:33:00   ...

I need one dataframe like the following :

                       A
2014-06-02 09:00:00   ...
2014-06-02 10:00:00   ...
2014-06-02 11:00:00   ...
2014-06-02 12:00:00   ...
2014-06-05 11:00:00   ...
2014-06-05 12:00:00   ...

That is: Drop from the first dataframe every row having year-month-day in the second dataframe

Alfonso_MA
  • 537
  • 5
  • 26

1 Answers1

2

You could use floor, and ~(invert symbol), and check to see if index of dfb isin dfa:

dfa[~dfa.index.floor('D').isin(dfb.index.floor('D'))]

Note both indexes need to be datetime dtypes.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 1
    I came up with solution almost the same as yours :) +1 – Andy L. Nov 18 '19 at 21:08
  • For clarification, `dfa[~dfa.index.isin(dfb.index)]` would be enough to remove every row having the exact same data as the second dataframe, `floor('D')` takes the datetime object and "rounds it down" to a day value, so that different datetime entries having the same "year-month-day" will match. If your indexes are not datetime types, they can simply be converted with `dfa.set_index=pd.to_datetime(dfa.index)`. ` – mlg556 Nov 18 '19 at 21:19
  • What the invert symbol means? – Alfonso_MA Nov 18 '19 at 21:32
  • 1
    ~ means NOT or invert True to False or False to True. https://stackoverflow.com/a/8305291/6361531 – Scott Boston Nov 18 '19 at 21:35