Pandas convert types and set invalid values ​​as na

Is it possible to convert pandas series values ​​to a specific type and set those n / a items that cannot be converted?

I found Series.astype(dtype, copy=True, raise_on_error=True)

with and installed raise_on_error=True

to avoid exceptions, but this will not set invalid elements to na ...

Update

More precisely, I want to specify the type to which the column should be converted. For a series containing values [123, 'abc', '2010-01-01', 1.3]

and type conversions to float

, I would expect [123.0, nan, nan, 1.3]

as a result, if selected datetime

, only the value series[2]

will contain a valid datetime value. convert_objects

doesn't allow for such flexibility, IMHO.

+3


source to share


2 answers


I think you might be lucky with convert_objects

:

In [11]: s = pd.Series(['1', '2', 'a'])

In [12]: s.astype(int, raise_on_error=False)  # just returns s
Out[12]:
0    1
1    2
2    a
dtype: object

In [13]: s.convert_objects(convert_numeric=True)
Out[13]:
0     1
1     2
2   NaN
dtype: float64

      




Update: In more recent pandas, the method is convert_objects

deprecated.
In favor pd.to_numeric

:

In [21]: pd.to_numeric(s, errors='coerce')
Out[21]:
0    1.0
1    2.0
2    NaN
dtype: float64

      

It's not quite as powerful / magical as convert_objects

(which also worked on DataFrames), but works well in this case too. Read the section on converting objects in docs for other functions to_*

.

+4


source


s.astype(int, raise_on_error=False)
s = s.apply(lambda x: x if type(x)==int else np.nan)
s = s.dropna()

      



-1


source







All Articles