Skip to content

Commit

Permalink
Synchronize input arg parsing between DataFrame and LazyFrame with_co…
Browse files Browse the repository at this point in the history
…lumns
  • Loading branch information
zundertj committed Jan 13, 2023
1 parent 5b11271 commit 8525792
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 0 additions & 2 deletions py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5571,8 +5571,6 @@ def with_columns(
└─────┴──────┴───────┴──────┴───────┘
"""
if exprs is not None and not isinstance(exprs, Sequence):
exprs = [exprs]
return (
self.lazy().with_columns(exprs, **named_exprs).collect(no_optimization=True)
)
Expand Down
14 changes: 9 additions & 5 deletions py-polars/polars/internals/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2474,11 +2474,15 @@ def with_columns(
elif exprs is None and not named_exprs:
raise ValueError("Expected at least one of 'exprs' or **named_exprs")

exprs = (
[]
if exprs is None
else ([exprs] if isinstance(exprs, pli.Expr) else list(exprs))
)
if exprs is None:
exprs = []
elif isinstance(exprs, pli.Expr):
exprs = [exprs]
elif isinstance(exprs, pli.Series):
exprs = [pli.lit(exprs)]
else:
exprs = list(exprs)

exprs.extend(
pli.expr_to_lit_or_expr(expr).alias(name)
for name, expr in named_exprs.items()
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,14 @@ def test_with_column_renamed(fruits_cars: pl.DataFrame) -> None:
assert res.columns[0] == "C"


def test_with_columns_single_series() -> None:
df = pl.DataFrame({"a": [1, 2]})
result = df.lazy().with_columns(pl.Series("b", [3, 4])).collect()

expected = pl.DataFrame({"a": [1, 2], "b": [3, 4]})
assert_frame_equal(result, expected)


def test_reverse() -> None:
out = pl.DataFrame({"a": [1, 2], "b": [3, 4]}).lazy().reverse()
expected = pl.DataFrame({"a": [2, 1], "b": [4, 3]})
Expand Down

0 comments on commit 8525792

Please sign in to comment.