Skip to content

Instantly share code, notes, and snippets.

@lppier
Last active March 6, 2019 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lppier/5aacf0aaf310ce2ad1e5f24bb3db321b to your computer and use it in GitHub Desktop.
Save lppier/5aacf0aaf310ce2ad1e5f24bb3db321b to your computer and use it in GitHub Desktop.
Avoiding pandas copywithsetting error
#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
# assign was introduced in pandas 0.16 to deal with this false positive
# Instead of
df_weekly_season.loc[:, 'market'] = market
# or
df_weekly_season['market'] = market
# do
df_weekly_season = df_weekly_season.assign(market=market)
# Instead of
df_weekly_season['DOW_ENG'] = weekly_season['ds'].apply(
lambda x: calendar.day_name[pd.to_datetime(x).weekday()])
# do
df_weekly_season = df_weekly_season.assign(DOW_ENG=weekly_season['ds'].apply(
lambda x: calendar.day_name[pd.to_datetime(x).weekday()]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment