Skip to content
Advertisement

How to add hours to the original time in dataframe then generate a new date?

The data in test.csv likes this:

device_id,upload_time
id1,2020-06-01 07:46:30+00:00
id2,2020-06-05 16:04:32+00:00

I want to remove the +00:00 in upload_time and add 8 hours to upload_time ,then generate a new column new_upload_time.

I use this code to do it.

import pandas as pd
from datetime import datetime, timedelta

df = pd.read_csv(r'E:/test.csv',parse_dates=[1], encoding='utf-8')
df['new_upload_time'] = pd.DatetimeIndex(df['upload_time'].dt.strftime('%Y-%m-%d %H:%M:%S'))+timedelta(hours=8)
df.to_csv(r'E:/result.csv', index=False, mode='w', header=True)

result.csv:

device_id,upload_time,new_upload_time
id1,2020-06-01 07:46:30+00:00,2020-06-01 15:46:30
id2,2020-06-05 16:04:32+00:00,2020-06-06 00:04:32

Although I have implemented it, I feel that the code is a bit complicated.

Is there a simple way?

Advertisement

Answer

Do the formatting after adding 8 hours. You are formatting as a string and then trying to add a number to it. Add the number first and then format as string with strftime:

df['upload_time'] = pd.to_datetime(df['upload_time'])
df['new_upload_time'] = df['upload_time'] + pd.Timedelta(hours=8)
df['new_upload_time'] = df['new_upload_time'].dt.strftime('%Y/%m/%d %H:%M:%S')
df['upload_time'] = df['upload_time'].dt.strftime('%Y/%m/%d %H:%M:%S') #pass whaetver format that you want to `strftime` here.
df
Out1]: 
  device_id          upload_time      new_upload_time
0       id1  2020/06/01 07:46:30  2020/06/01 15:46:30
1       id2  2020/06/05 16:04:32  2020/06/06 00:04:32

You can also specify the date_format on export:

df['upload_time'] = pd.to_datetime(df['upload_time'])
df['new_upload_time'] = df['upload_time'] + pd.Timedelta(hours=8)
df.to_csv('test.csv', index=False, date_format='%Y-%m-%d %H:%M:%S')
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement