-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 8 commits
07782bc
1b164c3
142c81f
3357901
e38934e
fa2166d
c32aafa
2eb7219
db4154d
30d2f09
26314f7
8d81ec9
d7fced7
7bdc0ac
9bf6b25
ec837c0
c7db14a
e70a7df
ff1ede7
1df019c
2a5df3e
79582d4
1206096
bcf0896
cde954b
d9f4809
225a260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use the data fixture There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)