Skip to content

Commit e92349e

Browse files
committed
BUG: set tz on DTI from fixed format HDFStore
Set the tz after creating the DatetimeIndex instance when reading from a fixed format HDFStore. Setting the tz during instance creation offset data. closes pandas-dev#17618
1 parent 727ea20 commit e92349e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ Indexing
938938
I/O
939939
^^^
940940

941+
- Bug in :func:`read_hdf` when reading a timezone aware index from ``fixed`` format HDFStore (:issue:`17618`)
941942
- Bug in :func:`read_csv` in which columns were not being thoroughly de-duplicated (:issue:`17060`)
942943
- Bug in :func:`read_csv` in which specified column names were not being thoroughly de-duplicated (:issue:`17095`)
943944
- Bug in :func:`read_csv` in which non integer values for the header argument generated an unhelpful / unrelated error message (:issue:`16338`)

pandas/io/pytables.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2391,8 +2391,11 @@ def _alias_to_class(self, alias):
23912391
def _get_index_factory(self, klass):
23922392
if klass == DatetimeIndex:
23932393
def f(values, freq=None, tz=None):
2394-
return DatetimeIndex._simple_new(values, None, freq=freq,
2395-
tz=tz)
2394+
# data are already in UTC, localize and convert if tz present
2395+
result = DatetimeIndex._simple_new(values, None, freq=freq)
2396+
if tz is not None:
2397+
result = result.tz_localize('UTC').tz_convert(tz)
2398+
return result
23962399
return f
23972400
elif klass == PeriodIndex:
23982401
def f(values, freq=None, tz=None):

pandas/tests/io/test_pytables.py

+11
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,17 @@ def test_calendar_roundtrip_issue(self):
22722272
result = store.select('table')
22732273
assert_series_equal(result, s)
22742274

2275+
def test_roundtrip_tz_aware_index(self):
2276+
# GH 17618
2277+
time = pd.Timestamp('2000-01-01 01:00:00', tz='US/Eastern')
2278+
df = pd.DataFrame(data=[0], index=[time])
2279+
2280+
with ensure_clean_store(self.path) as store:
2281+
store.put('frame', df, format='fixed')
2282+
recons = store['frame']
2283+
tm.assert_frame_equal(recons, df)
2284+
assert recons.index[0].value == 946706400000000000
2285+
22752286
def test_append_with_timedelta(self):
22762287
# GH 3577
22772288
# append timedelta

0 commit comments

Comments
 (0)