Skip to content

Commit

Permalink
test columns with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Jan 16, 2025
1 parent 5880e60 commit f1feb93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 8 additions & 2 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,13 @@ def drop_nulls(self: Self, subset: str | list[str] | None) -> Self:
import duckdb

rel = self._native_frame
subset_ = subset if subset is not None else rel.columns
keep_condition = " and ".join(f"{col} is not null" for col in subset_)
subset_ = (
[subset]
if isinstance(subset, str)
else rel.columns
if subset is None
else subset
)
keep_condition = " and ".join(f'"{col}" is not null' for col in subset_)
query = f"""select * from rel where {keep_condition}""" # noqa: S608
return self._from_native_frame(duckdb.sql(query))
14 changes: 7 additions & 7 deletions tests/frame/drop_nulls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
from tests.utils import assert_equal_data

data = {
"a": [1.0, 2.0, None, 4.0],
"b": [None, 3.0, None, 5.0],
"alpha": [1.0, 2.0, None, 4.0],
"beta gamma": [None, 3.0, None, 5.0],
}


def test_drop_nulls(constructor: Constructor) -> None:
result = nw.from_native(constructor(data)).drop_nulls()
expected = {
"a": [2.0, 4.0],
"b": [3.0, 5.0],
"alpha": [2.0, 4.0],
"beta gamma": [3.0, 5.0],
}
assert_equal_data(result, expected)


@pytest.mark.parametrize(
("subset", "expected"),
[
("a", {"a": [1, 2.0, 4.0], "b": [None, 3.0, 5.0]}),
(["a"], {"a": [1, 2.0, 4.0], "b": [None, 3.0, 5.0]}),
(["a", "b"], {"a": [2.0, 4.0], "b": [3.0, 5.0]}),
("alpha", {"alpha": [1, 2.0, 4.0], "beta gamma": [None, 3.0, 5.0]}),
(["alpha"], {"alpha": [1, 2.0, 4.0], "beta gamma": [None, 3.0, 5.0]}),
(["alpha", "beta gamma"], {"alpha": [2.0, 4.0], "beta gamma": [3.0, 5.0]}),
],
)
def test_drop_nulls_subset(
Expand Down

0 comments on commit f1feb93

Please sign in to comment.