Skip to content

Commit cce0e57

Browse files
committed
BUG: this fixes pandas-dev#11188
1 parent e53782f commit cce0e57

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

pandas/io/pytables.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,11 @@ def __init__(self, store, s, func, where, nrows, start=None, stop=None,
14591459
stop = nrows
14601460
stop = min(nrows, stop)
14611461

1462+
# Piggy-back normalized `nrows` (considering start and stop) onto
1463+
# PyTables tables object so that the GenericIndexCol constructor
1464+
# knows how large the index should be.
1465+
self.s.table._nrows_to_read = stop - start
1466+
14621467
self.nrows = nrows
14631468
self.start = start
14641469
self.stop = stop
@@ -1819,7 +1824,17 @@ def is_indexed(self):
18191824
def convert(self, values, nan_rep, encoding, errors):
18201825
""" set the values from this selection: take = take ownership """
18211826

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

18251840
def get_attr(self):

pandas/tests/io/test_pytables.py

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

52225222
def test_read_complete(self):
52235223
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5224-
5225-
df = pd.read_hdf(path, key=objname)
5226-
self._compare(df, samples)
5224+
self._compare(pd.read_hdf(path, key=objname), samples)
52275225

52285226
def test_read_with_start(self):
52295227
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5230-
5231-
# Currently this fails as of
5232-
# https://github.com/pandas-dev/pandas/issues/11188
5233-
with pytest.raises(ValueError, match='Shape of passed values is'):
5234-
df = pd.read_hdf(path, key=objname, start=1)
5235-
self._compare(df, samples[1:])
5228+
# This is a regression test for pandas-dev/pandas/issues/11188
5229+
self._compare(pd.read_hdf(path, key=objname, start=1), samples[1:])
52365230

52375231
def test_read_with_stop(self):
52385232
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5239-
5240-
# Currently this fails as of
5241-
# https://github.com/pandas-dev/pandas/issues/11188
5242-
with pytest.raises(ValueError, match='Shape of passed values is'):
5243-
df = pd.read_hdf(path, key=objname, stop=1)
5244-
self._compare(df, samples[0:1])
5233+
# This is a regression test for pandas-dev/pandas/issues/11188
5234+
self._compare(pd.read_hdf(path, key=objname, stop=1), samples[0:1])
52455235

52465236
def test_read_with_startstop(self):
52475237
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5248-
5249-
# Currently this fails as of
5250-
# https://github.com/pandas-dev/pandas/issues/11188
5251-
with pytest.raises(ValueError, match='Shape of passed values is'):
5252-
df = pd.read_hdf(path, key=objname, start=1, stop=2)
5253-
self._compare(df, samples[1:2])
5238+
# This is a regression test for pandas-dev/pandas/issues/11188
5239+
self._compare(
5240+
pd.read_hdf(path, key=objname, start=1, stop=2),
5241+
samples[1:2]
5242+
)

0 commit comments

Comments
 (0)