Skip to content

Commit c62b84f

Browse files
jbrockmendeljreback
authored andcommitted
REF: make selection not a state variable in io.pytables (#29804)
1 parent d6c6f18 commit c62b84f

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

pandas/io/pytables.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -3185,7 +3185,6 @@ def __init__(self, *args, **kwargs):
31853185
self.metadata = []
31863186
self.info = dict()
31873187
self.nan_rep = None
3188-
self.selection = None
31893188

31903189
@property
31913190
def table_type_short(self) -> str:
@@ -3568,8 +3567,8 @@ def read_axes(self, where, **kwargs) -> bool:
35683567
return False
35693568

35703569
# create the selection
3571-
self.selection = Selection(self, where=where, **kwargs)
3572-
values = self.selection.select()
3570+
selection = Selection(self, where=where, **kwargs)
3571+
values = selection.select()
35733572

35743573
# convert the data
35753574
for a in self.axes:
@@ -3857,7 +3856,7 @@ def get_blk_items(mgr, blocks):
38573856
if validate:
38583857
self.validate(existing_table)
38593858

3860-
def process_axes(self, obj, columns=None):
3859+
def process_axes(self, obj, selection: "Selection", columns=None):
38613860
""" process axes filters """
38623861

38633862
# make a copy to avoid side effects
@@ -3866,6 +3865,7 @@ def process_axes(self, obj, columns=None):
38663865

38673866
# make sure to include levels if we have them
38683867
if columns is not None and self.is_multi_index:
3868+
assert isinstance(self.levels, list) # assured by is_multi_index
38693869
for n in self.levels:
38703870
if n not in columns:
38713871
columns.insert(0, n)
@@ -3875,8 +3875,8 @@ def process_axes(self, obj, columns=None):
38753875
obj = _reindex_axis(obj, axis, labels, columns)
38763876

38773877
# apply the selection filters (but keep in the same order)
3878-
if self.selection.filter is not None:
3879-
for field, op, filt in self.selection.filter.format():
3878+
if selection.filter is not None:
3879+
for field, op, filt in selection.filter.format():
38803880

38813881
def process_filter(field, filt):
38823882

@@ -3966,10 +3966,10 @@ def read_coordinates(
39663966
return False
39673967

39683968
# create the selection
3969-
self.selection = Selection(self, where=where, start=start, stop=stop)
3970-
coords = self.selection.select_coords()
3971-
if self.selection.filter is not None:
3972-
for field, op, filt in self.selection.filter.format():
3969+
selection = Selection(self, where=where, start=start, stop=stop)
3970+
coords = selection.select_coords()
3971+
if selection.filter is not None:
3972+
for field, op, filt in selection.filter.format():
39733973
data = self.read_column(
39743974
field, start=coords.min(), stop=coords.max() + 1
39753975
)
@@ -4245,8 +4245,8 @@ def delete(
42454245

42464246
# create the selection
42474247
table = self.table
4248-
self.selection = Selection(self, where, start=start, stop=stop)
4249-
values = self.selection.select_coords()
4248+
selection = Selection(self, where, start=start, stop=stop)
4249+
values = selection.select_coords()
42504250

42514251
# delete the rows in reverse order
42524252
sorted_series = Series(values).sort_values()
@@ -4349,8 +4349,9 @@ def read(self, where=None, columns=None, **kwargs):
43494349
else:
43504350
df = concat(frames, axis=1)
43514351

4352+
selection = Selection(self, where=where, **kwargs)
43524353
# apply the selection filters & axis orderings
4353-
df = self.process_axes(df, columns=columns)
4354+
df = self.process_axes(df, selection=selection, columns=columns)
43544355

43554356
return df
43564357

0 commit comments

Comments
 (0)