Reputation: 4807
I have a dataframe with one column as follows:
df['mixed'].values
array([' GC', '345', '69', '28-'], dtype=object)
I tried the following:
df['mixed'].astype(float, errors='ignore')
But it doesn't do anything. I was expecting the following:
df['mixed'].values
array([' GC', 345, 69, '28-'], dtype=object)
Upvotes: 0
Views: 1280
Reputation: 28322
If there is an error during the conversion the original object will be returned. The documentation for astype
[emphasis mine]:
errors{‘raise’, ‘ignore’}, default ‘raise’ Control raising of exceptions on invalid data for provided dtype.
- raise : allow exceptions to be raised
- ignore : suppress exceptions. On error return original object.
To obtain the expected output, you can instead use to_numeric
and setting errors='coerce'
which will set any invalid rows to NaN
. These can then be set to the original value using fillna
(see the answer by BENY).
Upvotes: 1
Reputation: 323226
You can do pd.to_numeric
pd.to_numeric(df['mixed'],errors='coerce').fillna(df['mixed']).tolist()
[' GC', 345.0, 69.0, '28-']
Upvotes: 1