title: How to fix this SettingWithCopyWarning with pd.DataFrame.apply()?
body: <p>I've a dataframe with 2-columns: ImageData, Label. ImageData column is of 2-D array for various dimensions. Label column is boolean True/False.</p>
  <p>I'm trying to convert data in the &quot;ImageData&quot; column to 128x128 shape (along with minor other transformations). So, I'm doing following:</p>
  <pre><code>def convert_image_to_binary_image(img: np.ndarray, threshold: int = 1, max_value: int = 1) -&gt; np.ndarray:
      ret, bin_img = cv.threshold(img, thresh=threshold, maxval=max_value, type=cv.THRESH_BINARY)
      bin_img = bin_img.astype('float32')
      return bin_img
  
  def transform_img_dimension(img: np.ndarray, target_width: int = 128, target_height: int = 128) -&gt; np.ndarray:
      img = img.astype('uint8')
  
      bin_image = convert_image_to_binary_image(img)
      bin_3dimg = tf.expand_dims(input=bin_image, axis=2)
      bin_img_reshaped = tf.image.resize_with_pad(image=bin_3dimg, target_width=target_width, target_height=target_height, method=&quot;bilinear&quot;)
  
      xformed_img = np.squeeze(bin_img_reshaped, axis=2)
  
      # return xformed_img.copy()
      return xformed_img
  </code></pre>
  <p>I'm calling <em>apply</em> as following:</p>
  <pre><code>testDF[&quot;ImageData&quot;] = testDF.apply(lambda row: transform_img_dimension(row[&quot;ImageData&quot;], axis=1)
  </code></pre>
  <p>But that's causing SettingWithCopyWarning.</p>
  <p>I tried defining a wrapper function (instead of lambda) as following:</p>
  <pre><code>def transform_dimension(row: pd.Series, target_width: int = 128, target_height: int = 128) -&gt; np.ndarray:
      copy_row = row.copy(deep=True)
      xformed_data = transform_img_dimension(copy_row[&quot;ImageData&quot;], target_width=target_width, target_height=target_height)
      del copy_row
      return xformed_data
  </code></pre>
  <p>And updated the call to <em>apply</em> as following:</p>
  <pre><code>testDF[&quot;ImageData&quot;] = testDF.apply(transform_dimension, axis=1)
  </code></pre>
  <p>However, this is not resolving the problem. What is the fix for this warning for my case?</p>
  <h2>Update 1:</h2>
  <p>If I rewrite as following, I don't get the warning</p>
  <pre><code>testDF2 = testDF.copy(deep=True)
  testDF2[&quot;ImageData&quot;] = testDF.apply(lambda row: transform_img_dimension(row[&quot;ImageData&quot;], axis=1)
  </code></pre>
  <p>Is it not memory overhead now to hold 2 dataframes? Am I recommended to delete the original dataframe, testDF, now?</p>