0
votes

I have the following code:

block_table[[compared_attribute]] = block_table[[compared_attribute]].astype(int)

I want to change the datatype of a column. The code is working, but I get a warning from Python: 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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self[k1] = value[k2]

I looked into this warning and I was reading it may be creating a copy of the dataframe, instead of just overwriting it, so I tried the following solutions with no luck...

block_table.loc[[compared_attribute]] = block_table[[compared_attribute]].astype(int)




block_table.loc[:,compared_attribute] = block_table[[compared_attribute]].astype(int)
1
try using single brackets instead of the double ones, let us know what you get. - Ezer K
Thanks, I tried: block_table[compared_attribute] = block_table[compared_attribute].astype(int) but its still giving me the warning... - user1064285

1 Answers

0
votes

It should be as simple as:

block_table.loc[:,compared_attribute] = block_table[compared_attribute].astype(int)

This is assuming compared attributes is by columns otherwise, switch the colon and compared_attribute in the loc part. Also quite hard to answer without an example of what the data looks like and what compared_attribute looks like.