diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 8afbd293a095b..c1341438f7efe 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -3218,7 +3218,6 @@ def __init__(self, *args, **kwargs): self.metadata = [] self.info = dict() self.nan_rep = None - self.selection = None @property def table_type_short(self) -> str: @@ -3611,8 +3610,8 @@ def read_axes(self, where, **kwargs) -> bool: return False # create the selection - self.selection = Selection(self, where=where, **kwargs) - values = self.selection.select() + selection = Selection(self, where=where, **kwargs) + values = selection.select() # convert the data for a in self.axes: @@ -3902,7 +3901,7 @@ def get_blk_items(mgr, blocks): if validate: self.validate(existing_table) - def process_axes(self, obj, columns=None): + def process_axes(self, obj, selection: "Selection", columns=None): """ process axes filters """ # make a copy to avoid side effects @@ -3911,6 +3910,7 @@ def process_axes(self, obj, columns=None): # make sure to include levels if we have them if columns is not None and self.is_multi_index: + assert isinstance(self.levels, list) # assured by is_multi_index for n in self.levels: if n not in columns: columns.insert(0, n) @@ -3920,8 +3920,8 @@ def process_axes(self, obj, columns=None): obj = _reindex_axis(obj, axis, labels, columns) # apply the selection filters (but keep in the same order) - if self.selection.filter is not None: - for field, op, filt in self.selection.filter.format(): + if selection.filter is not None: + for field, op, filt in selection.filter.format(): def process_filter(field, filt): @@ -4014,10 +4014,10 @@ def read_coordinates( return False # create the selection - self.selection = Selection(self, where=where, start=start, stop=stop) - coords = self.selection.select_coords() - if self.selection.filter is not None: - for field, op, filt in self.selection.filter.format(): + selection = Selection(self, where=where, start=start, stop=stop) + coords = selection.select_coords() + if selection.filter is not None: + for field, op, filt in selection.filter.format(): data = self.read_column( field, start=coords.min(), stop=coords.max() + 1 ) @@ -4302,8 +4302,8 @@ def delete( # create the selection table = self.table - self.selection = Selection(self, where, start=start, stop=stop) - values = self.selection.select_coords() + selection = Selection(self, where, start=start, stop=stop) + values = selection.select_coords() # delete the rows in reverse order sorted_series = Series(values).sort_values() @@ -4406,8 +4406,9 @@ def read(self, where=None, columns=None, **kwargs): else: df = concat(frames, axis=1) + selection = Selection(self, where=where, **kwargs) # apply the selection filters & axis orderings - df = self.process_axes(df, columns=columns) + df = self.process_axes(df, selection=selection, columns=columns) return df