我有一个数据集要根据'user_id'
和'contest_id'
进行分组,其中,我必须按日期和时间升序对每个比赛中进入比赛的每个用户进行排序订单。
我首先尝试根据contest_id
和user handle
对数据进行分组,然后在将datetime
列转换为`to_datetime'
当我尝试保存代码时,出现错误 '''
Excel doesn't support timezones in datetimes. Set the tzinfo in the
datetime/time object to None or use the 'remove_timezone' Workbook()
option
'''
dftotal.groupby(["contestID", "userHandle"])
dftotal["registerDateTime"]=pd.to_datetime(dftotal.registerDateTime)
dftotal["RegistrationDateTime"] = dftotal["registerDateTime"]
dftotal["submitDateTime"] = pd.to_datetime(dftotal.submitDateTime)
dftotal["SubmissionDateTime"] = dftotal["submitDateTime"]
dftotal = dftotal.sort_values(by=['RegistrationDateTime'])
数据是
contest_id user_id registration submission score
1234 abc 2012-01-09 2012-01-09 90
21:51:00+00:00 22:51:00+00:00
4489 pabc 2013-01-09 2013-01-09 39
21:51:00+00:00 22:55:00+00:00
1234 tiop 2012-01-09 2012-01-09 100
23:51:00+00:00 23:55:00+00:00
4489 pabceu 2013-01-09 2013-01-09 39
23:20:00+00:00 23:55:00+00:00
预期为
contest_id user_id registration submission score
1234 abc 2012-01-09 2012-01-09 90
21:51:00+00:00 22:51:00+00:00
1234 tiop 2012-01-09 2012-01-09 100
23:51:00+00:00 23:55:00+00:00
4489 pabc 2013-01-09 2013-01-09 39
21:51:00+00:00 22:55:00+00:00
4489 pabceu 2013-01-09 2013-01-09 39
23:20:00+00:00 23:55:00+00:00
答案 0 :(得分:0)
我终于可以复制并修复了。
import pandas as pd
import io
t = '''contest_id user_id registration submission score
1234 abc 2012-01-09 21:51:00+00:00 2012-01-09 22:51:00+00:00 90
4489 pabc 2013-01-09 21:51:00+00:00 2013-01-09 22:55:00+00:00 39
1234 tiop 2012-01-09 23:51:00+00:00 2012-01-09 23:55:00+00:00 100
4489 pabceu 2013-01-09 23:20:00+00:00 2013-01-09 23:55:00+00:00 39'''
dftotal=pd.read_csv(io.StringIO(t), sep=r'\s\s+', engine='python')
print(dftotal.to_string())
dftotal['registration'] = pd.to_datetime(dftotal.registration, utc=True)
dftotal['submission'] = pd.to_datetime(dftotal.submission, utc=True)
print(dftotal.to_string())
dftotal.to_excel('contest_new.xlsx')
哪个显示:
contest_id user_id registration submission score
0 1234 abc 2012-01-09 21:51:00+00:00 2012-01-09 22:51:00+00:00 90
1 4489 pabc 2013-01-09 21:51:00+00:00 2013-01-09 22:55:00+00:00 39
2 1234 tiop 2012-01-09 23:51:00+00:00 2012-01-09 23:55:00+00:00 100
3 4489 pabceu 2013-01-09 23:20:00+00:00 2013-01-09 23:55:00+00:00 39
contest_id user_id registration submission score
0 1234 abc 2012-01-09 21:51:00+00:00 2012-01-09 22:51:00+00:00 90
2 1234 tiop 2012-01-09 23:51:00+00:00 2012-01-09 23:55:00+00:00 100
1 4489 pabc 2013-01-09 21:51:00+00:00 2013-01-09 22:55:00+00:00 39
3 4489 pabceu 2013-01-09 23:20:00+00:00 2013-01-09 23:55:00+00:00 39
并加注:
TypeError:Excel在日期时间中不支持时区。将datetime / time对象中的tzinfo设置为None或使用'remove_timezone'Workbook()选项
使用openpyxl:
xlsxwriter后端引发此错误。如果已安装openpyxl,则足以要求该引擎:
...
dftotal.to_excel('contest_new.xlsx', engine='openpyxl')
它会自动删除tz信息并正确写入excel文件
明确删除ts信息:
可以使用tz_localize(None)
明确删除时区信息:
...
dftotal['registration'] = pd.to_datetime(dftotal.registration).dt.tz_localize(None)
dftotal['submission'] = pd.to_datetime(dftotal.submission).dt.tz_localize(None)
dftotal = dftotal.sort_values(by=['registration'])
print(dftotal.to_string())
dftotal.to_excel('contest_new.xlsx')
数据框显示为:
contest_id user_id registration submission score
0 1234 abc 2012-01-09 21:51:00 2012-01-09 22:51:00 90
2 1234 tiop 2012-01-09 23:51:00 2012-01-09 23:55:00 100
1 4489 pabc 2013-01-09 21:51:00 2013-01-09 22:55:00 39
3 4489 pabceu 2013-01-09 23:20:00 2013-01-09 23:55:00 39
,并由默认的xlsxwriter引擎正确写入。