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 3 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
7 changes: 7 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ def __init__(self, values, mask, copy=False):
self._data = values
self._mask = mask

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=False):
return integer_array(scalars, dtype=dtype, copy=copy)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,26 @@ def test_integer_array_constructor_copy():
assert result._mask is not mask


def test_integer_Series_iter_return_native():
expected = int
result = type(pd.Series([1, 2], dtype="int64").tolist()[0])
assert expected == result
Copy link
Member

Choose a reason for hiding this comment

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

I think it's generally preferred to use isinstance, see https://www.python.org/dev/peps/pep-0008/

Object type comparisons should always use isinstance() instead of comparing types directly.

Yes: if isinstance(obj, int):

No: if type(obj) is type(1):

Copy link
Contributor Author

@rushabh-v rushabh-v Jan 26, 2020

Choose a reason for hiding this comment

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

But that would become isinstance(int, int) in this case, which returns False.
Are you asking to do it some other way?

Copy link
Member

Choose a reason for hiding this comment

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

What I meant was

isinstance(pd.Series([1, 2], dtype="int64").tolist()[0], int)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, okay. I will commit that soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have done that. Can you review it, please?

result = type(pd.Series([1, 2], dtype="Int64").tolist()[0])
assert expected == result
result = type(pd.Series([1, 2], dtype="int64").to_dict()[0])
assert expected == result
result = type(pd.Series([1, 2], dtype="Int64").to_dict()[0])
assert expected == result
result = type(list(pd.Series([1, 2], dtype="int64").iteritems())[0][1])
assert expected == result
result = type(list(pd.Series([1, 2], dtype="Int64").iteritems())[0][1])
assert expected == result
result = type(list(iter(pd.Series([1, 2], dtype="int64")))[0])
assert expected == result
result = type(list(iter(pd.Series([1, 2], dtype="Int64")))[0])
assert expected == result


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