0

I have a time field that goes from 07:00 to 21:00. I want to make bins of 20 minutes, is there something like this in python:

07:00 - 07:20
07:20 - 07:40
07:40 - 08:00
08:00 - 08:20
08:20 - 08:40
08:40 - 09:00
09:00 - 09:20
09:20 - 09:40
09:40 - 10:00
John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Yes, use the standard [**`datetime`**](https://docs.python.org/3/library/datetime.html) library, particularly [**`timedelta`**](https://docs.python.org/3/library/datetime.html#timedelta-objects) – Peter Wood Mar 26 '19 at 19:11
  • The accepted answer here does a pretty good job: https://stackoverflow.com/questions/10747974/how-to-check-if-the-current-time-is-in-range-in-python – Hoog Mar 26 '19 at 19:11
  • This question might help: [time - Rounding up to nearest 30 minutes in python](https://stackoverflow.com/q/32723150/4996248). Your binning is determined by the rounding. – John Coleman Mar 26 '19 at 19:19

1 Answers1

1

You may be able to easily do this with pd.cut, for example, if your time always ranges from 7:00-21:00, binned every 20 minutes means 3 per hour * 14 hours = 42 bins. https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.cut.html

df['binned_hours'] = pd.cut(df.timestamp.dt.hour, bins=42)

Michael B
  • 568
  • 3
  • 12