我正在阅读一个大型CSV文件,其中包含各种不同国家/地区的数据:
def get_ods_reader():
ods_reader = pd.read_csv("mycsv.csv",
chunksize=200000, parse_dates=[6, 9, 10, 16],
dtype={"account_nbr": object, "REPOSSESSION_STATUS_CD": object},
converters={"repossession_ind": parse_int},
date_parser=parse_date)
return ods_reader
with pd.HDFStore("ods_07.h5", "w") as store:
for chunk in get_ods_reader():
for country in countries:
data = chunk[chunk.country_cd == country]
data.reset_index(drop=True, inplace=True)
data.to_hdf(store, country, append=True, format="t", data_columns=True,
min_itemsize={"account_nbr": 12, "npa_stage": 8})
我收到以下错误:TypeError: too many timezones in this block, create separate data columns
我专门遍历不同的国家/地区并将它们分开存储,以免出现此错误。在我尝试将所有内容存储在一个文件中之前,我得到了同样的错误,这对我来说很有意义。为什么我仍然会收到错误,尽管所有日期时间列都应该为每个国家/地区设置相同的时区?
修改 可能的块看起来像这样:
account_nbr country_cd date1 date2 \
4400000 111111 AT 2017-03-31 2017-07-28
4400001 222222 CH 2017-03-31 2017-07-27
4400002 333333 DE 2017-03-31 2017-07-29
4400003 444444 BR 2017-03-31 2017-07-28
4400004 555555 MX 2017-03-31 2017-07-30
date3 npa_stage date4 amt1
4400000 2017-06-27 REGULAR NaT 10000.00
4400001 2017-06-28 REGULAR NaT 10000.00
4400002 2017-06-29 REGULAR NaT 10000.00
4400003 2017-06-29 REGULAR NaT 10000.00
4400004 2017-06-28 REGULAR NaT 10000.00
答案 0 :(得分:1)
<强>更新强>
演示:
In [355]: df = pd.DataFrame({
...: 'date':pd.date_range('2018-01-01', freq='3T', periods=2,
...: tz='Europe/Berlin')})
...:
In [357]: df.to_hdf('d:/temp/test.h5', 'tab', format='t', mode='a', append=True, data_columns=True)
In [358]: df1 = pd.DataFrame({
...: 'date':pd.date_range('2018-01-01', freq='3T', periods=5,
...: tz='Europe/Kiev')})
...:
In [359]: df1.to_hdf('d:/temp/test.h5', 'tab', format='t', mode='a', append=True, data_columns=True)
...
skipped
...
ValueError: invalid info for [date] for [tz], existing_value [Europe/Berlin] conflicts with new value [Europe/Kiev]
PS错误信息非常明确且不言自明......
旧回答:
我不确定它是否可行 - 如果您想存储一个时区感知的列,时区信息将保存为整个列/系列的元数据。
考虑以下演示:
In [223]: df = pd.DataFrame({
'date':pd.date_range('2018-01-01', freq='3T', periods=5,
tz='Europe/Berlin')})
In [224]: df
Out[224]:
date
0 2018-01-01 00:00:00+01:00
1 2018-01-01 00:03:00+01:00
2 2018-01-01 00:06:00+01:00
3 2018-01-01 00:09:00+01:00
4 2018-01-01 00:12:00+01:00
In [225]: df.dtypes
Out[225]:
date datetime64[ns, Europe/Berlin] # <----
dtype: object
In [226]: df.loc[:1, 'date'] = pd.date_range('2018-05-01', freq='30T', periods=2,
tz='Europe/Kiev')
In [227]: df.dtypes
Out[227]:
date object # <----
dtype: object
注意:只要我在系列中添加了第二个(不同的)时区,它的dtype就会更改为object