'Odd behaviour from pandas str.isnumeric() [duplicate]

s1 = pd.Series([float(1), 'one1', 212, '123.3'])

s1.astype(str)

This outputs

0      1.0
1     one1
2      212
3    123.3
dtype: object

And this:

s1.astype(str).str.isnumeric()

outputs this:

0    False
1    False
2     True
3    False
dtype: bool

I would expect all, except 'one1' to give True... why do we have this result?



Solution 1:[1]

It is expected function Series.str.isnumeric return:

Check whether all characters in each string are numeric.

it means . is not numeric.

Possible solution is create missing values to non numeric values by to_numeric with erros='coerce' and test non missing values:

pd.to_numeric(s1, erros='coerce').notna()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jezrael