带有时区的pandas数据帧出现to_csv错误

时间:2015-11-18 13:56:02

标签: python pandas timezone

以下代码在pandas 0.17下给出错误,但在0.16.2下工作得很好。

to_pickle功能没问题,但to_csv收到错误。

有人要小费处理吗?

In[23]: new_index = pd.date_range('2015-01-01', '2015-12-31', freq = 'H', tz='Europe/Paris')
In[24]: df = pd.DataFrame({}, index = new_index)
In[25]: df['test'] = 1.
In[26]: df.to_pickle(r'test.h5')

In[27]: df.to_csv(r'test.csv')
Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-27-2ced74ae66e1>", line 1, in <module>
    df.to_csv(r'test.csv')
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 1289, in to_csv
    formatter.save()
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 1494, in save
    self._save()
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 1594, in _save
    self._save_chunk(start_i, end_i)
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 1619, in _save_chunk
    quoting=self.quoting)
  File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 1292, in to_native_types
    return values._format_native_types(**kwargs)
  File "C:\Anaconda\lib\site-packages\pandas\tseries\index.py", line 746, in _format_native_types
    format = _get_format_datetime64_from_values(self, date_format)
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 2191, in _get_format_datetime64_from_values
    is_dates_only = _is_dates_only(values)
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 2145, in _is_dates_only
    values = DatetimeIndex(values)
  File "C:\Anaconda\lib\site-packages\pandas\util\decorators.py", line 89, in wrapper
    return func(*args, **kwargs)
  File "C:\Anaconda\lib\site-packages\pandas\tseries\index.py", line 344, in __new__
    ambiguous=ambiguous)
  File "pandas\tslib.pyx", line 3753, in pandas.tslib.tz_localize_to_utc (pandas\tslib.c:64516)
AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00'), try using the 'ambiguous' argument

1 个答案:

答案 0 :(得分:1)

这似乎是一个已知的错误#11619,应该在0.17.1中修复

根本问题是您的时间范围从标准时间跨越到夏令时,这是错误显示的确切时间AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00')