Repository: narwhals-dev/narwhals
Pull request #385 (2024-07-02 17:37:45)

feat: add Series.is_empty
<!--
# Thanks for contributing a pull request! 
## Please make sure you see our contribution guidelines: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md
-->

## What type of PR is this? (check all applicable)

- [ ] 💾 Refactor
- [ ] ✨ Feature
- [ ] 🐛 Bug Fix
- [ ] 🔧 Optimization
- [ ] 📝 Documentation
- [ ] ✅ Test
- [ ] 🐳 Other

## Related issues 

- Related issue # 
- Closes #

## Checklist

- [ ] Code follows style guide (ruff)
- [ ] Tests added 
- [ ] Documented the changes

## If you have comments or can explain your changes, please do so below.




Patch:
diff --git a/narwhals/_arrow/series.py b/narwhals/_arrow/series.py
index 84725f98..dfb8a936 100644
--- a/narwhals/_arrow/series.py
+++ b/narwhals/_arrow/series.py
@@ -82,6 +82,9 @@ class ArrowSeries:
         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),)
diff --git a/utils/check_backend_completeness.py b/utils/check_backend_completeness.py
index a5352dbf..d5481cb5 100644
--- a/utils/check_backend_completeness.py
+++ b/utils/check_backend_completeness.py
@@ -37,10 +37,7 @@ MISSING = [
     "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",
@@ -49,7 +46,6 @@ MISSING = [
     "Series.head",
     "Series.is_between",
     "Series.is_duplicated",
-    "Series.is_empty",
     "Series.is_first_distinct",
     "Series.is_in",
     "Series.is_last_distinct",
@@ -130,6 +126,18 @@ if __name__ == "__main__":
     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)


Test patch:
diff --git a/tests/series/is_empty_test.py b/tests/series/is_empty_test.py
new file mode 100644
index 00000000..791dc732
--- /dev/null
+++ b/tests/series/is_empty_test.py
@@ -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()
diff --git a/tests/series/test_common.py b/tests/series/test_common.py
index 38fb798f..3d6f1f14 100644
--- a/tests/series/test_common.py
+++ b/tests/series/test_common.py
@@ -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)