-1

I have defined a function as shown.

def calculate_mass_loss(group, idnum):
    """
    Calculates mass loss on star referenced by idnum due to high mass stars

    Parameters
    ----------
    group: `pd.DataFrame`
        Dataframe of all stars
    idnum: int
        ID number of star we care about
    Returns
    -------
    mass_loss: float
        Disc mass loss in solar masses per year
    """

    #this will select just the low-mass star I'm focussing on
    my_star = group.query(f'ID == {idnum}')

    # this will select just the high mass stars
    high_mass_stars = group.query('star_mass > 3')

    # use a function to calculate a separation column
    high_mass_stars['separation'] = calc_separation(high_mass_stars, my_star)
    high_mass_stars['flux_FUV'] = high_mass_stars['LFUV'] / (4*np.pi*high_mass_stars['separation']**2) /(1.6*10**(-3))
    total_fuv_flux = high_mass_stars.loc[:, ('flux_FUV')].sum()

    # use interpolation function to calculate mass loss
    mass_loss = find_mass_loss(total_fuv_flux, my_star)
    return(mass_loss)

calculate_mass_loss(snap_0, 2)

However, when I run the code I get an error that I don't understand:

<ipython-input-11-733debcada29>:20: 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
  high_mass_stars['separation'] = calc_separation(high_mass_stars, my_star)
<ipython-input-11-733debcada29>:21: 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
  high_mass_stars['flux_FUV'] = high_mass_stars.loc[:, ('LFUV')] / (4*np.pi*high_mass_stars.loc[:, ('separation')]**2) /(1.6*10**(-3))

screenshot of the error I get

I was wondering if anyone could explain it and potentially tell me how to fix it please???

pbarber
  • 139
  • 2
  • 8

1 Answers1

0

Since you're not returning high_mass_stars, why not use two intermediate variables, instead of assigning new columns to the high_mass_stars subset:

high_mass_stars = group.query('star_mass > 3')

separation = calc_separation(high_mass_stars, my_star)
flux_FUV = high_mass_stars['LFUV'] / (4 * np.pi * separation**2) / 1.6e-3
total_fuv_flux = flux_FUV.sum()

mass_loss = find_mass_loss(total_fuv_flux, my_star)

return mass_loss

For an explanation about the actual warning, see How to deal with SettingWithCopyWarning in Pandas .

9769953
  • 10,344
  • 3
  • 26
  • 37