Python 如何在dataframe中向原始时间添加小时数,然后生成新日期?

Python 如何在dataframe中向原始时间添加小时数,然后生成新日期?,python,pandas,Python,Pandas,test.csv中的数据如下所示: device_id,upload_time id1,2020-06-01 07:46:30+00:00 id2,2020-06-05 16:04:32+00:00 我想删除upload\u time中的+00:00,并在upload\u time中添加8小时,然后生成一个新列new\u upload\u time 我用这个代码来做这件事 import pandas as pd from datetime import datetime, timedelta

test.csv中的数据如下所示:

device_id,upload_time
id1,2020-06-01 07:46:30+00:00
id2,2020-06-05 16:04:32+00:00
我想删除
upload\u time
中的
+00:00
,并在
upload\u time
中添加8小时,然后生成一个新列
new\u upload\u time

我用这个代码来做这件事

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
虽然我已经实现了它,但我觉得代码有点复杂

有简单的方法吗?

在添加8小时后进行格式化。您正在将格式设置为字符串,然后尝试向其中添加数字。首先添加数字,然后使用
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
您还可以在导出时指定
date\u格式

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')
添加8小时后进行格式化。您正在将格式设置为字符串,然后尝试向其中添加数字。首先添加数字,然后使用
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
您还可以在导出时指定
date\u格式

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')

您可以使用

因此,代码应该如下所示:

df['new_upload_time'] = df['upload_time'].dt.tz_convert(None) + timedelta(hours=8)
这假设
upload\u time
dtype
Timestamp


注意,我将答案改为使用
tz\u convert
而不是
tz\u localize

您可以使用

因此,代码应该如下所示:

df['new_upload_time'] = df['upload_time'].dt.tz_convert(None) + timedelta(hours=8)
这假设
upload\u time
dtype
Timestamp


请注意,我将答案改为使用
tz\u convert
而不是
tz\u localize

先生,非常有用。@CR7没问题,请参阅我答案中的第二个选项。先生,非常有用。@CR7没问题,请参阅我答案中的第二个选项。非常有用。谢谢!非常有帮助,谢谢!