Skip to content

Commit 9c1e738

Browse files
jzwinckjreback
authored andcommitted
BUG: preserve DatetimeIndex.name in HDFStore/read_hdf() with tz (#13884)
closes #13884 Author: John Zwinck <[email protected]> Closes #13888 from jzwinck/fix-13884 and squashes the following commits: 789fa59 [John Zwinck] BUG: preserve DatetimeIndex.name in HDFStore/read_hdf() with tz (#13884)
1 parent 0be0d67 commit 9c1e738

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ Bug Fixes
880880
- Bug in ``isnull`` ``notnull`` raise ``TypeError`` if input datetime-like has other unit than ``ns`` (:issue:`13389`)
881881
- Bug in ``.merge`` may raise ``TypeError`` if input datetime-like has other unit than ``ns`` (:issue:`13389`)
882882

883-
883+
- Bug in ``HDFStore``/``read_hdf()`` discarded ``DatetimeIndex.name`` if ``tz`` was set (:issue:`13884`)
884884

885885
- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)
886886
- Bug in ``groupby`` with ``as_index=False`` returns all NaN's when grouping on multiple columns including a categorical one (:issue:`13204`)

pandas/io/pytables.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4339,9 +4339,10 @@ def _set_tz(values, tz, preserve_UTC=False, coerce=False):
43394339
coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
43404340
"""
43414341
if tz is not None:
4342+
name = getattr(values, 'name', None)
43424343
values = values.ravel()
43434344
tz = tslib.get_timezone(_ensure_decoded(tz))
4344-
values = DatetimeIndex(values)
4345+
values = DatetimeIndex(values, name=name)
43454346
if values.tz is None:
43464347
values = values.tz_localize('UTC').tz_convert(tz)
43474348
if preserve_UTC:

pandas/io/tests/test_pytables.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@ def test_store_hierarchical(self):
28512851
with ensure_clean_store(self.path) as store:
28522852
store['frame'] = frame
28532853
recons = store['frame']
2854-
assert(recons.index.names == ('foo', 'bar'))
2854+
tm.assert_frame_equal(recons, frame)
28552855

28562856
def test_store_index_name(self):
28572857
df = tm.makeDataFrame()
@@ -2860,7 +2860,19 @@ def test_store_index_name(self):
28602860
with ensure_clean_store(self.path) as store:
28612861
store['frame'] = df
28622862
recons = store['frame']
2863-
assert(recons.index.name == 'foo')
2863+
tm.assert_frame_equal(recons, df)
2864+
2865+
def test_store_index_name_with_tz(self):
2866+
# GH 13884
2867+
df = pd.DataFrame({'A': [1, 2]})
2868+
df.index = pd.DatetimeIndex([1234567890123456787, 1234567890123456788])
2869+
df.index = df.index.tz_localize('UTC')
2870+
df.index.name = 'foo'
2871+
2872+
with ensure_clean_store(self.path) as store:
2873+
store.put('frame', df, format='table')
2874+
recons = store['frame']
2875+
tm.assert_frame_equal(recons, df)
28642876

28652877
def test_store_series_name(self):
28662878
df = tm.makeDataFrame()
@@ -2869,7 +2881,7 @@ def test_store_series_name(self):
28692881
with ensure_clean_store(self.path) as store:
28702882
store['series'] = series
28712883
recons = store['series']
2872-
assert(recons.name == 'A')
2884+
tm.assert_series_equal(recons, series)
28732885

28742886
def test_store_mixed(self):
28752887

0 commit comments

Comments
 (0)