Skip to content

Commit

Permalink
Revert "patch: allow lit to broadcast as left operand (#854)" (#858)
Browse files Browse the repository at this point in the history
This reverts commit ad7e6fe.
  • Loading branch information
MarcoGorelli authored Aug 24, 2024
1 parent baa84b8 commit c072b5d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
4 changes: 2 additions & 2 deletions narwhals/_arrow/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ def all(self) -> ArrowExpr:
)

def lit(self, value: Any, dtype: dtypes.DType | None) -> ArrowExpr:
def _lit_arrow_series(df: ArrowDataFrame) -> ArrowSeries:
def _lit_arrow_series(_: ArrowDataFrame) -> ArrowSeries:
arrow_series = ArrowSeries._from_iterable(
data=[value] * len(df),
data=[value],
name="lit",
backend_version=self._backend_version,
)
Expand Down
12 changes: 3 additions & 9 deletions narwhals/_pandas_like/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from narwhals._pandas_like.utils import create_native_series
from narwhals._pandas_like.utils import horizontal_concat
from narwhals._pandas_like.utils import vertical_concat
from narwhals.utils import Implementation

if TYPE_CHECKING:
from narwhals._pandas_like.typing import IntoPandasLikeExpr
from narwhals.utils import Implementation


class PandasLikeNamespace:
Expand Down Expand Up @@ -130,17 +130,11 @@ def all(self) -> PandasLikeExpr:
)

def lit(self, value: Any, dtype: dtypes.DType | None) -> PandasLikeExpr:
if self._implementation is Implementation.CUDF: # pragma: no cover
import cupy as np # ignore-banned-import
else:
import numpy as np # ignore-banned-import

def _lit_pandas_series(df: PandasLikeDataFrame) -> PandasLikeSeries:
native_frame = df._native_frame
pandas_series = PandasLikeSeries._from_iterable(
data=np.full(native_frame.shape[0], value),
data=[value],
name="lit",
index=native_frame.index,
index=df._native_frame.index[0:1],
implementation=self._implementation,
backend_version=self._backend_version,
)
Expand Down
18 changes: 1 addition & 17 deletions tests/frame/lit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_lit(
request.applymarker(pytest.mark.xfail)
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor(data)
df = nw.from_native(df_raw)
df = nw.from_native(df_raw).lazy()
result = df.with_columns(nw.lit(2, dtype).alias("lit"))
expected = {
"a": [1, 3, 2],
Expand All @@ -35,22 +35,6 @@ def test_lit(
compare_dicts(result, expected)


def test_lit_operation(constructor: Any) -> None:
data = {"a": [1, 3, 2]}
df_raw = constructor(data)
df = nw.from_native(df_raw)

result = df.select(
left_lit=nw.lit(1) + nw.col("a"),
right_lit=nw.col("a") - nw.lit(1),
)
expected = {
"left_lit": [2, 4, 3],
"right_lit": [0, 2, 1],
}
compare_dicts(result, expected)


def test_lit_error(constructor: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor(data)
Expand Down

0 comments on commit c072b5d

Please sign in to comment.