我可以将pandas DataFrame导出到Excel中以剥离tzinfo吗? [英] Can I export pandas DataFrame to Excel stripping tzinfo?
问题描述
我在pandas 0.10.1中有一个了解时区的TimeSeries.我想导出到Excel,但是时区阻止该日期被识别为Excel中的日期.
I have a timezone aware TimeSeries in pandas 0.10.1. I want to export to Excel, but the timezone prevents the date from being recognized as a date in Excel.
In [40]: resultado
Out[40]:
fecha_hora
2013-04-11 13:00:00+02:00 31475.568
2013-04-11 14:00:00+02:00 37263.072
2013-04-11 15:00:00+02:00 35979.434
2013-04-11 16:00:00+02:00 35132.890
2013-04-11 17:00:00+02:00 36356.584
如果我使用 .tz_convert(None)
删除tzinfo,则日期将转换为UTC:
If I strip the tzinfo with .tz_convert(None)
, the date gets converted to UTC:
In [41]: resultado.tz_convert(None)
Out[41]:
fecha_hora
2013-04-11 11:00:00 31475.568
2013-04-11 12:00:00 37263.072
2013-04-11 13:00:00 35979.434
2013-04-11 14:00:00 35132.890
2013-04-11 15:00:00 36356.584
是否有TimeSeries方法将 .replace(tzinfo = None)
应用于索引中的每个日期?
Is there a TimeSeries method to apply .replace(tzinfo=None)
to each date in the index?
或者,有没有一种方法可以将对时间敏感的TimeSeries正确导出到Excel?
Alternativelly, is there a way to properly export time-aware TimeSeries to Excel?
推荐答案
您可以简单地创建一个没有时区的副本.
You can simply create a copy without timezone.
import pandas as pa
time = pa.Timestamp('2013-04-16 10:08', tz='Europe/Berlin')
time_wo_tz = pa.datetime(year=time.year, month=time.month, day=time.day,
hour=time.hour, minute=time.minute, second=time.second,
microsecond=time.microsecond)
要转换时间序列的整个索引时,请使用列表推导.
When you want to convert the whole index of the timeseries, use a list comprehension.
ts.index = [pa.datetime(year=x.year, month=x.month, day=x.day,
hour=x.hour, minute=x.minute, second=x.second,
microsecond=x.microsecond)
for x in ts.index]
这篇关于我可以将pandas DataFrame导出到Excel中以剥离tzinfo吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!