-
- 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
Changes from 7 commits
a594847 a1bb9fc dbba7b4 ddb71cb 323cfdc 7b4180e 337589a 4e8761a e2b4cbb 23c4ae6 e140b7a f583a10 a6cb152 d707a61 6b2c39f fda4ba1 1a23118 1015b07 0279be9 56747da 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 |
|---|---|---|
| | @@ -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,7 @@ | |
| from pandas.core.dtypes.generic import ABCIndex, ABCSeries | ||
| | ||
| import pandas as pd | ||
| from pandas.core.arrays.numeric import NumericArray | ||
| | ||
| | ||
| def to_numeric(arg, errors="raise", downcast=None): | ||
| | @@ -118,6 +122,7 @@ 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 | ||
| | @@ -142,6 +147,10 @@ def to_numeric(arg, errors="raise", downcast=None): | |
| else: | ||
| values = arg | ||
| | ||
| if is_extension_array_dtype(arg) and isinstance(values, NumericArray): | ||
| is_numeric_extension_dtype = True | ||
| mask, values = values._mask, values._data | ||
| ||
| | ||
| values_dtype = getattr(values, "dtype", None) | ||
| if is_numeric_dtype(values_dtype): | ||
| pass | ||
| | @@ -188,6 +197,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: | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -725,3 +725,28 @@ def test_to_numeric_from_nullable_string(values, expected): | |
| s = Series(values, dtype="string") | ||
| result = to_numeric(s) | ||
| tm.assert_series_equal(result, expected) | ||
| | ||
| | ||
| @pytest.mark.parametrize( | ||
| "data, input_dtype, downcast, expected_dtype", | ||
| ( | ||
| ([1, 1], "Int64", "integer", "Int8"), | ||
| ([1, pd.NA], "Int64", "integer", "Int8"), | ||
| ([450, 300], "Int64", "integer", "Int16"), | ||
| ([1, 1], "Int64", "signed", "Int8"), | ||
| ([1, pd.NA], "Int64", "signed", "Int8"), | ||
jreback marked this conversation as resolved. Show resolved Hide resolved | ||
| ([450, -300], "Int64", "signed", "Int16"), | ||
| ([1, 1], "Int64", "unsigned", "UInt8"), | ||
| ([1, pd.NA], "Int64", "unsigned", "UInt8"), | ||
| ([450, -300], "Int64", "unsigned", "Int64"), | ||
| ([-1, -1], "Int32", "unsigned", "Int32"), | ||
| ([1, 1], "Float64", "float", "Float32"), | ||
| ([1, 1], "Float64", "float", "Float32"), | ||
| ([1, 1], "Float64", "integer", "Int8"), | ||
arw2019 marked this conversation as resolved. Show resolved Hide resolved | ||
| ), | ||
| ) | ||
| def test_downcast_nullable_numeric(data, input_dtype, downcast, expected_dtype): | ||
| arr = pd.array(data, dtype=input_dtype) | ||
| result = pd.to_numeric(arr, downcast=downcast) | ||
| expected = pd.array(data, dtype=expected_dtype) | ||
| tm.assert_extension_array_equal(result, expected) | ||
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.
this really should be part of the above logic. also mask needs to be defined for all cases (can be default to None)
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.
The reason for having it down here is to handle EA dtype Series and array in a single place (and Index when #34159/#37869 go through)
done