I have a Pandas dataframe with datetime64 column.
date
2022-01-30 10:57:00
2022-01-30 12:02:00
2022-01-30 18:58:00
2022-01-30 19:27:00
2022-01-30 12:00:00
2022-01-31 02:10:00
2022-01-31 01:55:00
2022-01-31 01:24:00
2022-01-31 02:37:00
...
What I need is to set working shifts (I don't know if shift is correct for turno in spanish...) with this conditions
- Night shift: 22:00:00 - 05:00:00
- Morning shift: 05:00:00 - 13:00:00
- Afternoon shift: 13:00:00 - 22:00:00
to store it in a new column:
date shift
2022-01-30 10:57:00 Morning
2022-01-30 12:02:00 Morning
2022-01-30 18:58:00 Afternoon
2022-01-30 19:27:00 Afternoon
2022-01-30 12:00:00 Morning
2022-01-31 02:10:00 Night
2022-01-31 01:55:00 Night
2022-01-31 01:24:00 Night
...
I have done this with this code (it works fine).
startTime = pd.to_datetime(entradaDf['startDate']).dt.strftime('%H:%M:%S')
condShift = [
startTime.ge('05:00:00') & startTime.lt('13:30:00'), # Morning
startTime.ge('13:30:00') & startTime.lt('22:00:00'), # Afternoon
startTime.ge('22:00:00') & startTime.lt('00:00:00'), # Night
startTime.ge('00:00:00') & startTime.lt('05:00:00'), # Night
]
shiftValues = ['Mañana', 'Tarde', 'Noche', 'Noche']
df['shift'] = np.select(condShift , shiftValues , default='Night')
It work fine but, is there a way to do this more efficiently?