0

I have the following lines of code to change the type of data in pandas DataFrame columns:

X["AIRLINE_ARR_ICAO"] = X["AIRLINE_ARR_ICAO"].apply(lambda x: str(x))
X["WAKE"] = X["WAKE"].apply(lambda x: str(x))
X["PLANNED_TURNAROUND"] = X["PLANNED_TURNAROUND"].apply(lambda x: float(str(x)))

But I get an error:

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

I can overcome this warming by doing X1 = X.copy(), but it may leak to a memory leak.

ScalaBoy
  • 3,254
  • 13
  • 46
  • 84
  • Do you create `X` as a slice from another `DataFrame` or from some other operation like `.drop_duplicates`? – ALollz Feb 23 '19 at 17:08
  • 1
    "but it may leak to a memory leak." Why would it lead to a memory leak? Anyway, this is hugely inefficient to use `lambda`. Use `X["AIRLINE_ARR_ICAO"] = X["AIRLINE_ARR_ICAO"].astype(str)` – roganjosh Feb 23 '19 at 17:08
  • @ALollz: I create X as a slice from another DataFrame: `X = csvData[:-1]` – ScalaBoy Feb 23 '19 at 17:09
  • @roganjosh: Thanks. Answer: because it creates a copy of DataFrame. No? – ScalaBoy Feb 23 '19 at 17:10
  • Yes, so the warning is saying that your slice is a view on `csvData` and any changes will also be seen in the original data – roganjosh Feb 23 '19 at 17:10
  • That's different than a "memory leak" which is unrecoverable memory. You are correct that it will take up _more_ memory, but this is not a memory leak. Sometimes you really just have to make copies of data to avoid changes being propagated to other data that you hadn't anticipated; that is what the warning is warning you about. – roganjosh Feb 23 '19 at 17:11
  • @roganjosh: In my case it does not matter if the original DataFrame changes, because I do not use it anymore. I only want to remove warnings. – ScalaBoy Feb 23 '19 at 17:12

0 Answers0