1

In my Excel sheet there is a column "InvoiceDate" of type datetime64. I am trying to extract the date from this column and assign it a new column.

Below is my python code along with an image of the data sheet. It outputs a warning but the operation is completed successfully.

CODE:

df1["InvoiceMonth"] = df1["InvoiceDate"].dt.date

ERROR:

C:\Users\anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value

data

upe
  • 1,862
  • 1
  • 19
  • 33
  • [Please clarify the question you have](https://stackoverflow.com/help/how-to-ask) and provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – upe May 23 '20 at 19:36
  • My question is how can we use .loc or .iloc while creating a new column, so that this warning does not pop out. The new column which I am trying to create will only have dates from the column "InvoiceDate" and not time. – Bharat Chandwani May 24 '20 at 06:28
  • Maybe you can circumvent the warning by [using the apply function](https://stackoverflow.com/a/30132313/7259176) like so: `df1['InvoiceDate'] = df1['InvoiceDate'].apply(lambda x: x.strftime('%Y-%m-%d'))`. You can also [disable the warnings](https://stackoverflow.com/a/20627316/7259176). – upe May 24 '20 at 09:39
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – upe May 24 '20 at 10:15
  • df1['InvoiceMonth'] = df1['InvoiceDate'].apply(lambda x: x.strftime('%Y-%m-%d')). Using this code, even then it throws same warning. – Bharat Chandwani May 24 '20 at 16:44
  • And if it is fine, if I turn off the warnings using the command "pd.options.mode.chained_assignment = None" in Pandas – Bharat Chandwani May 24 '20 at 16:52

0 Answers0