By ldevyataykina


2016-07-01 13:43:01 8 Comments

I try to delete some column and convert some value in column with

df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True)
df2['date'] = df2['date'].map(lambda x: str(x)[1:])
df2['date'] = df2['date'].str.replace(':', ' ', 1)
df2['date'] = pd.to_datetime(df2['date'])

and to all this string I get

  df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True)
C:/Users/����� �����������/Desktop/projects/youtube_log/filter.py:11: 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

What is problem there?

1 comments

@piRSquared 2016-07-01 14:07:02

Your df2 is a slice of another dataframe. You need to explicitly copy it with df2 = df2.copy() just prior to your attempt to drop

Consider the following dataframe:

import pandas as pd
import numpy as np


df1 = pd.DataFrame(np.arange(20).reshape(4, 5), list('abcd'), list('ABCDE'))

df1

enter image description here

Let me assign a slice of df1 to df2

df2 = df1[['A', 'C']]

enter image description here

df2 is now a slice of df1 and should trigger those pesky SettingWithCopyWarning's if we try to change things in df2. Let's take a look.

df2.drop('c')

enter image description here

No problems. How about:

df2.drop('c', inplace=True)

There it is:

enter image description here

The problem is that pandas tries to be efficient and tracks that df2 is pointing to the same data as df1. It is preserving that relationship. The warning is telling you that you shouldn't be trying to mess with the original dataframe via the slice.

Notice that when we look at df2, row 'c' has been dropped.

df2

enter image description here

And looking at df1 we see that row 'c' is still there.

df1

enter image description here

pandas made a copy of df2 then dropped row 'c'. This is potentially inconsistent with what our intent may have been considering we made df2 a slice of and pointing to same data as df1. So pandas is warning us.

To not see the warning, make the copy yourself.

df2 = df2.copy()
# or
df2 = df1[['A', 'C']].copy()

@ldevyataykina 2016-07-01 14:11:29

Thank you for great explanation!

@Gabriel 2016-11-30 11:45:03

if don't want to do a copy and still remove the message, you can set it up: pd.options.mode.chained_assignment = None

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Renaming columns in pandas

18 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

10 Answered Questions

[SOLVED] Select rows from a DataFrame based on values in a column in pandas

13 Answered Questions

[SOLVED] Delete column from pandas DataFrame

22 Answered Questions

[SOLVED] Adding new column to existing DataFrame in Python pandas

13 Answered Questions

[SOLVED] How to deal with SettingWithCopyWarning in Pandas?

2 Answered Questions

[SOLVED] Replace letter in string pandas

  • 2018-08-31 11:12:37
  • BERA
  • 59 View
  • 3 Score
  • 2 Answer
  • Tags:   pandas

3 Answered Questions

[SOLVED] Dataframe Warning : SettingWithCopyWarning in python

2 Answered Questions

[SOLVED] Pandas: SettingWithCopyWarning:

  • 2017-07-18 14:55:37
  • salhin
  • 1339 View
  • 4 Score
  • 2 Answer
  • Tags:   python pandas

1 Answered Questions

Sponsored Content