Skip to content

Commit

Permalink
feat: raise informative error message for DataFrame.__eq__/__neq__ (#…
Browse files Browse the repository at this point in the history
…1494)

* feat: raise informative error message for DataFrame.__eq__/__neq__

* type
  • Loading branch information
MarcoGorelli authored Dec 3, 2024
1 parent e02685b commit ffa2abb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
22 changes: 22 additions & 0 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,28 @@ def unpivot(
)
)

def __neq__(self, other: Any) -> NoReturn:
msg = (
"DataFrame.__neq__ and LazyFrame.__neq__ are not implemented, please "
"use expressions instead.\n\n"
"Hint: instead of\n"
" df != 0\n"
"you may want to use\n"
" df.select(nw.all() != 0)"
)
raise NotImplementedError(msg)

def __eq__(self, other: object) -> NoReturn:
msg = (
"DataFrame.__eq__ and LazyFrame.__eq__ are not implemented, please "
"use expressions instead.\n\n"
"Hint: instead of\n"
" df == 0\n"
"you may want to use\n"
" df.select(nw.all() == 0)"
)
raise NotImplementedError(msg)


class DataFrame(BaseFrame[DataFrameT]):
"""Narwhals DataFrame, backed by a native dataframe.
Expand Down
17 changes: 17 additions & 0 deletions tests/frame/eq_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

import narwhals as nw

if TYPE_CHECKING:
from tests.utils import Constructor


def test_eq_neq_raise(constructor: Constructor) -> None:
with pytest.raises(NotImplementedError, match="please use expressions"):
nw.from_native(constructor({"a": [1, 2, 3]})) == 0 # noqa: B015
with pytest.raises(NotImplementedError, match="please use expressions"):
nw.from_native(constructor({"a": [1, 2, 3]})) != 0 # noqa: B015
6 changes: 3 additions & 3 deletions tests/translate/from_native_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_pandas_like_validate() -> None:
def test_init_already_narwhals() -> None:
df = nw.from_native(pl.DataFrame({"a": [1, 2, 3]}))
result = nw.from_native(df)
assert result is df # type: ignore[comparison-overlap]
assert result is df
s = df["a"]
result_s = nw.from_native(s, allow_series=True)
assert result_s is s
Expand All @@ -194,7 +194,7 @@ def test_init_already_narwhals() -> None:
def test_init_already_narwhals_unstable() -> None:
df = unstable_nw.from_native(pl.DataFrame({"a": [1, 2, 3]}))
result = unstable_nw.from_native(df)
assert result is df # type: ignore[comparison-overlap]
assert result is df
s = df["a"]
result_s = unstable_nw.from_native(s, allow_series=True)
assert result_s is s
Expand Down Expand Up @@ -257,4 +257,4 @@ def __dataframe__(self) -> None: # pragma: no cover

mockdf = MockDf()
result = nw.from_native(mockdf, eager_only=True, strict=False)
assert result is mockdf # type: ignore[comparison-overlap]
assert result is mockdf

0 comments on commit ffa2abb

Please sign in to comment.