Repository: narwhals-dev/narwhals
Pull request #345 (2024-06-28 09:47:01)

feat: add Series.to_list
<!--
# 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/docs/api-reference/series.md b/docs/api-reference/series.md
index f1d2ddfc..2687eead 100644
--- a/docs/api-reference/series.md
+++ b/docs/api-reference/series.md
@@ -42,6 +42,7 @@
         - sum
         - tail
         - to_frame
+        - to_list
         - to_numpy
         - to_pandas
         - unique
diff --git a/narwhals/_pandas_like/series.py b/narwhals/_pandas_like/series.py
index 09d9ea56..1ea029d0 100644
--- a/narwhals/_pandas_like/series.py
+++ b/narwhals/_pandas_like/series.py
@@ -180,6 +180,9 @@ class PandasSeries:
             self._series.to_frame(), implementation=self._implementation
         )
 
+    def to_list(self) -> Any:
+        return self._series.to_list()
+
     def is_between(
         self, lower_bound: Any, upper_bound: Any, closed: str = "both"
     ) -> PandasSeries:
diff --git a/narwhals/series.py b/narwhals/series.py
index 8e68ee2d..b892053b 100644
--- a/narwhals/series.py
+++ b/narwhals/series.py
@@ -301,6 +301,33 @@ class Series:
 
         return DataFrame(self._series.to_frame())
 
+    def to_list(self) -> list[Any]:
+        """
+        Convert to list.
+
+        Examples:
+            >>> import pandas as pd
+            >>> import polars as pl
+            >>> import narwhals as nw
+            >>> s = [1, 2, 3]
+            >>> s_pd = pd.Series(s, name="a")
+            >>> s_pl = pl.Series("a", s)
+
+            We define a library agnostic function:
+
+            >>> @nw.narwhalify
+            ... def func(s_any):
+            ...     return s_any.to_list()
+
+            We can then pass either pandas or Polars to `func`:
+
+            >>> func(s_pd)
+            [1, 2, 3]
+            >>> func(s_pl)
+            [1, 2, 3]
+        """
+        return self._series.to_list()  # type: ignore[no-any-return]
+
     def mean(self) -> Any:
         """
         Reduce this Series to the mean value.
diff --git a/utils/check_api_reference.py b/utils/check_api_reference.py
index 44d81ffb..b40b55ad 100644
--- a/utils/check_api_reference.py
+++ b/utils/check_api_reference.py
@@ -145,6 +145,7 @@ if (
     .difference(
         {
             "to_pandas",
+            "to_list",
             "to_numpy",
             "dtype",
             "name",


Test patch:
diff --git a/tests/series/to_list_test.py b/tests/series/to_list_test.py
new file mode 100644
index 00000000..c17b4f14
--- /dev/null
+++ b/tests/series/to_list_test.py
@@ -0,0 +1,15 @@
+from typing import Any
+
+import pandas as pd
+import polars as pl
+import pytest
+
+import narwhals as nw
+
+data = [1, 2, 3]
+
+
+@pytest.mark.parametrize("constructor", [pd.Series, pl.Series])
+def test_to_list(constructor: Any) -> None:
+    s = nw.from_native(constructor(data), series_only=True)
+    assert s.to_list() == [1, 2, 3]