Home > Blockchain >  How to append hour:min:sec to the DateTime in pandas Dataframe
How to append hour:min:sec to the DateTime in pandas Dataframe

Time:09-11

I have following dataframe, where date was set as the index col,

date || renormalized
2017-01-01 | 6
2017-01-08 | 5
2017-01-15 | 3
2017-01-22 | 3
2017-01-29 | 3

I want to append 00:00:00 to each of the datetime in the index column, make it like

date || renormalized
2017-01-01 00:00:00 | 6
2017-01-08 00:00:00 | 5
2017-01-15 00:00:00 | 3
2017-01-22 00:00:00 | 3
2017-01-29 00:00:00 | 3

It seems I got stuck for no solution to make it happen.... It will be great if anyone can help...

Thanks

AL

CodePudding user response:

When your time is 0 for all instances, pandas doesn't show the time by default (although it's a Timestamp class, so it has the time!). Probably your data is already normalized, and you can perform delta time operations as usual.

You can see a target observation with df.index[0] for instance, or take a look at all the times with df.index.time.

CodePudding user response:

You can use DatetimeIndex.strftime

df.index = pd.to_datetime(df.index).strftime('%Y-%m-%d %H:%M:%S')
print(df)

                     renormalized
date
2017-01-01 00:00:00             6
2017-01-08 00:00:00             5
2017-01-15 00:00:00             3
2017-01-22 00:00:00             3
2017-01-29 00:00:00             3

Or you can choose

df.index = df.index   ' 00:00:00'
  • Related