0

I am trying to add hours to specific values in a pandas df so that they are consistent.

For the df below I want to add 24 hours to the first value.

import pandas as pd

d = ({
    'time' : ['3:00:00','27:30:00','28:00:00'],               
     })

df = pd.DataFrame(data=d)

I'm using this at the moment.

df['time'].iloc[0] = df['time'].iloc[0] + pd.Timedelta(hours=24)

But it produces:

              time
0  1 days 03:00:00
1         27:30:00
2         28:00:00

My intended output is:

              time
0         27:00:00
1         27:30:00
2         28:00:00
  • What dtype is `time`? If it's `timedelta` I'm surprised the others aren't displaying like that – user3483203 Aug 16 '18 at 23:34
  • Take look: https://stackoverflow.com/questions/34134971/python-format-timedelta-greater-than-24-hours-for-display-only-containing-hours – rafaelc Aug 16 '18 at 23:39

1 Answers1

0

First this is not time delta , if you doing the df.applymap(type) after the output, you will see the first one is time object, all other should be str.

I can only come out this in my mind

df1=df.time.str.split(':',expand=True)
df1.iloc[0,0]=str(int(df1.iloc[0,0])+24)
df1
Out[63]: 
    0   1   2
0  27  00  00
1  27  30  00
2  28  00  00
df1.apply(':'.join,1)# you can add `to_frame('Time') ` as the end 
Out[64]: 
0    27:00:00
1    27:30:00
2    28:00:00
dtype: object
BENY
  • 317,841
  • 20
  • 164
  • 234