Skip to content

Commit

Permalink
python: series.to_pandas (#2835)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 6, 2022
1 parent cdcc9a4 commit 5d342a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Conversion
Series.to_list
Series.to_numpy
Series.to_arrow
Series.to_numpy
Series.to_pandas


Aggregation
Expand Down
10 changes: 10 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,16 @@ def to_arrow(self) -> "pa.Array":
"""
return self._s.to_arrow()

def to_pandas(self) -> "pd.Series":
"""
Convert this Series to a pandas Series
"""
if not _PYARROW_AVAILABLE:
raise ImportError( # pragma: no cover
"'pyarrow' is required for converting a 'polars' Series to a 'pandas' Series."
)
return self.to_arrow().to_pandas()

def set(self, filter: "Series", value: Union[int, float]) -> "Series":
"""
Set masked values.
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,7 @@ def test_from_empty_arrow() -> None:

def test_from_null_column() -> None:
assert pl.from_pandas(pd.DataFrame(data=[pd.NA, pd.NA])).shape == (2, 1)


def test_to_pandas_series() -> None:
assert (pl.Series("a", [1, 2, 3]).to_pandas() == pd.Series([1, 2, 3])).all()

0 comments on commit 5d342a6

Please sign in to comment.