【问题标题】:Pandas to_csv AmbiguousTimeError熊猫 to_csv AmbiguousTimeError
【发布时间】:2016-01-17 03:32:54
【问题描述】:

在导出具有时区感知日期时间索引和夏令时的数据帧(pandas 版本 17.0)时,to_csv 方法会引发 AmbiguousTimeError 时间错误。例如,

import pandas as pd
import numpy as np
df = pd.DataFrame(data={"c1": np.random.randn(960)}, 
                  index=pd.date_range("2015-10-1", periods=960, freq="H", tz="Europe/Berlin"))
df.to_csv("test.csv")

导致无法推断夏令时的错误:

AmbiguousTimeError: 无法从 Timestamp('2015-10-25 02:00:00') 推断 dst 时间,请尝试使用 'ambiguous' 参数

作为一种解决方法,我目前在导出之前将索引转换为字符串:

df.index = df.index.map(str)

调用 to_csv 方法时有没有办法直接解决问题?

更新1

pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Windows
OS-release: 8
machine: AMD64
processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.17.0
nose: 1.3.4
pip: 7.1.2
setuptools: 18.3.1
Cython: None
numpy: 1.10.0b1
scipy: 0.14.0
statsmodels: 0.6.1
IPython: 3.2.1
sphinx: 1.2.2
patsy: 0.3.0
dateutil: 2.4.2
pytz: 2015.4
blosc: None
bottleneck: 0.8.0
tables: 3.1.1
numexpr: 2.4
matplotlib: 1.4.3
openpyxl: 1.8.6
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.5.7
lxml: 3.3.5
bs4: 4.3.2
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: 0.9.7
pymysql: None
psycopg2: 2.5.3 (dt dec pq3 ext)

更新2

作为对此的更新,似乎将具有夏令时的日期范围分配为 DateTimeIndex 根本不起作用,并产生不明确的时间错误:

pd.DatetimeIndex(pd.date_range("2015-10-25 00:00", "2015-10-25 04:00", freq="H", tz="Europe/Berlin"))

任何提示我缺少什么?

【问题讨论】:

  • 为了完成这个,更新到 pandas 17.1 两个错误都不再发生了。

标签: python pandas export-to-csv dst


【解决方案1】:

您没有指定 pandas 版本。但这在 >= 0.14.1

中是可以的
In [18]: np.random.seed(1234)

In [19]: df = pd.DataFrame(data={"c1": np.random.randn(960)}, 
                  index=pd.date_range("2015-10-1", periods=960, freq="H", tz="Europe/Berlin"))

In [20]: df.to_csv("test.csv")

In [21]: df['20151025':'20151025 05:00:00']
Out[21]: 
                                 c1
2015-10-25 00:00:00+02:00 -0.424467
2015-10-25 01:00:00+02:00  1.118855
2015-10-25 02:00:00+02:00  1.569548
2015-10-25 02:00:00+01:00  1.427732
2015-10-25 03:00:00+01:00 -1.371838
2015-10-25 04:00:00+01:00 -0.266418
2015-10-25 05:00:00+01:00  0.779215

In [22]: !head -585 test.csv|tail -10
2015-10-24 22:00:00+02:00,-1.53089210839
2015-10-24 23:00:00+02:00,0.801888214216
2015-10-25 00:00:00+02:00,-0.424466712863
2015-10-25 01:00:00+02:00,1.11885497214
2015-10-25 02:00:00+02:00,1.56954806458
2015-10-25 02:00:00+01:00,1.42773177107
2015-10-25 03:00:00+01:00,-1.37183787312
2015-10-25 04:00:00+01:00,-0.266417892642
2015-10-25 05:00:00+01:00,0.779214565214
2015-10-25 06:00:00+01:00,-0.102814294685

【讨论】:

  • 版本为 17.0(已编辑问题)。我在15.0下试了一下,没有出现错误。在 17.0 下可以。
  • 嗨,杰夫,我已经更新了帖子并添加了另一个引发相同错误的示例。如果你能看看,让我知道我是否遗漏了什么,我会很高兴。
【解决方案2】:

我遇到了同样的问题。一次索引

pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from Timestamp('2012-10-28 02:00:00'),尝试使用'ambiguous'参数

我必须改变

data['time'] = date
data.set_index('time')

到这里:

data['time'] = date
data.index = date

--> 解决了

与 pd.to_csv() 一样,我得到了这个错误:

pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from Timestamp('2012-10-28 02:00:00'),尝试使用'ambiguous'参数

通过降级熊猫解决了这个问题:

pip install pandas==0.16.2

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多