替代已弃用的pd.Series()。convert_objects(convert_numeric = true),能够解析字符串

时间:2017-05-25 13:39:21

标签: python-3.x pandas numpy series

我想知道以下情况是否存在pandas.Series(x).convert_objects(convert_numeric = true)的替代方案:

px = dt.ix[:,1] # pandas.core.series.Series
pmat = pd.Series(px).convert_objects(convert_numeric = True) # works but convert_objects is deprecated in future version

如果有关数据的进一步信息可能有所帮助,请注意:

dt:Type = DataFrame, size=(1790,2), Value= column names: 0,1
px: Type=Series, size=(1790,), Value= class 'pandas.core.series.Series'

到目前为止,我已尝试过一些搜索方式:

pmat = px.apply(pd.to_numeric, errors="ignore")
pmat = pd.to_numeric(px) # Unable to parse string

最佳,

1 个答案:

答案 0 :(得分:1)

我认为您需要使用to_numeric Seriesdf列):

#invalid parsing will be set as NaN
pmat = pd.to_numeric(px, errors='coerce')

或者:

#invalid parsing will return the input
pmat = pd.to_numeric(px, errors='ignore')

同样ix deprecated,最好使用iloc