简体   繁体   English

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

[英]to_csv error with pandas dataframe with timezone

The following code gives an error under the pandas 0.17 but work very well with the 0.16.2. 以下代码在熊猫0.17下给出了错误,但在0.16.2下可以很好地工作。

No problem with the to_pickle function but get an error with the to_csv . 没有问题与to_pickle功能,但获得与错误to_csv

Has someone a tip to deal with it ? 有人给小费吗?

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

This seems to be a known bug #11619 and should be fixed in 0.17.1 这似乎是一个已知的错误#11619 ,应在0.17.1中修复。

The underlying issue is that your timeframe crosses from standard time to daylight saving time, which is the exact time showing in the error AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00') 根本问题是您的时间跨度从标准时间跨到夏令时,这是错误AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00')显示的确切时间AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM