简体   繁体   中英

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. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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