-
- Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: support downcasting of nullable EAs in pd.to_numeric #38746
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits Select commit Hold shift + click to select a range
a594847 tests
arw2019 a1bb9fc add NumericArray path in to_numeric
arw2019 dbba7b4 replace to_numpy use with _data
arw2019 ddb71cb review comment
arw2019 323cfdc more testcases
arw2019 7b4180e Merge branch 'master' of https://github.com/pandas-dev/pandas into EA…
arw2019 337589a whatsnew
arw2019 4e8761a review comments
arw2019 e2b4cbb cleanup
arw2019 23c4ae6 review comments (tests)
arw2019 e140b7a more tests
arw2019 f583a10 de-duplicate testcae
arw2019 a6cb152 more cleanup
arw2019 d707a61 review: code comments
arw2019 6b2c39f review: code comments
arw2019 fda4ba1 review: update docstring
arw2019 1a23118 review: reorder test parameters
arw2019 1015b07 review: add testcases
arw2019 0279be9 Merge branch 'master' of https://github.com/pandas-dev/pandas into EA…
arw2019 56747da Update pandas/core/tools/numeric.py
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
add NumericArray path in to_numeric
- Loading branch information
commit a1bb9fc065e0654b33cc6d9e520bae6c62d977d2
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| | @@ -7,6 +7,9 @@ | |
| ensure_object, | ||
| is_datetime_or_timedelta_dtype, | ||
| is_decimal, | ||
| is_extension_array_dtype, | ||
| is_float_dtype, | ||
| is_integer_dtype, | ||
| is_number, | ||
| is_numeric_dtype, | ||
| is_scalar, | ||
| | @@ -15,6 +18,8 @@ | |
| from pandas.core.dtypes.generic import ABCIndex, ABCSeries | ||
| | ||
| import pandas as pd | ||
| from pandas.core.arrays.numeric import NumericArray | ||
| from pandas.core.construction import extract_array | ||
| | ||
| | ||
| def to_numeric(arg, errors="raise", downcast=None): | ||
| | @@ -118,10 +123,14 @@ def to_numeric(arg, errors="raise", downcast=None): | |
| is_series = False | ||
| is_index = False | ||
| is_scalars = False | ||
| is_numeric_extension_dtype = False | ||
| | ||
| if isinstance(arg, ABCSeries): | ||
| is_series = True | ||
| values = arg.values | ||
| if is_extension_array_dtype(arg) and isinstance(values, NumericArray): | ||
| is_numeric_extension_dtype = True | ||
| values = extract_array(arg) | ||
| elif isinstance(arg, ABCIndex): | ||
| is_index = True | ||
| if needs_i8_conversion(arg.dtype): | ||
| | @@ -142,6 +151,14 @@ def to_numeric(arg, errors="raise", downcast=None): | |
| else: | ||
| values = arg | ||
| | ||
| if is_numeric_extension_dtype or ( | ||
| is_extension_array_dtype(arg) and isinstance(values, NumericArray) | ||
| ): | ||
| is_numeric_extension_dtype = True | ||
| mask = values._mask | ||
| values = values.to_numpy() | ||
| values[mask] = 0 | ||
| | ||
| values_dtype = getattr(values, "dtype", None) | ||
| if is_numeric_dtype(values_dtype): | ||
| pass | ||
| | @@ -188,6 +205,16 @@ def to_numeric(arg, errors="raise", downcast=None): | |
| if values.dtype == dtype: | ||
| break | ||
| | ||
| if is_numeric_extension_dtype: | ||
| ||
| if is_integer_dtype(values): | ||
| from pandas.core.arrays import IntegerArray | ||
| | ||
| values = IntegerArray(values, mask) | ||
| elif is_float_dtype(values): | ||
| from pandas.core.arrays import FloatingArray | ||
| | ||
| values = FloatingArray(values, mask) | ||
| | ||
| if is_series: | ||
| return arg._constructor(values, index=arg.index, name=arg.name) | ||
| elif is_index: | ||
| | ||
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.
Is this extract_array line needed? (the
values = arg.valuesfrom above should have worked fine, I think)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.
It's not, I reverted this bit