简体   繁体   中英

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

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

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 .

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM