Sunil Raperia
Sunil Raperia

Reputation: 125

Datetime comparison in Python

I have a datetime column and need to find those records where the time is between 9:30 to 17:00 of any day. I am not concerned about the date, but need to filter data on the basis of time.

2018-12-28 10:53:24.950
2018-12-28 10:53:55.010
2019-01-02 16:48:31.593
2019-01-02 16:48:31.593
2019-01-02 16:48:31.593

I am using the following command to extract the hours.

df1['hour_of_timestamp'] = df1['User_date'].dt.hour

Upvotes: 1

Views: 73

Answers (3)

David Buck
David Buck

Reputation: 3830

Pandas does have a built in method, between_time() to select rows within a certain time period but it only works in the datetime object is the index (or, recently, column).

As you already have a datetime column, you could extract the rows like this (adapting the example from here):

import pandas as pd

data = [["20090102 04:51:00", 89.9900, 89.9900, 89.9900, 89.9900, 100], ["20190102 05:36:00", 90.0100, 90.0100, 90.0100, 90.0100, 200], ["20090102 05:44:00", 90.1400, 90.1400, 90.1400, 90.1400, 100], ["20090102 05:50:00", 90.0500, 90.0500, 90.0500, 90.0500, 500], ["20090102 05:56:00", 90.1000, 90.1000, 90.1000, 90.1000, 300], ["20090102 05:57:00", 90.1000, 90.1000, 90.1000, 90.1000, 200]]

# Building sample dataframe with Datetime column
df = pd.DataFrame(data)
df.columns = ["Datetime", "1", "2", "3", "4", "5"]
df['Datetime'] = pd.to_datetime(df['Datetime'], format="%Y%m%d %H:%M:%S")

# Extract rows with datetime matching index range
print(df.set_index("Datetime").between_time('5:30:00', '5:45:00'))

This outputs just the records between the time range.

                         1      2      3      4    5
Datetime                                            
2019-01-02 05:36:00  90.01  90.01  90.01  90.01  200
2009-01-02 05:44:00  90.14  90.14  90.14  90.14  100

Upvotes: 1

Bob Smith
Bob Smith

Reputation: 262

If your dates are always in the format YYYY-MM-DD then why not just slice your string like so to get the times:

time_str = date_str[10:].strip()

Then you can just sort your list of times with sorted(time_list) and look for times between 9:30 and 17:00.

Upvotes: 0

Chris
Chris

Reputation: 2860

You can parse your dates with d = datetime.datetime.fromisoformat(date_str). And then you can inspect d.hour and d.minute to make your decision.

Upvotes: 0

Related Questions