I’m getting a chained assignment warning with the following code:
file_2 = pd.read_csv(data_list[place_holder2])
file_2.drop(file_2.index[0:3], inplace = True)
file_2.iloc[:,8] = pd.to_numeric(file_2.iloc[:,8])
file_2.iloc[:,8] = file_2.iloc[:,8].multiply(time_adjust)
file_2_sorted = file_2.sort_values(by = ["TRACK_ID","EDGE_TIME"], ascending = [True,True])
file_2_grouped = file_2_sorted.groupby("TRACK_ID", sort = False)
track_id_2 = file_2.iloc[:,1]
track_id_2.drop_duplicates(inplace = True)
for index_2 in track_id_2:
group_2 = file_2_grouped.get_group(index_2)
first_row = group_2.iloc[0,:]
compare_x_2 = float(first_row.iloc[9])
compare_y_2 = float(first_row.iloc[10])
if ((compare_x_1 == compare_x_2) and (compare_y_1 == compare_y_2)):
file_2_grouped.get_group(index_2).iloc[:,1] = track_number_1
file_2_grouped.get_group(index_2).iloc[:, 8] += time_interval
else:
file_2_grouped.get_group(index_2).iloc[:,1] = track_number_2
I’ve tried using .loc and the warning persists. How can I correct this?
The warning is with the last line of this bloc:
“FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!
You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.
A typical example is when you are setting values in a column of a DataFrame, like:
df[“col”][row_indexer] = value
Use df.loc[row_indexer, "col"] = values
instead, to perform the assignment in a single step and ensure this keeps updating the original df
.
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
file_2_grouped.get_group(index_2).iloc[:,1] = track_number_2″
4