0

I'm trying an exercise from a DataQuest proyect, and when creating a 'month' column from a datetime variable, to aggregate values, I get a warning message that I don't know how to use to fix my code (I've read something about this warning message, but didn't find the connection with my code).

My piece of code:

#Create a new column containing the month
daytime_data['month'] = daytime_data['date_time'].dt.month

#Aggregate the data and avearge it by month
by_month = daytime_data.groupby('month').mean()
print(by_month['traffic_volume'])

The warning message:

C:\Users\Alvaro\AppData\Local\Temp/ipykernel_4856/2147418321.py:2: 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 instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy daytime_data['month'] = daytime_data['date_time'].dt.month

Could you please help me find what should I fix in my code?

Josip Juros
  • 349
  • 2
  • 12
Álvaro V.
  • 39
  • 6
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Hamzah Mar 31 '22 at 11:37

1 Answers1

1

The answer is in your warning message. to remove this error simply change the

daytime_data['month'] = daytime_data['date_time'].dt.month

into

daytime_data.loc[:, 'month'] = daytime_data['date_time'].dt.month
Waleed Alfaris
  • 136
  • 1
  • 9
  • Hi Waleed! I already tried that, but it doesn't work, either :( - this is the warning message that I get when I try that code: *C:\Users\Alvaro\anaconda3\lib\site-packages\pandas\core\indexing.py:1667: 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 instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[key] = value* – Álvaro V. Apr 01 '22 at 07:23
  • Can you post the link to the DataQuest excersice and your complete code so I may try to replicate the issue? – Waleed Alfaris Apr 01 '22 at 07:39
  • Sure! Here's the link to the full exercise and code: https://community.dataquest.io/t/my-answer-to-guided-project-finding-heavy-traffic-indicators-on-i-94/561401 – Álvaro V. Apr 04 '22 at 09:01
  • 1
    Apologies for the delayed response Alvaro. After looking at your code I have identified the issue. The issue is that daytime_data itself is a slice of the metro dataframe. To remove this error, create the daytime_data dataframe from a copy of the metro dataframe by simply adding the copy() method to it. ```daytime_data = metro.copy(deep=True)[(metro['date_time'].dt.hour >= 7) & (metro['date_time'].dt.hour < 19)]``` – Waleed Alfaris Apr 19 '22 at 07:24
  • 1
    This can also be split into two lines if you prefer. ```daytime_data = metro.copy(deep=True)``` ```daytime_data['month'] = daytime_data['date_time'].dt.month``` – Waleed Alfaris Apr 19 '22 at 07:27