Skip to content

Commit c872a8b

Browse files
authored
BUG: Series(nullable).tolist() -> numpy scalars (#49890)
1 parent c3a257c commit c872a8b

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ Sparse
766766
ExtensionArray
767767
^^^^^^^^^^^^^^
768768
- Bug in :meth:`Series.mean` overflowing unnecessarily with nullable integers (:issue:`48378`)
769+
- Bug in :meth:`Series.tolist` for nullable dtypes returning numpy scalars instead of python scalars (:issue:`49890`)
769770
- Bug when concatenating an empty DataFrame with an ExtensionDtype to another DataFrame with the same ExtensionDtype, the resulting dtype turned into object (:issue:`48510`)
770771
-
771772

pandas/core/arrays/masked.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,8 @@ def to_numpy(
436436
def tolist(self):
437437
if self.ndim > 1:
438438
return [x.tolist() for x in self]
439-
if not self._hasna:
440-
# faster than list(self)
441-
return list(self._data)
442-
return list(self)
439+
dtype = None if self._hasna else self._data.dtype
440+
return self.to_numpy(dtype=dtype).tolist()
443441

444442
@overload
445443
def astype(self, dtype: npt.DTypeLike, copy: bool = ...) -> np.ndarray:
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
import pandas.util._test_decorators as td
4+
5+
from pandas import (
6+
Interval,
7+
Period,
8+
Series,
9+
Timedelta,
10+
Timestamp,
11+
)
12+
13+
14+
@pytest.mark.parametrize(
15+
"values, dtype, expected_dtype",
16+
(
17+
([1], "int64", int),
18+
([1], "Int64", int),
19+
([1.0], "float64", float),
20+
([1.0], "Float64", float),
21+
(["abc"], "object", str),
22+
(["abc"], "string", str),
23+
([Interval(1, 3)], "interval", Interval),
24+
([Period("2000-01-01", "D")], "period[D]", Period),
25+
([Timedelta(days=1)], "timedelta64[ns]", Timedelta),
26+
([Timestamp("2000-01-01")], "datetime64[ns]", Timestamp),
27+
pytest.param([1], "int64[pyarrow]", int, marks=td.skip_if_no("pyarrow")),
28+
pytest.param([1.0], "float64[pyarrow]", float, marks=td.skip_if_no("pyarrow")),
29+
pytest.param(["abc"], "string[pyarrow]", str, marks=td.skip_if_no("pyarrow")),
30+
),
31+
)
32+
def test_tolist_scalar_dtype(values, dtype, expected_dtype):
33+
# GH49890
34+
ser = Series(values, dtype=dtype)
35+
result_dtype = type(ser.tolist()[0])
36+
assert result_dtype == expected_dtype

0 commit comments

Comments
 (0)