64

I have a pandas DataFrame from 6:36 AM to 5:31 PM. I want to remove all observations where the time is less than 8:00:00 AM. Here is my attempt:

df = df[df.index < '2013-10-16 08:00:00']

This does nothing, please help.

4
  • possible duplicate: stackoverflow.com/questions/16341367/… Commented Nov 27, 2013 at 3:05
  • that filters by column value, not by timeindex though. Thanks for the link Commented Nov 27, 2013 at 3:08
  • 1
    This isn't a duplicate the other post doesn't filter by datetimeindex Commented Nov 27, 2013 at 3:11
  • More generalized solution here. Commented Jan 5, 2017 at 19:31

2 Answers 2

90

You want df.loc[df.index < '2013-10-16 08:00:00'] since you're selecting by label (index) and not by value.

selecting by label

Sign up to request clarification or add additional context in comments.

3 Comments

Oh I had no idea you could use .loc with boolean tests on the index! Great.
what if it has a multiindex?
don't have a usecase handy, but I'd suspect it's: df.loc[(df.index['a'] < something & df.index['b'] < another_thing)]
1

You can use query for a more concise option:

df.query("index < '2013-10-16 08:00:00'")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.