shuoren fei
shuoren fei

Reputation: 11

How to deal with SettingWithCopyWarning in GeoPandas?

when i use

df['b'] = df['a'].str.split("_").str.get(0)

i get

D:\ProgramFiles\anaconda3\Lib\site-packages\geopandas\geodataframe.py:1543: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  super().__setitem__(key, value)

What should I do to cancel the warning without using pd.options.mode.copy_on_write = True?

GeoPandas.__version__ is 0.14.2

i tried

df.loc[:,'b'] = df['a'].str.split("_").str.get(0)

or

df.loc[:,'b'] = df.loc[:,'a'].str.split("_").str.get(0)

the warning still exists

Upvotes: -1

Views: 69

Answers (1)

shuoren fei
shuoren fei

Reputation: 11

I suspend the warning using

df = df.assign(b=lambda r:r['a'].str.split("_").str.get(0))

Upvotes: 1

Related Questions