1

I have a pandas dataframe which has datetime as its index (shown below)

              ['2018-02-13 11:55:00', '2018-02-13 12:00:00',
               '2018-02-13 12:05:00', '2018-02-13 12:10:00',
               '2018-02-13 12:15:00', '2018-02-13 12:20:00',
               '2018-02-13 12:25:00', '2018-02-13 12:30:00',
               '2018-02-13 12:35:00', '2018-02-13 12:40:00',
               ...
               '2018-02-19 04:40:00', '2018-02-19 04:45:00',
               '2018-02-19 04:50:00', '2018-02-19 05:05:00',
               '2018-02-19 05:10:00', '2018-02-19 05:15:00',
               '2018-02-19 05:20:00', '2018-02-19 05:25:00',
               '2018-02-19 05:30:00', '2018-02-19 05:40:00'])

I would like to filter all the value which has time 12:00:00 for example, therefore I am looking for a return value of

['2018-02-13 12:00:00','2018-02-14 12:00:00','2018-02-15 12:00:00','2018-02-16 12:00:00','2018-02-17 12:00:00', '2018-02-18 12:00:00']

How can I perform such indexing please?

2 Answers 2

6

Using the hour attribute is not enough if you want to find entries corresponding to noon at 12:00PM. Instead, compare your index/date-range with a datetime object:

dt = pd.date_range('2018-02-13', '2018-02-19', freq='1h')
dt[dt.time == datetime.time(12)]

DatetimeIndex(['2018-02-13 12:00:00', '2018-02-14 12:00:00',
               '2018-02-15 12:00:00', '2018-02-16 12:00:00',
               '2018-02-17 12:00:00', '2018-02-18 12:00:00'],
              dtype='datetime64[ns]', freq=None)

If you want to introduce a minutes or seconds component, simply change datetime.time(12) to datetime.time(12, 5) for 12:05:00PM, or datetime.time(12, 5, 30) for 12:05:30PM, and so on.

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

5 Comments

Thanks. However, it does not work on my data. Using data.time == datetime(12,5) it reports no time attribute in dataframe data, using data.index == datetime(12,5) it returns an empty dataframe, using data.date_Time == datetime(12,5) it reports no time attribute as well. My index's name however is Date_Time
@Victor Do df.index.time == datetime.time(12, 5)
Thanks again, so which one performs faster? or a more preferable in python please? (data[(data.index.minute==5) & (data.index.hour == 10)] or data[data.index.time == datetime.time(12,5)])
@Victor The former requires two masks. I think the latter would be better, but the best is to test it and check. If you wanted to introduce a seconds component, you will need to introduce a 3rd mask (df.index.seconds == 30) and & it with the first two masks. For the second method, just add "30" to the datetime.time call.
I agree with you, if second is introduced the code will look more complicated. I recall that I have seen some tutorial of creating a timer for time measurement, let me do this research now. Many thanks for your quick help!
2

You can filter using strftime

df[df.index.strftime('%H:%M:%S') == '12: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.