Skip to content

Ensure conversion to "native" types for integer EA #31328

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

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ Sparse
ExtensionArray
^^^^^^^^^^^^^^

-
- integer EA will now return objects of native type only when iterated. (:issue:`29738`)
-


Expand Down
7 changes: 7 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
)
super().__init__(values, mask, copy=copy)

def __iter__(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could actually move this to base masked and/or the base EA interface (maybe)

for i in range(len(self)):
if self._mask[i]:
yield self.dtype.na_value
else:
yield self._data[i].item()

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "IntegerArray":
return integer_array(scalars, dtype=dtype, copy=copy)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,24 @@ def test_integer_array_constructor_copy():
assert result._mask is not mask


@pytest.mark.parametrize(
"data",
[
pd.Series([1, 2], dtype="int64").tolist()[0],
pd.Series([1, 2], dtype="Int64").tolist()[0],
pd.Series([1, 2], dtype="int64").to_dict()[0],
pd.Series([1, 2], dtype="Int64").to_dict()[0],
list(pd.Series([1, 2], dtype="int64").iteritems())[0][1],
list(pd.Series([1, 2], dtype="Int64").iteritems())[0][1],
list(iter(pd.Series([1, 2], dtype="int64")))[0],
list(iter(pd.Series([1, 2], dtype="Int64")))[0],
],
)
def test_integer_Series_iter_return_native(data):
# GH 29738
assert isinstance(data, int)


@pytest.mark.parametrize(
"values",
[
Expand Down