Skip to content

Commit e9c7c39

Browse files
committed
BUG: this fixes pandas-dev#11188
1 parent 19dc304 commit e9c7c39

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

pandas/io/pytables.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,11 @@ def __init__(self, store, s, func, where, nrows, start=None, stop=None,
14561456
stop = nrows
14571457
stop = min(nrows, stop)
14581458

1459+
# Piggy-back normalized `nrows` (considering start and stop) onto
1460+
# PyTables tables object so that the GenericIndexCol constructor
1461+
# knows how large the index should be.
1462+
self.s.table._nrows_to_read = stop - start
1463+
14591464
self.nrows = nrows
14601465
self.start = start
14611466
self.stop = stop
@@ -1816,7 +1821,16 @@ def is_indexed(self):
18161821
def convert(self, values, nan_rep, encoding, errors):
18171822
""" set the values from this selection: take = take ownership """
18181823

1819-
self.values = Int64Index(np.arange(self.table.nrows))
1824+
if hasattr(self.table, '_nrows_to_read'):
1825+
# The `_nrows_to_read` property is set on the table object by the
1826+
# code path invoked by the top-level `read_hdf()`, and calculated
1827+
# based on the start` and `stop` integer values. These values allow
1828+
# for a sub-selection and likewise the index size needs to be
1829+
# adjusted to the size of this sub-selection.
1830+
self.values = Int64Index(np.arange(self.table._nrows_to_read))
1831+
else:
1832+
self.values = Int64Index(np.arange(self.table.nrows))
1833+
18201834
return self
18211835

18221836
def get_attr(self):

pandas/tests/io/test_pytables.py

+10-21
Original file line numberDiff line numberDiff line change
@@ -5231,33 +5231,22 @@ def _compare(self, df, samples):
52315231

52325232
def test_read_complete(self):
52335233
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5234-
5235-
df = pd.read_hdf(path, key=objname)
5236-
self._compare(df, samples)
5234+
self._compare(pd.read_hdf(path, key=objname), samples)
52375235

52385236
def test_read_with_start(self):
52395237
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5240-
5241-
# Currently this fails as of
5242-
# https://github.com/pandas-dev/pandas/issues/11188
5243-
with pytest.raises(ValueError, match='Shape of passed values is'):
5244-
df = pd.read_hdf(path, key=objname, start=1)
5245-
self._compare(df, samples[1:])
5238+
# This is a regression test for pandas-dev/pandas/issues/11188
5239+
self._compare(pd.read_hdf(path, key=objname, start=1), samples[1:])
52465240

52475241
def test_read_with_stop(self):
52485242
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5249-
5250-
# Currently this fails as of
5251-
# https://github.com/pandas-dev/pandas/issues/11188
5252-
with pytest.raises(ValueError, match='Shape of passed values is'):
5253-
df = pd.read_hdf(path, key=objname, stop=1)
5254-
self._compare(df, samples[0:1])
5243+
# This is a regression test for pandas-dev/pandas/issues/11188
5244+
self._compare(pd.read_hdf(path, key=objname, stop=1), samples[0:1])
52555245

52565246
def test_read_with_startstop(self):
52575247
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5258-
5259-
# Currently this fails as of
5260-
# https://github.com/pandas-dev/pandas/issues/11188
5261-
with pytest.raises(ValueError, match='Shape of passed values is'):
5262-
df = pd.read_hdf(path, key=objname, start=1, stop=2)
5263-
self._compare(df, samples[1:2])
5248+
# This is a regression test for pandas-dev/pandas/issues/11188
5249+
self._compare(
5250+
pd.read_hdf(path, key=objname, start=1, stop=2),
5251+
samples[1:2]
5252+
)

0 commit comments

Comments
 (0)