0

How do i get rid of setting with copy warning upon assigining the value of cosine similarity of two dataframes to the column "sim" of dataframe spotify_df and is it something I should worry about.

P.S: user_track_df has only 1 row and spotify_df has around 6000 rows and both have equal number of columns.

Code snippet:

def generate_recommendations(spotify_df, user_track_df):
    spotify_df['sim'] = cosine_similarity(spotify_df.drop(['name', 'artists', 'id'], axis=1),
                                           user_track_df.drop(['name', 'artists', 'id'], axis=1))

    spotify_df.sort_values(by='sim', ascending=False, inplace=True, kind="mergesort")
    spotify_df.reset_index(drop=True, inplace=True)

    return spotify_df.head(10)

Warning snippet: enter image description here

Game Code
  • 21
  • 4

1 Answers1

1

Most likely your source DataFrame (spotify_df) has been created as a view of another DataFrame.

The side effect is that spotify_df does not have its own data buffer. Instead it shares the data buffer with the DataFrame it has been created from.

To get rid of this warning: When you create spotify_df, add .copy() to the code. This way spotify_df will be an "independent" DataFrame, with its own data buffer, so you can do with it anything you want.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • Thanks, this worked. But i still don't understand one thing that when I comment out the part of code that contains the cosine similarity function, it doesn't give me the warning anymore, why does it give me warning when i use cosine similarity without making the spotify_df as copy. – Game Code May 24 '22 at 10:10
  • This error occurs just in your function. Another remedy to this error is that you can create an "independent" copy just in your function (and then return the initial part of it). – Valdi_Bo May 24 '22 at 13:13