Home > Software design >  how to get the part of the day from a given 24 hour time format in pandas and python
how to get the part of the day from a given 24 hour time format in pandas and python

Time:11-30

Hi I have dataset in which a col value looks like 08:25:00 I want to the resultant value as morning.

10:36:00  - Morning 
16:00:00 - afternoon
17:00:00 - afternoon 
19:00:00 -evening 

I tried with this below steps but for few rows I am getting Nan values and incorrect result

df['PNR_CREATE_TM_1']=pd.DataFrame({'PNR_CREATE_TM':range(1,25)})

bns=[0,4,8,12,16,20,24]

part_days=['Late Night','Early Morning','Morning','Noon','Evening','Night']

df['PNR_CREATE_SESSION'] = pd.cut(df['PNR_CREATE_TM_1'],bins=bns,labels=part_days,include_lowest=True)

CodePudding user response:

Assuming 'time' the initial column as string type, you could split the hours, and use pandas.cut:

df = pd.DataFrame({'time': ['10:36:00', '16:00:00', '17:00:00', '19:00:00']})
bns=[0,4,8,12,16,20,24]
part_days=['Late Night','Early Morning','Morning','Noon','Evening','Night']

s = df['time'].str.split(':').str[0].astype(int)
df['part'] = pd.cut(s, bins=bns, labels=part_days, include_lowest=True)

output:

       time     part
0  10:36:00  Morning
1  16:00:00     Noon
2  17:00:00  Evening
3  19:00:00  Evening

CodePudding user response:

Convert values to datetimes by to_datetime and get hours by Series.dt.hour:

df['PNR_CREATE_SESSION'] = pd.cut(pd.to_datetime(df['PNR_CREATE_TM_1']).dt.hour,
                                  bins=bns,
                                  labels=part_days,
                                  include_lowest=True)

Or if python object times:

df['PNR_CREATE_SESSION'] = pd.cut(pd.to_datetime(df['PNR_CREATE_TM_1'].astype(str)).dt.hour,
                                  bins=bns,
                                  labels=part_days,
                                  include_lowest=True)
  • Related