Skip to content

ENH/BUG: implement __iter__ for IntegerArray so conversions (to_dict, tolist, etc.) return python native types #37377

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 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
07782bc
TST: add tests from OP
arw2019 Oct 24, 2020
1b164c3
ENH: implement __iter__ from IntegerArray
arw2019 Oct 24, 2020
142c81f
feedback: move __iter__ method to base class
arw2019 Oct 24, 2020
3357901
TST: parametrize Int tests on dtype
arw2019 Oct 24, 2020
e38934e
TST: add floating tests
arw2019 Oct 24, 2020
fa2166d
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 Oct 24, 2020
c32aafa
TST: add boolean tests
arw2019 Oct 24, 2020
2eb7219
TST: #346654
arw2019 Oct 24, 2020
db4154d
feedback: gather tests in separate file + use fixtures
arw2019 Oct 30, 2020
30d2f09
TST: remove tests from original locations
arw2019 Oct 30, 2020
26314f7
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 Oct 30, 2020
8d81ec9
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 Nov 1, 2020
d7fced7
TST: rewrite expected construction using pd.array
arw2019 Nov 1, 2020
7bdc0ac
TST: add comment
arw2019 Nov 1, 2020
9bf6b25
TST: skip float-string conversion, reason:M f-p precision
arw2019 Nov 1, 2020
ec837c0
TST/BUG: correct test rewrite
arw2019 Nov 1, 2020
c7db14a
TST: skip string conversion test due to fp-precision issues
arw2019 Nov 1, 2020
e70a7df
TST: DRY the code using data fixture
arw2019 Nov 1, 2020
ff1ede7
CLN: remove unused code
arw2019 Nov 1, 2020
1df019c
TST/BUG: implement jorisvandenbossche suggestion to fix astype(str) t…
arw2019 Nov 3, 2020
2a5df3e
TST: skip boolean combine_add test
arw2019 Nov 3, 2020
79582d4
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 Nov 5, 2020
1206096
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 Nov 30, 2020
bcf0896
skip str astype tests for Float32Dtype
arw2019 Nov 30, 2020
cde954b
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 Dec 31, 2020
d9f4809
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 Jan 31, 2021
225a260
docstring fix
arw2019 Jan 31, 2021
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 @@ -357,6 +357,13 @@ def __pos__(self):
def __abs__(self):
return type(self)(np.abs(self._data), self._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.

this actually be in our base class so that bool and float will work as well here

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

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
15 changes: 15 additions & 0 deletions pandas/tests/arrays/integer/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,18 @@ def test_astype_boolean():
result = a.astype("boolean")
expected = pd.array([True, False, True, True, None], dtype="boolean")
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize(
"func",
[
lambda s: s.tolist()[0],
lambda s: s.to_dict()[0],
lambda s: list(s.iteritems())[0][1],
lambda s: list(iter(s))[0],
],
)
def test_conversion_methods_return_type_is_native(func):
Copy link
Contributor

Choose a reason for hiding this comment

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

paramterize per all Int and UInt types

also add tests for float / bool

may need o move this to the base class

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the tests. Atm they're in:

pandas/tests/arrays/boolean/test_astype.py
pandas/tests/arrays/floating/test_astype.py
pandas/tests/arrays/integer/test_dtypes.py

Do you want them moved?

# GH 29738
s = pd.Series([1, 2], dtype="Int64")
assert isinstance(func(s), int)