Can I export pandas DataFrame to Excel stripping tzinfo?

假如想象 提交于 2019-12-12 03:57:34

问题


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

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

Is there a TimeSeries method to apply .replace(tzinfo=None) to each date in the index?

Alternativelly, is there a way to properly export time-aware TimeSeries to Excel?


回答1:


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]


来源:https://stackoverflow.com/questions/15967468/can-i-export-pandas-dataframe-to-excel-stripping-tzinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!