0

I've read all kinds of documentation about this warning in Pandas. Aside from getting frustrated because I still haven't seen a case where it's not absolutely clear what is trying to be done (so the warning feels like a needless complication), I'm still getting the warning even when seemingly doing what is recommended.

Here is my original code. I'm trying to convert two columns ('A' and 'B') to DateTime.

df[['A', 'B']] = df[['A', 'B']].apply(pd.to_datetime)

After reading about using .loc as the preferred method, I changed to:

df.loc[:, ['A', 'B']] = df.loc[:, ['A', 'B']].apply(pd.to_datetime)

But I'm still getting the same warning. Would someone be kind enough to point out what I'm missing?

NaiveBae
  • 358
  • 1
  • 10
  • 2
    `df` is probably already a copy somewhere before this line. So you need to go back and see where you made a subset and add `.copy()` – Erfan Aug 29 '22 at 19:17
  • can you provide a minimal example where this happens? – n4321d Aug 29 '22 at 19:18
  • 1
    if you have somewhere `df1=df`, you will have such a warning. `df1=df.copy()` will not have this warning, - P.S. these two statements are not the same – NoobVB Aug 29 '22 at 19:38
  • Yes, there was an upstream step where I isolated some of the rows: `df = original_df[original_df['payment_type']==1]`. I'm guessing that's where the problem is. – NaiveBae Aug 29 '22 at 20:44

0 Answers0