Skip to content

Commit

Permalink
Fix rows.select bug
Browse files Browse the repository at this point in the history
  • Loading branch information
luis11011 committed Apr 7, 2022
1 parent 3cbd2c9 commit ad2d026
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions optimus/engines/base/rows.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC
from functools import reduce
from typing import Callable
from optimus.helpers.types import *

Expand Down Expand Up @@ -113,19 +114,45 @@ def select(self, expr=None, contains=None, case=None, flags=0, na=False, regex=F
df = self.root
dfd = df.data

if is_str(expr) or is_list_of_str(expr):
if expr in df.cols.names() or is_list_of_str(expr) or expr == "*":
all_cols = df.cols.names()

if expr == "*":
expr = all_cols

expr = val_to_list(expr)

if is_list_of_str(expr):

if all([col in all_cols for col in expr]):
if contains is not None:
expr = df.mask.contains(expr, value=contains, case=case, flags=flags, na=na, regex=regex).mask.any()
expr = [df.mask.contains(expr, value=contains, case=case, flags=flags, na=na, regex=regex).mask.any()]
else:
expr = df[expr].mask.any()
elif is_str(expr):
expr = eval(expr)

if not hasattr(expr, "get_series"):
raise ValueError(f"Invalid value for 'expr': {expr}")

dfd = dfd.reset_index(drop=True)[expr.get_series().reset_index(drop=True)].reset_index(drop=True)
expr = [df[expr].mask.any()]

else:
for i, expr_ in enumerate(expr):
if expr_ not in all_cols:
expr[i] = eval(expr_)
elif contains is not None:
expr[i] = df.mask.contains(expr_, value=contains, case=case, flags=flags, na=na, regex=regex)
else:
expr[i] = df[expr_]

if len(expr) > 1:

def _reduce_exprs(a, b):
if hasattr(a, "get_series"):
a = a.get_series()
if hasattr(b, "get_series"):
b = b.get_series()
return a & b

expr = reduce(_reduce_exprs, expr)

else:
expr = expr[0].get_series()

dfd = dfd.reset_index(drop=True)[expr.reset_index(drop=True)].reset_index(drop=True)

meta = Meta.action(df.meta, Actions.SELECT_ROW.value, df.cols.names())

Expand Down

0 comments on commit ad2d026

Please sign in to comment.