-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
ENH/BUG: implement __iter__ for IntegerArray so conversions (to_dict, tolist, etc.) return python native types #37377
Closed
Closed
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
07782bc
TST: add tests from OP
arw2019 1b164c3
ENH: implement __iter__ from IntegerArray
arw2019 142c81f
feedback: move __iter__ method to base class
arw2019 3357901
TST: parametrize Int tests on dtype
arw2019 e38934e
TST: add floating tests
arw2019 fa2166d
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 c32aafa
TST: add boolean tests
arw2019 2eb7219
TST: #346654
arw2019 db4154d
feedback: gather tests in separate file + use fixtures
arw2019 30d2f09
TST: remove tests from original locations
arw2019 26314f7
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 8d81ec9
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 d7fced7
TST: rewrite expected construction using pd.array
arw2019 7bdc0ac
TST: add comment
arw2019 9bf6b25
TST: skip float-string conversion, reason:M f-p precision
arw2019 ec837c0
TST/BUG: correct test rewrite
arw2019 c7db14a
TST: skip string conversion test due to fp-precision issues
arw2019 e70a7df
TST: DRY the code using data fixture
arw2019 ff1ede7
CLN: remove unused code
arw2019 1df019c
TST/BUG: implement jorisvandenbossche suggestion to fix astype(str) t…
arw2019 2a5df3e
TST: skip boolean combine_add test
arw2019 79582d4
Merge remote-tracking branch 'upstream/master' into GH29738
arw2019 1206096
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 bcf0896
skip str astype tests for Float32Dtype
arw2019 cde954b
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 d9f4809
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 225a260
docstring fix
arw2019 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
|
||
from pandas.core.dtypes.common import is_bool_dtype, is_float_dtype, is_integer_dtype | ||
|
||
import pandas as pd | ||
from pandas import isna | ||
|
||
from .base import BaseExtensionTests | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
lambda df: df.to_dict()["A"].values(), | ||
lambda df: [record["A"] for record in df.to_dict(orient="records")], | ||
] | ||
) | ||
def frame_conversion(request): | ||
return request.param | ||
|
||
|
||
class BaseReturnTypeTests(BaseExtensionTests): | ||
def get_native_dtype(self, dtype): | ||
if is_integer_dtype(dtype): | ||
return int | ||
elif is_float_dtype(dtype): | ||
return float | ||
elif is_bool_dtype(dtype): | ||
return bool | ||
else: | ||
raise ValueError("invalid dtype provided") | ||
|
||
@pytest.mark.parametrize( | ||
"func", | ||
[ | ||
lambda s: s.tolist(), | ||
lambda s: s.to_dict().values(), | ||
lambda s: [v for _, v in s.iteritems()], | ||
lambda s: list(iter(s)), | ||
], | ||
) | ||
def test_series(self, all_data, func): | ||
# GH 29738 | ||
s = pd.Series(all_data) | ||
native_dtype = self.get_native_dtype(all_data.dtype) | ||
|
||
assert all(isinstance(val, native_dtype) or isna(val) for val in func(s)) | ||
|
||
@pytest.mark.parametrize( | ||
"func", | ||
[ | ||
lambda df: df.to_dict()["A"].values(), | ||
lambda df: [record["A"] for record in df.to_dict(orient="records")], | ||
], | ||
) | ||
def test_frame(self, all_data, func): | ||
# GH 29738 | ||
s = pd.DataFrame({"A": all_data}) | ||
native_dtype = self.get_native_dtype(all_data.dtype) | ||
|
||
assert all(isinstance(val, native_dtype) or isna(val) for val in func(s)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
My previous comment was actually about moving them to a file in
tests/arrays/masked/
(so to share the test with the different nullable dtypes).Now, I am also fine to move it to the base extension tests as you did here, but then we should add this to all EAs. If it's only activated for nullable floating/integer/boolean as is done now, then it should move to
tests/arrays/masked/
, as it's not a general test for all EAs.Making it truly generic is probably a bit annoying (you need to know the exact expected type for the scalars), but so maybe the subclasses can override
get_native_dtype
where needed? Or maybe it can be an attribute on the test class (if it's always a single type for each dtype, then that's simpler)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.
I did misunderstand but this makes sense
This sounds like the way to go. I like not having to keep a registry of the mapping between scalars and native types in a single place