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
  • Loading branch information
MarcoGorelli committed Dec 3, 2024
1 parent e02685b commit 46c5caa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 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.__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)

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

0 comments on commit 46c5caa

Please sign in to comment.