Skip to content

Commit a2a5cec

Browse files
JonasAbernotjreback
authored andcommitted
BUG: bug in proper serialization of TimedeltaIndex in fixed HDFStores (GH9635)
1 parent 9a5a9d3 commit a2a5cec

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

doc/source/whatsnew/v0.16.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Bug Fixes
7373

7474

7575
- Bug in ``DataFrame`` slicing may not retain metadata (:issue:`9776`)
76-
76+
- Bug where ``TimdeltaIndex`` were not properly serialized in fixed ``HDFStore`` (:issue:`9635`)
7777

7878

7979

pandas/io/pytables.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pandas.sparse.api import SparseSeries, SparseDataFrame, SparsePanel
1919
from pandas.sparse.array import BlockIndex, IntIndex
2020
from pandas.tseries.api import PeriodIndex, DatetimeIndex
21+
from pandas.tseries.tdi import TimedeltaIndex
2122
from pandas.core.base import StringMixin
2223
from pandas.core.common import adjoin, pprint_thing
2324
from pandas.core.algorithms import match, unique
@@ -4234,6 +4235,11 @@ def _convert_index(index, encoding=None, format_type=None):
42344235
freq=getattr(index, 'freq', None),
42354236
tz=getattr(index, 'tz', None),
42364237
index_name=index_name)
4238+
elif isinstance(index, TimedeltaIndex):
4239+
converted = index.asi8
4240+
return IndexCol(converted, 'timedelta64', _tables().Int64Col(),
4241+
freq=getattr(index, 'freq', None),
4242+
index_name=index_name)
42374243
elif isinstance(index, (Int64Index, PeriodIndex)):
42384244
atom = _tables().Int64Col()
42394245
return IndexCol(
@@ -4253,6 +4259,11 @@ def _convert_index(index, encoding=None, format_type=None):
42534259
freq=getattr(index, 'freq', None),
42544260
tz=getattr(index, 'tz', None),
42554261
index_name=index_name)
4262+
elif inferred_type == 'timedelta64':
4263+
converted = values.view('i8')
4264+
return IndexCol(converted, 'timedelta64', _tables().Int64Col(),
4265+
freq=getattr(index, 'freq', None),
4266+
index_name=index_name)
42564267
elif inferred_type == 'datetime':
42574268
converted = np.asarray([(time.mktime(v.timetuple()) +
42584269
v.microsecond / 1E6) for v in values],
@@ -4303,6 +4314,8 @@ def _unconvert_index(data, kind, encoding=None):
43034314
kind = _ensure_decoded(kind)
43044315
if kind == u('datetime64'):
43054316
index = DatetimeIndex(data)
4317+
elif kind == u('timedelta64'):
4318+
index = TimedeltaIndex(data)
43064319
elif kind == u('datetime'):
43074320
index = np.asarray([datetime.fromtimestamp(v) for v in data],
43084321
dtype=object)

pandas/io/tests/test_pytables.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pandas
1212
import pandas as pd
1313
from pandas import (Series, DataFrame, Panel, MultiIndex, Categorical, bdate_range,
14-
date_range, Index, DatetimeIndex, isnull)
14+
date_range, timedelta_range, Index, DatetimeIndex, TimedeltaIndex, isnull)
1515

1616
from pandas.io.pytables import _tables
1717
try:
@@ -4588,6 +4588,18 @@ def test_duplicate_column_name(self):
45884588
other = read_hdf(path, 'df')
45894589
tm.assert_frame_equal(df, other)
45904590

4591+
def test_preserve_timedeltaindex_type(self):
4592+
# GH9635
4593+
# Storing TimedeltaIndexed DataFrames in fixed stores did not preserve
4594+
# the type of the index.
4595+
df = DataFrame(np.random.normal(size=(10,5)))
4596+
df.index = timedelta_range(start='0s',periods=10,freq='1s',name='example')
4597+
4598+
with ensure_clean_store(self.path) as store:
4599+
4600+
store['df'] = df
4601+
assert_frame_equal(store['df'], df)
4602+
45914603

45924604
def _test_sort(obj):
45934605
if isinstance(obj, DataFrame):

0 commit comments

Comments
 (0)