0

the issue

  1. I have read all possible solutions to get rid of the warning "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"

but NOTHING worked, I can't get rid of the error message.

Many thank in advance for the time you will invest help me and certainly more people

which tools do I use

I am using kaggle to run a notebook, the configuration is straightfoward, I didn't load any library

the goal

I want to round a column and create a new one with the result

the data

id;    city.coord.lon; city.coord.lat 
12957; 4.32664;        51.219791    
12958; 3.33848;        50.812679 
12959; 3.81052;        50.869560

how is df_weather_in_cities created?

    # load the history city list
    with open(history_city_list_path) as f: 
        d = json.load(f) 

    df_weather_in_cities = pd.json_normalize(d) 


    # filter the belgian cities
    df_cities_weather_in_be = df_weather_in_cities[df_weather_in_cities['city.country']=='BE']

what are the dtypes of df_weather_in_cities?

id                            float64
city.name                      object
city.coord.lon                float64
city.coord.lat                float64
dtype: object

the code #1, using .loc on the right side


    df_cities_weather_in_be['lat_rounded'] = df_cities_weather_in_be.loc[:,('city.coord.lat')].apply(lambda x: np.round(x, 4))

the code #2, using a simple column selection


    df_cities_weather_in_be['long_rounded'] = df_cities_weather_in_be['city.coord.lat'].apply(lambda x: np.round(x, 4))

the code #3, using .loc on the left side


    df_cities_weather_in_be.loc[:,'long_rounded'] = df_cities_weather_in_be['city.coord.lat'].apply (lambda x: np.round(x, 4))

the code #4, using .loc on the both sides

    df_cities_weather_in_be.loc[:,'long_rounded'] = np.round(df_cities_weather_in_be.loc[:,'city.coord.lat'], 4)

the error

/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:1: 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 

"""Entry point for launching an IPython kernel.
Abdelkrim
  • 2,048
  • 5
  • 30
  • 44
  • How is created `df_cities_weather_in_be` ? – jezrael May 18 '20 at 11:36
  • @jezrael did you test my code? I have said that I have NOT found a solution to get rid the warning; the answer you are referring to proposes to disable the warning; I am looking for a solution not hiding the warning. – Abdelkrim May 18 '20 at 11:50
  • This errors is really unpleasant, becyse problem is with `df_cities_weather_in_be`, how is created. So my comment was for ask code for create `df_cities_weather_in_be` for solution with copy for avoid warning – jezrael May 18 '20 at 11:52
  • @jezrael df_cities_weather_in_be is created by reading a json file. I will update the question – Abdelkrim May 18 '20 at 14:20
  • 1
    Thanks, use `df_cities_weather_in_be = df_weather_in_cities[df_weather_in_cities['city.country']=='BE']. copy()` for avoid warning. – jezrael May 18 '20 at 14:59
  • @jezrael, you are awesome! I do prefer when you answer to the question ;-) put your comment as an answer to vote for it! – Abdelkrim May 18 '20 at 15:25

0 Answers0