Skip to content
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
add NumericArray path in to_numeric
  • Loading branch information
arw2019 committed Dec 28, 2020
commit a1bb9fc065e0654b33cc6d9e520bae6c62d977d2
27 changes: 27 additions & 0 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Copy link
Member

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.values from above should have worked fine, I think)

Copy link
Contributor Author

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

elif isinstance(arg, ABCIndex):
is_index = True
if needs_i8_conversion(arg.dtype):
Expand All @@ -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
Expand Down Expand Up @@ -188,6 +205,16 @@ def to_numeric(arg, errors="raise", downcast=None):
if values.dtype == dtype:
break

if is_numeric_extension_dtype:
Copy link
Contributor

Choose a reason for hiding this comment

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

L194 might need to handle the mask

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean?

 float_32_ind = typecodes.index(float_32_char) 

I feel like there's a testcase I haven't looked at if yes (the ones I have work as is)

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:
Expand Down