Skip to content

ENH: EA.tolist #43920

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 5 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,18 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
# ------------------------------------------------------------------------
# Non-Optimized Default Methods

def tolist(self) -> list:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this public? (nbd, but list in the top section)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update

"""
Return a list of the values.

These are each a scalar type, which is a Python scalar
(for str, int, float) or a pandas scalar
(for Timestamp/Timedelta/Interval/Period)
"""
if self.ndim > 1:
return [x.tolist() for x in self]
return list(self)

def delete(self: ExtensionArrayT, loc: PositionalIndexer) -> ExtensionArrayT:
indexer = np.delete(np.arange(len(self)), loc)
return self.take(indexer)
Expand Down
13 changes: 3 additions & 10 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
Ordered,
PositionalIndexer2D,
PositionalIndexerTuple,
Scalar,
ScalarIndexer,
SequenceIndexer,
Shape,
Expand Down Expand Up @@ -566,17 +565,11 @@ def itemsize(self) -> int:
"""
return self.categories.itemsize

def tolist(self) -> list[Scalar]:
def to_list(self):
"""
Return a list of the values.

These are each a scalar type, which is a Python scalar
(for str, int, float) or a pandas scalar
(for Timestamp/Timedelta/Interval/Period)
Alias for tolist.
"""
return list(self)

to_list = tolist
return self.tolist()

@classmethod
def _from_inferred_categories(
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,6 @@ def tolist(self):
numpy.ndarray.tolist : Return the array as an a.ndim-levels deep
nested list of Python scalars.
"""
if not isinstance(self._values, np.ndarray):
# check for ndarray instead of dtype to catch DTA/TDA
return list(self._values)
return self._values.tolist()

to_list = tolist
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/extension/base/dim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ def test_iter_2d(self, data):
assert obj.ndim == 1
assert len(obj) == arr2d.shape[1]

def test_tolist_2d(self, data):
arr2d = data.reshape(1, -1)

result = arr2d.tolist()
expected = [data.tolist()]

assert isinstance(result, list)
assert all(isinstance(x, list) for x in result)

assert result == expected

def test_concat_2d(self, data):
left = data.reshape(-1, 1)
right = left.copy()
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,9 @@ def test_view(self, data):

# check specifically that the `dtype` kwarg is accepted
data.view(dtype=None)

def test_tolist(self, data):
result = data.tolist()
expected = list(data)
assert isinstance(result, list)
assert result == expected