I have a file with the below table:
Name AvailableDate totalRemaining
0 X3321 2018-03-14 13:00:00 200
1 X3321 2018-03-14 14:00:00 200
2 X3321 2018-03-14 15:00:00 200
3 X3321 2018-03-14 16:00:00 200
4 X3321 2018-03-14 17:00:00 193
I wanted to return a DataFrame with all the records in a specific time period regardless of the actual date.
I followed the example here:
filter pandas dataframe by time
but when I execute the below:
## setup
import pandas as pd
import numpy as np
### Step 2
### Check available slots
file2 = r'C:\Users\user\Desktop\Files\data.xlsx'
slots = pd.read_excel(file2,na_values='')
## filter the preferred ones
slots['nextAvailableDate'] = pd.to_datetime((slots['nextAvailableDate']))
slots['times'] = pd.to_datetime((slots['nextAvailableDate']))
slots = slots[slots['times'].between('21:00:00', '02:00:00')]
This returns empty DataFrame as well as this solution:
slots = slots[slots['times'].dt.strftime('%H:%M:%S').between('21:00:00', '02:00:00')]
Is there a way to do it correctly without creating a columns for time separately? How I should approach this problem please?
My goal:
Name AvailableDate totalRemaining
0 X3321 2018-03-14 21:00:00 200
1 X3321 2018-03-14 22:00:00 200
2 X3321 2018-03-14 23:00:00 200
3 X3321 2018-03-14 00:00:00 200
4 X3321 2018-03-14 01:00:00 193
for every day that appears in the data set.
I think need between_time
working with Datetimeindex
created by set_index
, for columns add reset_index
with reindex
for same order of columns:
print (slots)
Name AvailableDate totalRemaining
0 X3321 2018-03-14 21:00:00 200
1 X3321 2018-03-14 20:00:00 200
2 X3321 2018-03-14 22:00:00 200
3 X3321 2018-03-14 23:00:00 200
4 X3321 2018-03-14 00:00:00 200
5 X3321 2018-03-14 01:00:00 193
6 X3321 2018-03-14 13:00:00 200
7 X3321 2018-03-14 14:00:00 200
8 X3321 2018-03-14 15:00:00 200
9 X3321 2018-03-14 16:00:00 200
10 X3321 2018-03-14 17:00:00 193
slots['AvailableDate'] = pd.to_datetime(slots['AvailableDate'])
df = (slots.set_index('AvailableDate')
.between_time('21:00:00', '02:00:00')
.reset_index()
.reindex(columns=df.columns))
print (df)
AvailableDate Name totalRemaining
0 2018-03-14 21:00:00 X3321 200
1 2018-03-14 22:00:00 X3321 200
2 2018-03-14 23:00:00 X3321 200
3 2018-03-14 00:00:00 X3321 200
4 2018-03-14 01:00:00 X3321 193
You can use pd.Series.between
with datetime
objects, as below.
from datetime import datetime
start = datetime.strptime('21:00:00', '%H:%M:%S').time()
end = datetime.strptime('02:00:00', '%H:%M:%S').time()
slots = slots[slots['times'].dt.time.between(start, end)]
Example usage
from datetime import datetime
import pandas as pd
slots = pd.DataFrame({'times': ['2018-03-08 05:00:00', '2018-03-08 07:00:00',
'2018-03-08 01:00:00', '2018-03-08 20:00:00',
'2018-03-08 22:00:00', '2018-03-08 23:00:00']})
slots['times'] = pd.to_datetime(slots['times'])
start = datetime.strptime('21:00:00', '%H:%M:%S').time()
end = datetime.strptime('23:30:00', '%H:%M:%S').time()
slots = slots[slots['times'].dt.time.between(start, end)]
# times
# 4 2018-03-08 22:00:00
# 5 2018-03-08 23:00:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With