Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading