0

I am trying to convert string to datetime and assigning it to original column

df = dfMain[dfMain["Exception"] == "PriorCancel"]
df["CRE_DTTM"] = pd.to_datetime(df["CRE_DTTM"], format='%Y-%m-%d')

but I am getting SettingWithCopyWarning. I then tried

df.loc[:, "CRE_DTTM"] = pd.to_datetime(df["CRE_DTTM"], format='%Y-%m-%d')

But I still get the same warning.

chintan s
  • 6,170
  • 16
  • 53
  • 86
  • What is code before `df["CRE_DTTM"] = pd.to_datetime(dfC["CRE_DTTM"], format='%Y-%m-%d')` ? How is created `df` ? – jezrael May 13 '20 at 10:22
  • I have updated the code. The link you posted does not help. – chintan s May 13 '20 at 10:26
  • Use `df = dfMain[dfMain["Exception"] == "PriorCancel"].copy()` – jezrael May 13 '20 at 10:26
  • Thanks, using `.copy()` does not give the error. Even when I use `df["CRE_DTTM"] = pd.to_datetime(df["CRE_DTTM"], format='%Y-%m-%d')` or `df.loc[:, "CRE_DTTM"] = pd.to_datetime(df["CRE_DTTM"], format='%Y-%m-%d')` – chintan s May 13 '20 at 10:32
  • 1
    If you modify values in `df` later you will find that the modifications do not propagate back to the original data (`dfMain`), and that Pandas does warning. – jezrael May 13 '20 at 10:34
  • 1
    Makes sense. Thanks for you help @jezrael – chintan s May 13 '20 at 10:35

0 Answers0