Skip to content

BUG: Series(nullable).tolist() -> numpy scalars #49890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ Sparse
ExtensionArray
^^^^^^^^^^^^^^
- Bug in :meth:`Series.mean` overflowing unnecessarily with nullable integers (:issue:`48378`)
- Bug in :meth:`Series.tolist` for nullable dtypes returning numpy scalars instead of python scalars (:issue:`49890`)
- Bug when concatenating an empty DataFrame with an ExtensionDtype to another DataFrame with the same ExtensionDtype, the resulting dtype turned into object (:issue:`48510`)
-

Expand Down
6 changes: 2 additions & 4 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,8 @@ def to_numpy(
def tolist(self):
if self.ndim > 1:
return [x.tolist() for x in self]
if not self._hasna:
# faster than list(self)
return list(self._data)
return list(self)
dtype = None if self._hasna else self._data.dtype
return self.to_numpy(dtype=dtype).tolist()

@overload
def astype(self, dtype: npt.DTypeLike, copy: bool = ...) -> np.ndarray:
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/series/methods/test_tolist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest

import pandas.util._test_decorators as td

from pandas import (
Interval,
Period,
Series,
Timedelta,
Timestamp,
)


@pytest.mark.parametrize(
"values, dtype, expected_dtype",
(
([1], "int64", int),
([1], "Int64", int),
([1.0], "float64", float),
([1.0], "Float64", float),
(["abc"], "object", str),
(["abc"], "string", str),
([Interval(1, 3)], "interval", Interval),
([Period("2000-01-01", "D")], "period[D]", Period),
([Timedelta(days=1)], "timedelta64[ns]", Timedelta),
([Timestamp("2000-01-01")], "datetime64[ns]", Timestamp),
pytest.param([1], "int64[pyarrow]", int, marks=td.skip_if_no("pyarrow")),
pytest.param([1.0], "float64[pyarrow]", float, marks=td.skip_if_no("pyarrow")),
pytest.param(["abc"], "string[pyarrow]", str, marks=td.skip_if_no("pyarrow")),
),
)
def test_tolist_scalar_dtype(values, dtype, expected_dtype):
# GH49890
ser = Series(values, dtype=dtype)
result_dtype = type(ser.tolist()[0])
assert result_dtype == expected_dtype