Skip to content

Commit

Permalink
REF: make selection not a state variable in io.pytables (pandas-dev#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 635af31 commit 5760244
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3185,7 +3185,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:
Expand Down Expand Up @@ -3568,8 +3567,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:
Expand Down Expand Up @@ -3857,7 +3856,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
Expand All @@ -3866,6 +3865,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)
Expand All @@ -3875,8 +3875,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):

Expand Down Expand Up @@ -3966,10 +3966,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
)
Expand Down Expand Up @@ -4245,8 +4245,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()
Expand Down Expand Up @@ -4349,8 +4349,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

Expand Down

0 comments on commit 5760244

Please sign in to comment.