I'm trying to convert pandas Series into numeric series with pd.to_numeric. When a value can't be converted into a numeric value I'm expecting to have a NaN instead, so I use errors="coerce".
I tried many weird inputs to make sure that users of my package will always have a numeric series whatever the series they provide. But I run into an issue when one value is a numpy array with one element.
>>> import pandas as pd
>>> import numpy as np
>>> s = pd.Series([3, np.array(2.1)])
>>> pd.to_numeric(s, errors="coerce")
0 3
1 2.1
dtype: object
I know this is a strange kind of input, but I was expecting the dtype to become float instead of object (whatever if np.array(2.1) becomes 2.1 or NaN, as long as the series is numeric). Yet, here, the series dtype stays as object.
Note that it works correctly when the value can be converted into an int:
>>> s = pd.Series([3, np.array(2.)])
>>> pd.to_numeric(s, errors="coerce")
0 3
1 2
dtype: int64
And it also works as expected when there is no np.array involved:
>>> s = pd.Series([3., 2.2])
>>> pd.to_numeric(s, errors="coerce")
0 3.0
1 2.2
dtype: float64
I'm on python 3.9.7, pandas 1.3.4, numpy 1.21.4.