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 8 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
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __iter__(self):
if self._mask[i]:
yield self.dtype.na_value
else:
yield self._data[i]
yield self._data[i].item()

def __len__(self) -> int:
return len(self._data)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/arrays/boolean/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ def test_astype_to_integer_array():
result = arr.astype("Int64")
expected = pd.array([1, 0, None], dtype="Int64")
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
Member

Choose a reason for hiding this comment

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

We could maybe move this test to the common arrays/masked/ section? We should be able to know the expected python class for each of the data types

Copy link
Member Author

Choose a reason for hiding this comment

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

For sure. I'll make a separate file there (call it test_conversions.py maybe)

# GH 29738
s = pd.Series([True, False], dtype="boolean")
assert isinstance(func(s), bool)
16 changes: 16 additions & 0 deletions pandas/tests/arrays/floating/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,19 @@ def test_astype_object(dtype):
# check exact element types
assert isinstance(result[0], float)
assert result[1] is pd.NA


@pytest.mark.parametrize(
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah can you move this to the base class to avoid this duplication

Copy link
Contributor

Choose a reason for hiding this comment

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

you can use the data fixture

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved + rewritten with fixtures (though not 100% sure that what I did is what you asked)

"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(float_ea_dtype, func):
# GH 29738
dtype = float_ea_dtype
s = pd.Series([1, 2], dtype=dtype)
assert isinstance(func(s), float)
29 changes: 29 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,32 @@ 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(any_nullable_int_dtype, func):
# GH 29738
dtype = any_nullable_int_dtype
s = pd.Series([1, 2], dtype=dtype)
assert isinstance(func(s), int)


def test_conversion_to_dict_oriented_record_returns_native(any_nullable_int_dtype):
Copy link
Contributor

Choose a reason for hiding this comment

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

see my comments about about moving this then these tests are much simpler

# GH 34665

df = pd.DataFrame({"A": [1, None]})
df["A"] = df["A"].astype("Int64")
records_as_dicts = df.to_dict(orient="records")
expected = [{"A": 1}, {"A": pd.NA}]

assert records_as_dicts == expected
assert type(records_as_dicts[0]["A"]) is int
assert type(records_as_dicts[1]["A"]) is pd._libs.missing.NAType