0

I am trying to assign certain values to a column in a dataframe within a Python function.

    df = df[(df['date'] <= max_date) & (df['date'] > min_date) | (df['x_date'] <= max_date) & (df['x_date'] > min_date)]
    numbers = len(df)
    df["patients"] = value_counts.patients
    df["numbers"] = numbers

However, I get this error:

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
  df["patients"] = value_counts.patients
file.py:52: 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

How can I get rid of this warning?

Jnl
  • 194
  • 1
  • 11
  • Error means there is some filtration in code before `df["patients"] = value_counts.patients`. Need add `.copy()` for avoid warning - e.g. `df = df[df.A > 10].copy()` – jezrael Jun 29 '22 at 13:47
  • So need `df = df[(df['date'] <= max_date) & (df['date'] > min_date) | (df['x_date'] <= max_date) & (df['x_date'] > min_date)].copy()` – jezrael Jun 29 '22 at 13:49
  • even with that, I get the same error @jezrael – Jnl Jun 29 '22 at 13:56

0 Answers0