导出具有时区感知日期时间索引和夏令时的数据帧(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方法时有没有办法直接解决问题?
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)
作为对此的更新,似乎将日期范围指定为夏令时,因为DateTimeIndex根本不起作用并产生不明确的时间错误:
pd.DatetimeIndex(pd.date_range("2015-10-25 00:00", "2015-10-25 04:00", freq="H", tz="Europe/Berlin"))
任何暗示我缺少的东西?
答案 0 :(得分:0)
您没有指定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
答案 1 :(得分:-1)
我遇到了同样的问题。一旦使用索引
pytz.exceptions.AmbiguousTimeError:无法从Timestamp('2012-10-28 02:00:00')推断dst时间,尝试使用'ambiguous'参数
我不得不改变
data['time'] = date
data.set_index('time')
到此:
data['time'] = date
data.index = date
- >解决
和pd.to_csv()一样,我收到了这个错误:
pytz.exceptions.AmbiguousTimeError:无法从Timestamp('2012-10-28 02:00:00')推断dst时间,尝试使用'ambiguous'参数
通过降级大熊猫来解决问题:
pip install pandas==0.16.2