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

Alternative to deprecated pd.Series().convert_objects(convert_numeric = true), able to parse string

我想知道以下情况是否存在 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

最佳,

我认为您需要 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')

还有ixdeprecated, better is use iloc.