Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing a column with iloc replaces another column with same name #22036

Closed
mitar opened this issue Jul 24, 2018 · 2 comments · Fixed by #32477
Closed

Replacing a column with iloc replaces another column with same name #22036

mitar opened this issue Jul 24, 2018 · 2 comments · Fixed by #32477
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@mitar
Copy link
Contributor

mitar commented Jul 24, 2018

Code Sample, a copy-pastable example if possible

import pandas

a = pandas.DataFrame({'a': ['0'], 'b': ['str']})

print('---')
print(a)

a.iloc[:, 0] = [int(v) for v in a.iloc[:, 0]]

print('---')
print(a)

b = pandas.concat([a, pandas.DataFrame({'b': ['str2']})], axis=1)

print('---')
print(b)

b.iloc[:, 2] = ['str3']

print('---')
print(b)

Problem description

The issue seems to be that if there is a DataFrame with duplicate column names and mixed dtypes, if I try to replace one column with another value, using iloc, another column with same name is replaced as well.

Expected Output

The final print should be:

   a    b     b
0  0  str  str3

And not:

   a     b     b
0  0  str3  str3

It is interesting that if I change concat line to (see renaming of column b to c):

b = pandas.concat([a, pandas.DataFrame({'c': ['str2']})], axis=1)

Then the output is correctly:

   a    b     c
0  0  str  str3

Also, if a.iloc[:, 0] = [int(v) for v in a.iloc[:, 0]] is commented out, it works also correctly.

Moreover, the following also work correctly (see the change of 0 column index into [0] column index, and similar for 2 (this latter change is not really necessary to make it work)):

import pandas

a = pandas.DataFrame({'a': ['0'], 'b': ['str']})

print('---')
print(a)

a.iloc[:, [0]] = [int(v) for v in a.iloc[:, 0]]

print('---')
print(a)

b = pandas.concat([a, pandas.DataFrame({'b': ['str2']})], axis=1)

print('---')
print(b)

b.iloc[:, [2]] = ['str3']

print('---')
print(b)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.0-46-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.3
pytest: None
pip: 18.0
setuptools: 40.0.0
Cython: None
numpy: 1.15.0
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@WillAyd
Copy link
Member

WillAyd commented Jul 24, 2018

Strange indeed - investigation and PRs welcome!

@WillAyd WillAyd added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 24, 2018
@WillAyd WillAyd added this to the Contributions Welcome milestone Jul 24, 2018
@fjdiod
Copy link
Contributor

fjdiod commented Jul 25, 2018

Seems problem is with

def setter(item, v):
s = self.obj[item]
pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer
# perform the equivalent of a setitem on the info axis
# as we have a null slice or a slice with full bounds
# which means essentially reassign to the columns of a
# multi-dim object
# GH6149 (null slice), GH10408 (full bounds)
if (isinstance(pi, tuple) and
all(com.is_null_slice(idx) or
com.is_full_slice(idx, len(self.obj))
for idx in pi)):
s = v
else:
# set the item, possibly having a dtype change
s._consolidate_inplace()
s = s.copy()
s._data = s._data.setitem(indexer=pi, value=v)
s._maybe_update_cacher(clear=True)
# reset the sliced object if unique
self.obj[item] = s

as I understand item is a column name, so on line 527 we're selecting both columns as they have the same name

@jbrockmendel jbrockmendel added this to iloc/iat setitem with duplicates in Indexing Feb 18, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.1 Mar 8, 2020
@jbrockmendel jbrockmendel removed this from iloc/iat setitem with duplicates in Indexing Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants