Renaming column: A value is trying to be set on a copy of a slice from a DataFrame

Question:

I am trying to rename a specific column.

Sample Data:

FROM_ID  TO_ID  DURATION_H  DIST_KM
    1      7    0.528556    38.43980
    1     26    0.512511    37.38515
    1     71    0.432453    32.57571
    1     83    0.599486    39.26188
    1     98    0.590517    35.53107

Sample Code:

df_temp_MSU1 = MSU_TT_Temp.loc[(MSU_TT_Temp['FROM_ID'] == 1)]
df_temp_MSU1.rename(columns = {'DURATION_H':'MSU1_DURATION'}, inplace = True)

I am facing this warning:

/var/folders/1w/x6q43vw10bj_1yrjqdwpg6047lmtwh/T/ipykernel_63817/131531939.py:7: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df_temp_MSU1.rename(columns = {'DURATION_H':'MSU1_DURATION'}, inplace = True)
/var/folders/1w/x6q43vw10bj_1yrjqdwpg6047lmtwh/T/ipykernel_63817/131531939.py:12: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

It is a warning message. I am trying to rename a column. Does this affect anything related to data that I am carrying in another column (After renaming)?

Asked By: LearningLogic

||

Answers:

Do this to counter warning (Using .copy()):

df_temp_MSU1 = MSU_TT_Temp.loc[(MSU_TT_Temp['FROM_ID'] == 1)].copy()
Answered By: ExploringAI