diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 8afbd293a095b..f6fd39308e0cc 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1634,7 +1634,6 @@ def __init__( self.start = start self.stop = stop - self.coordinates = None if iterator or chunksize is not None: if chunksize is None: chunksize = 100000 @@ -1644,14 +1643,12 @@ def __init__( self.auto_close = auto_close - def __iter__(self): - - # iterate + def iter_result(self, coordinates): current = self.start while current < self.stop: stop = min(current + self.chunksize, self.stop) - value = self.func(None, None, self.coordinates[current:stop]) + value = self.func(None, None, coordinates[current:stop]) current = stop if value is None or not len(value): continue @@ -1671,9 +1668,8 @@ def get_result(self, coordinates: bool = False): if not self.s.is_table: raise TypeError("can only use an iterator or chunksize on a table") - self.coordinates = self.s.read_coordinates(where=self.where) - - return self + coordinates = self.s.read_coordinates(where=self.where) + return self.iter_result(coordinates) # if specified read via coordinates (necessary for multiple selections if coordinates: diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index d79280f9ea494..d7a4579a2f4e9 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -52,7 +52,6 @@ ) from pandas.io import pytables as pytables # noqa: E402 isort:skip -from pandas.io.pytables import TableIterator # noqa: E402 isort:skip _default_compressor = "blosc" @@ -4528,10 +4527,8 @@ def test_read_hdf_iterator(self, setup_path): df.to_hdf(path, "df", mode="w", format="t") direct = read_hdf(path, "df") iterator = read_hdf(path, "df", iterator=True) - assert isinstance(iterator, TableIterator) indirect = next(iterator.__iter__()) tm.assert_frame_equal(direct, indirect) - iterator.store.close() def test_read_hdf_errors(self, setup_path): df = DataFrame(np.random.rand(4, 5), index=list("abcd"), columns=list("ABCDE"))