Skip to content

Commit

Permalink
Python: categorical to list
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 8, 2021
1 parent 82f2ae5 commit e124a9d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
10 changes: 7 additions & 3 deletions py-polars/polars/eager/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1681,10 +1681,15 @@ def cast(self, dtype: Type[DataType], strict: bool = True) -> "Series":
dtype = Float64
return wrap_s(self._s.cast(str(dtype), strict))

def to_list(self) -> tp.List[Optional[Any]]:
def to_list(self, use_pyarrow: bool = False) -> tp.List[Optional[Any]]:
"""
Convert this Series to a Python List. This operation clones data.
Parameters
----------
use_pyarrow
Use pyarrow for the conversion.
Examples
--------
>>> s = pl.Series("a", [1, 2, 3])
Expand All @@ -1694,8 +1699,7 @@ def to_list(self) -> tp.List[Optional[Any]]:
<class 'list'>
"""
# maybe we should not use pyarrow at all.
if (self.dtype != Object) and _PYARROW_AVAILABLE:
if use_pyarrow:
return self.to_arrow().to_pylist()
return self._s.to_list()

Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ impl PySeries {
};

let pylist = match series.dtype() {
DataType::Categorical => PyList::new(python, series.categorical().unwrap()),
DataType::Categorical => PyList::new(python, series.categorical().unwrap().iter_str()),
DataType::Object(_) => {
let v = PyList::empty(python);
for i in 0..series.len() {
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ def test_from_pandas_nested_list():
pldf = pl.from_pandas(pddf)
print(pldf)
assert pldf.shape == (4, 2)


def test_from_pandas_categorical_none():
s = pd.Series(["a", "b", "c", pd.NA], dtype="category")
out = pl.from_pandas(s)
assert out.dtype == pl.Categorical
assert out.to_list() == ["a", "b", "c", None]
2 changes: 1 addition & 1 deletion py-polars/tests/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_sort_dates_multiples():
"2021-01-02 00:00:00",
"2021-01-03 00:00:00",
],
).str.strptime(pl.datatypes.Datetime, "%Y-%m-%d %T"),
).str.strptime(pl.Datetime, "%Y-%m-%d %T"),
pl.Series("values", [5, 4, 3, 2, 1]),
]
)
Expand Down

0 comments on commit e124a9d

Please sign in to comment.