pandas 熊猫“.convert_objects(convert_numeric=True)”已弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33126477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas ".convert_objects(convert_numeric=True)" deprecated
提问by guidebortoli
I have this line in my code which converts my data to numeric...
我的代码中有这一行将我的数据转换为数字...
data["S1Q2I"] = data["S1Q2I"].convert_objects(convert_numeric=True)
The thing is that now the new pandas release (0.17.0) said that this function is deprecated.. This is the error:
问题是现在新的 Pandas 版本(0.17.0)说这个功能已被弃用。这是错误:
FutureWarning: convert_objects is deprecated.
Use the data-type specific converters pd.to_datetime,
pd.to_timedelta and pd.to_numeric.
data["S3BD5Q2A"] = data["S3BD5Q2A"].convert_objects(convert_numeric=True)
So, I went to the new documentation and I couldn't find any examples of how to use the new function to convert my data...
所以,我去了新的文档,我找不到任何关于如何使用新函数来转换我的数据的例子......
It only says this:
它只说:
"DataFrame.convert_objects has been deprecated in favor of type-specific functions pd.to_datetime, pd.to_timestamp and pd.to_numeric (new in 0.17.0) (GH11133)."
“DataFrame.convert_objects 已被弃用,取而代之的是特定于类型的函数 pd.to_datetime、pd.to_timestamp 和 pd.to_numeric(0.17.0 中的新功能)(GH11133)。”
Any help would be nice!
你能帮忙的话,我会很高兴!
回答by Hooked
回答by abalter
You can effect a replacement using apply as done here. An example would be:
您可以使用影响作为应用做了更换这里。一个例子是:
>>> import pandas as pd
>>> a = pd.DataFrame([{"letter":"a", "number":"1"},{"letter":"b", "number":"2"}])
>>> a.dtypes
letter object
number object
dtype: object
>>> b = a.apply(pd.to_numeric, errors="ignore")
>>> b.dtypes
letter object
number int64
dtype: object
>>>
But it sucks in two ways:
但它在两个方面很糟糕:
- You have to use apply rather than a non-native dataframe method
- You have to copy to another dataframe--can't be done in place. So much for use with "big data."
- 您必须使用 apply 而不是非本机数据框方法
- 您必须复制到另一个数据帧——不能就地完成。这么多用于“大数据”。
I'm not really loving the direction pandas is going. I haven't used R data.table much, but so far it seems superior.
我并不是很喜欢Pandas的发展方向。我没有经常使用 R data.table,但到目前为止它似乎更胜一筹。
I think a data table with native, in-place type conversion is pretty basic for a competitive data analysis framework.
我认为具有本地、就地类型转换的数据表对于竞争性数据分析框架来说是非常基础的。
回答by Samer
It depends on which version of Pandas...... if you have Pandas's version 0.18.0 this type will work ........
这取决于 Pandas 的版本......如果你有 Pandas 的 0.18.0 版本,这种类型将工作......
df['col name'] = df['col name'].apply(pd.to_numeric, errors='coerce')
another versions ........
另一个版本........
df['col name']=df.col name .astype(float)
回答by user8866085
If you convert all columns to numeric at once, this code may work.
如果您一次将所有列转换为数字,则此代码可能有效。
data = data.apply(pd.to_numeric, axis=0)
回答by magsmanston
You can get it to apply correctly to a particular variable name in a dataframe without having to copy into a different dataframe like this:
您可以将其正确应用于数据帧中的特定变量名称,而无需像这样复制到不同的数据帧中:
>>> import pandas as pd
>>> a = pd.DataFrame([{"letter":"a", "number":"1"},{"letter":"b", "number":"2"}])
>>> a.dtypes
letter object
number object
dtype: object
>>> a['number'] = a['number'].apply(pd.to_numeric, errors='coerce')
>>> a.dtypes
letter object
number int64
dtype: object
An example based on the original question above would be something like:
基于上述原始问题的示例类似于:
data['S1Q2I'] = data['S1Q2I'].apply(pd.to_numeric, errors='coerce')
This works the same way as your original:
这与您的原始工作方式相同:
data['S1Q2I'] = data['S1Q2I'].convert_objects(convert_numeric=True)
in my hands, anyway....
反正在我手里……
This doesn't address the point abalter made about inferring datatypes which is a little above my head I'm afraid!
这并没有解决 abalter 关于推断数据类型的观点,恐怕这有点超出我的想法!

