简体   繁体   English

使用df.astype的熊猫错误

[英]pandas error using df.astype

I'm just trying to convert a column of numeric strings to ints. 我只是想将一列数字字符串转换为整数。 This is what I'm trying: 这是我正在尝试的:

df.date = df.date.astype(np.int64)

But I'm getting the warning: 但是我得到警告:

/Users/austin/anaconda/lib/python3.5/site-packages/pandas/core/generic.py:2773: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. /Users/austin/anaconda/lib/python3.5/site-packages/pandas/core/generic.py:2773:SettingWithCopyWarning:试图在DataFrame的切片副本上设置一个值。 Try using .loc[row_indexer,col_indexer] = value instead 尝试改用.loc [row_indexer,col_indexer] = value

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self[name] = value 请参阅文档中的警告: http : //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self [name] = value

Not sure what this means. 不知道这意味着什么。 I also tried: 我也尝试过:

df.date = df.date.apply(int)

And I get the same warning as above. 而且我收到与上述相同的警告。

Why doesn't this work and what's the proper way? 为什么这不起作用,正确的方法是什么?

astype function returns a new array. astype函数返回一个新数组。 You need to assign the result: 您需要分配结果:

date = date.astype(int)
x = pd.DataFrame(['20.1','19.1','12.3'])
x[0].convert_objects(convert_numeric=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM