Skip to content

Address pandas SettingWithCopyWarning #50

@thorwhalen

Description

@thorwhalen

In cosmograph==1.0.0b1:

This warning always comes up on first run of cosmo:

/Users/thorwhalen/.pyenv/versions/3.12.12/envs/p12/lib/python3.12/site-packages/cosmograph/widget/utils.py:27: 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[df_int32.columns] = df_int32

Here's where the error occurs:

@memory.cache
def get_buffered_arrow_table(df):
  """Converts a Pandas DataFrame to a buffered Arrow IPC stream format.

  This function is cached using joblib.Memory.
  """

  if df is None:
      return None
  try:
      df_int32 = df.select_dtypes(include=["int64"]).astype("int32")
      df[df_int32.columns] = df_int32  # <--- HERE. 
      table = pa.Table.from_pandas(df)
      sink = pa.BufferOutputStream()

I can't find the github branch corresponding to cosmograph==1.0.0b1, but anyway, AI will help you repair this in minutes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions