0

Is there any way I can refactor my code to avoid getting this warning?

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

# get the total number of users who paid off
paidOffTotaDataFrame = newDF.loc[(newDF.Loan_Status=='PAIDOFF')]

#create column paid off in X days
difference = newDF.Paid_Off_Time - newDF.Effective_Date
paidOffTotaDataFrame['Paid_Off_In_Days'] = difference

I also tried

paidOffTotaDataFrame.loc[:,'Paid_Off_In_Days'] = difference

print(paidOffTotaDataFrame.apply(lambda row: "paid off column " + str(row['Paid_Off_In_Days']), axis=1))
bibscy
  • 2,598
  • 4
  • 34
  • 82

1 Answers1

1
  1. Copy the slice, so it is a new df and not a reference to a slice of old df: paidOffTotaDataFrame = newDF.loc[(newDF.Loan_Status=='PAIDOFF')].copy()

  2. Work only with the new df: paidOffTotaDataFrame['Paid_Off_In_Days'] = paidOffTotaDataFrame.Paid_Off_Time - paidOffTotaDataFrame.Effective_Date

Artyom Akselrod
  • 946
  • 6
  • 14
  • This is so weird. Why would they even create a chained reference to the old variable ```newDF``` when I modify ```paidOffTotaDataFrame```? I am coming from Apple Swift language. – bibscy Jul 03 '20 at 11:34
  • @bibscy the reason not to copy data when you create ```paidOffTotaDataFrame``` is to save memory. Once you want to modify slice - you need to make a copy, as python does not know what to do with the non-sliced parts of your ```newDF``` – Artyom Akselrod Jul 06 '20 at 08:38