【发布时间】: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