Skip to content

Strange behaviour of pd.DataFrame.drop() with inplace argument #30484

Closed
@DenisVorotyntsev

Description

@DenisVorotyntsev

Code Sample, a copy-pastable example if possible

# input - 1 
df = pd.DataFrame({})
df["x1"] = [1, 2, 3, 4, 5]
df["x2"] = [0, 0, 0, 1, 1]
df["target"] = [10, 20, 30, 40, 50]
y = df["target"]
df.drop("target", axis=1, inplace=True)
y = y + np.min(y)

# output df (without removed target column)
	x1	x2
0	1	0
1	2	0
2	3	0
3	4	1
4	5	1

# input - 2 
df = pd.DataFrame({})
df["x1"] = [1, 2, 3, 4, 5]
df["x2"] = [0, 0, 0, 1, 1]
df["target"] = [10, 20, 30, 40, 50]
y = df["target"]
df.drop("target", axis=1, inplace=True)
y += np.min(y)

# output df (with the removed target column)

  | x1 | x2 | target
-- | -- | -- | --
0 | 1 | 0 | 20
1 | 2 | 0 | 30
2 | 3 | 0 | 40
3 | 4 | 1 | 50
4 | 5 | 1 | 60

Problem description

Pandas return DataFrame with removed column after doing in-place operation with a column. Is it a bug or intentional behavior?

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None

pandas : 0.25.1
numpy : 1.16.2
pytz : 2018.9
dateutil : 2.7.5
pip : 18.1
setuptools : 42.0.2
Cython : 0.29.2
pytest : 4.1.0
hypothesis : None
sphinx : 1.8.3
blosc : None
feather : None
xlsxwriter : 1.1.2
lxml.etree : 4.3.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : 7.2.0
pandas_datareader: None
bs4 : 4.7.1
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.3.0
matplotlib : 3.0.2
numexpr : 2.6.9
odfpy : None
openpyxl : 2.5.12
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.2.1
sqlalchemy : 1.2.15
tables : 3.4.4
xarray : 0.14.0
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.1.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions