1

Given a dataframe:

df = pd.DataFrame({'a': [10, 20, 40, 50,60,70,80,100]},index = pd.DatetimeIndex(['2018-02-27 09:01:00','2018-02-27 09:02:00','2018-02-27 09:04:00','2018-02-27 09:05:00','2018-03-27 09:01:00','2018-03-27 09:02:00','2018-03-27 09:03:00','2018-03-27 09:05:00']))

I hope to get the values as of 09:04, in which case it should return 2 rows with 'a' value to be 40 and 80. It seems pandas only takes datetime as input for asof() and I have to do some pre-processing to create all the filters myself. I wonder if there is already such functionality in pandas where input like 09:04 would suffice, or other simple ways to get the result.

EDIT: Sorry I put in the wrong df in the beginning - there should not be 09:04 for the 2nd day, thus need asof().

1
  • 2
    Do you need asof because possible 40 and 90 for 09:04:30 ? Commented Apr 29, 2019 at 13:24

3 Answers 3

1

I am using strftime

df[df.index.strftime('%H:%M')=='09:04']
Out[652]: 
                      a
2018-02-27 09:04:00  40
2018-03-27 09:04:00  90
Sign up to request clarification or add additional context in comments.

Comments

1

If need to disregard the seconds for a minute, can use .between_time

df.between_time('09:04', '09:05', include_end=False)
#                      a
#2018-02-27 09:04:00  40
#2018-03-27 09:04:00  90

Or if you want to make this a bit more functional:

time = '09:04'
df.between_time(time, (pd.to_datetime(time)+pd.Timedelta('1min')).time(), include_end=False)

Comments

0

You can try using boolean index:

df[(df.index.hour == 9) & (df.index.minute == 4)]

Output:

                      a
2018-02-27 09:04:00  40
2018-03-27 09:04:00  90

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.