Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binning time column in Python

I had a datetime column (as object type) in my Pandas dataframe. I broke the datetime column to two columns date and time and I transformed both using to_datetime to the following format with the following code:

 df['Time'] =  pd.to_datetime(df['Time'], format='%H:%M:%S').dt.time
 df['Date'] =  pd.to_datetime(df['Date'], format='%Y.%m.%d')

After the above code execution, I have the Date column of type Datetime and the Time column (e.g., 00:14:53) of type object - I don't know why not Datetime since I used to_datetime-

I want to break the time column to 4 categories/bins, 00:00:00 - 06:00:00 as category 1, then 06:00:00 to 12:00:00 and so on.

I tried different methods with the cut but all wrong.

What I am doing wrong, any help how to do this?

Thanks

like image 315
Er1Hall Avatar asked May 03 '26 12:05

Er1Hall


1 Answers

Convert Time column to hours by Series.dt.hour and use cut for binning:

rng = pd.date_range('2017-04-03', periods=30, freq='H').strftime('%H:%M:%S')
df = pd.DataFrame({'Time': rng}) 

hours = pd.to_datetime(df['Time'], format='%H:%M:%S').dt.hour

df['cats'] = pd.cut(hours, 
                    bins=[0,6,12,18,24], 
                    include_lowest=True, 
                    labels=['cat1','cat2','cat3','cat4'])

print (df)
        Time  cats
0   00:00:00  cat1
1   01:00:00  cat1
2   02:00:00  cat1
3   03:00:00  cat1
4   04:00:00  cat1
5   05:00:00  cat1
6   06:00:00  cat1
7   07:00:00  cat2
8   08:00:00  cat2
9   09:00:00  cat2
10  10:00:00  cat2
11  11:00:00  cat2
12  12:00:00  cat2
13  13:00:00  cat3
14  14:00:00  cat3
15  15:00:00  cat3
16  16:00:00  cat3
17  17:00:00  cat3
18  18:00:00  cat3
19  19:00:00  cat4
20  20:00:00  cat4
21  21:00:00  cat4
22  22:00:00  cat4
23  23:00:00  cat4
24  00:00:00  cat1
25  01:00:00  cat1
26  02:00:00  cat1
27  03:00:00  cat1
28  04:00:00  cat1
29  05:00:00  cat1
like image 124
jezrael Avatar answered May 06 '26 02:05

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!