Skip to content

Commit

Permalink
refactor(pandas): simplify pandas helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs authored and cpcloud committed Jan 18, 2024
1 parent b2cfd65 commit 6f5e33b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/pandas/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def visit(cls, op: ops.Project, parent, values):
df, all_scalars = asframe(values)
if all_scalars and len(parent) != len(df):
df = pd.concat([df] * len(parent))
return df
return df.reset_index(drop=True)

@classmethod
def visit(cls, op: ops.Filter, parent, predicates):
Expand Down
28 changes: 13 additions & 15 deletions ibis/backends/pandas/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def asframe(values: dict | tuple, concat=True):

columns = [asseries(v, size) for v in values]
if concat:
df = pd.concat(columns, axis=1, keys=names).reset_index(drop=True)
df = pd.concat(columns, axis=1, keys=names)
return df, all_scalars
else:
return columns, all_scalars
Expand All @@ -49,31 +49,29 @@ def generic(func: Callable, operands):


def rowwise(func: Callable, operands):
"""Kernel applied to a row, where all the operands are scalars."""
# dealing with a collection of series objects
df, all_scalars = asframe(operands)
result = df.apply(func, axis=1) # , **kwargs)
return result.iat[0] if all_scalars else result
df, _ = asframe(operands)
return df.apply(func, axis=1)


def columnwise(func: Callable, operands):
df, all_scalars = asframe(operands)
result = func(df)
return result.iat[0] if all_scalars else result
"""Kernel where all the operands are series objects."""
df, _ = asframe(operands)
return func(df)


def serieswise(func, operands):
"""Kernel where the first operand is a series object."""
(key, value), *rest = operands.items()
if isinstance(value, pd.Series):
# dealing with a single series object
return func(**operands)
else:
# dealing with a single scalar object
value = pd.Series([value])
operands = {key: value, **dict(rest)}
return func(**operands).iat[0]
# ensure that the first operand is a series object
value = asseries(value)
operands = {key: value, **dict(rest)}
return func(**operands)


def elementwise(func, operands):
"""Kernel applied to an element, where all the operands are scalars."""
value = operands.pop(next(iter(operands)))
if isinstance(value, pd.Series):
# dealing with a single series object
Expand Down
3 changes: 0 additions & 3 deletions ibis/backends/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,6 @@ def test_percent_rank_whole_table_no_order_by(backend, alltypes, df):

@pytest.mark.notimpl(["polars"], raises=com.OperationNotDefinedError)
@pytest.mark.notimpl(["dask"], raises=NotImplementedError)
@pytest.mark.broken(
["pandas"], reason="pandas returns incorrect results", raises=AssertionError
)
def test_grouped_ordered_window_coalesce(backend, alltypes, df):
t = alltypes
expr = (
Expand Down

0 comments on commit 6f5e33b

Please sign in to comment.