Skip to content

Commit

Permalink
feat: add Series.is_empty (#385)
Browse files Browse the repository at this point in the history
* feat: add Series.is_empty

* update
  • Loading branch information
MarcoGorelli authored Jul 2, 2024
1 parent 39f94a6 commit 6a1a6f3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
3 changes: 3 additions & 0 deletions narwhals/_arrow/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def all(self) -> bool:
pc = get_pyarrow_compute()
return pc.all(self._series) # type: ignore[no-any-return]

def is_empty(self) -> bool:
return len(self) == 0

@property
def shape(self) -> tuple[int]:
return (len(self._series),)
Expand Down
11 changes: 11 additions & 0 deletions tests/series/is_empty_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import Any

import narwhals as nw


def test_is_empty(constructor_series_with_pyarrow: Any) -> None:
series = nw.from_native(constructor_series_with_pyarrow([1, 2, 3]), series_only=True)
assert not series.is_empty()
assert not series[:1].is_empty()
assert len(series[:1]) == 1
assert series[:0].is_empty()
8 changes: 0 additions & 8 deletions tests/series/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,6 @@ def test_is_duplicated(df_raw: Any) -> None:
assert (result.to_numpy() == expected).all()


@pytest.mark.parametrize("df_raw", [df_pandas, df_polars])
@pytest.mark.parametrize(("threshold", "expected"), [(0, False), (10, True)])
def test_is_empty(df_raw: Any, threshold: Any, expected: Any) -> None:
series = nw.from_native(df_raw["b"], series_only=True)
result = series.filter(series > threshold).is_empty()
assert result == expected


@pytest.mark.parametrize("df_raw", [df_pandas, df_polars])
def test_is_unique(df_raw: Any) -> None:
series = nw.from_native(df_raw["b"], series_only=True)
Expand Down
18 changes: 13 additions & 5 deletions utils/check_backend_completeness.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
"DataFrame.with_columns",
"DataFrame.with_row_index",
"DataFrame.write_parquet",
"Series.all",
"Series.any",
"Series.cast",
"Series.cat",
"Series.diff",
"Series.drop_nulls",
"Series.fill_null",
Expand All @@ -49,7 +46,6 @@
"Series.head",
"Series.is_between",
"Series.is_duplicated",
"Series.is_empty",
"Series.is_first_distinct",
"Series.is_in",
"Series.is_last_distinct",
Expand Down Expand Up @@ -130,6 +126,18 @@ def name(self):
missing.extend([x for x in pd_methods if x not in pa_methods and x not in MISSING])

if missing:
print(sorted(missing))
print(
"The following have not been implemented for the Arrow backend: ",
sorted(missing),
)
sys.exit(1)

no_longer_missing = [x for x in MISSING if x in pa_methods and x in pd_methods]
if no_longer_missing:
print(
"Please remove the following from MISSING in utils/check_backend_completeness.py: ",
sorted(no_longer_missing),
)
sys.exit(1)

sys.exit(0)

0 comments on commit 6a1a6f3

Please sign in to comment.