Skip to content

BUG: Setting values to slice fails with duplicated column name #38521

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

Closed
3 tasks done
leonarduschen opened this issue Dec 16, 2020 · 1 comment · Fixed by #39278
Closed
3 tasks done

BUG: Setting values to slice fails with duplicated column name #38521

leonarduschen opened this issue Dec 16, 2020 · 1 comment · Fixed by #39278
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@leonarduschen
Copy link
Contributor

leonarduschen commented Dec 16, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Originally posted on StackOverflow.

Possibly related to #15695 (the traceback looks different though)


Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame(columns=['a', 'b', 'b'])
df.loc[:, 'a'] = list(range(5))  # raise ValueError

Traceback:

Traceback (most recent call last):
  File "c:\Users\leona\pandas\main.py", line 3, in <module>
    df.loc[:, 'a'] = list(range(5))
  File "c:\Users\leona\pandas\pandas\core\indexing.py", line 691, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "c:\Users\leona\pandas\pandas\core\indexing.py", line 1636, in _setitem_with_indexer
    self._setitem_single_block(indexer, value, name)
  File "c:\Users\leona\pandas\pandas\core\indexing.py", line 1862, in _setitem_single_block
    self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
  File "c:\Users\leona\pandas\pandas\core\internals\managers.py", line 565, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "c:\Users\leona\pandas\pandas\core\internals\managers.py", line 428, in apply
    applied = getattr(b, f)(**kwargs)
  File "c:\Users\leon\pandas\pandas\core\internals\blocks.py", line 1022, in setitem
    values[indexer] = value
ValueError: cannot copy sequence with size 5 to array axis with dimension 0

Problem description

It works with no duplicated column:

df = pd.DataFrame(columns=['a', 'b', 'c'])
df.loc[:, 'a'] = list(range(5))

These work even with duplicated column names:

df = pd.DataFrame(columns=['a', 'b', 'b'])
df['a'] = list(range(5))  # Same as expected output below

df = pd.DataFrame(columns=['a', 'b', 'b'])
df.a = list(range(5))  # Same as expected output below

Setting on new column name is okay:

df = pd.DataFrame(columns=['a', 'b', 'b'])
df.loc[:, 'c'] = list(range(5))

#      a    b    b  c
# 0  NaN  NaN  NaN  0
# 1  NaN  NaN  NaN  1
# 2  NaN  NaN  NaN  2
# 3  NaN  NaN  NaN  3
# 4  NaN  NaN  NaN  4

Expected Output

   a    b    b
0  0  NaN  NaN
1  1  NaN  NaN
2  2  NaN  NaN
3  3  NaN  NaN
4  4  NaN  NaN

Output of pd.show_versions()

Output:

INSTALLED VERSIONS

commit : 122d502
python : 3.9.0.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 11, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_Singapore.1252

pandas : 1.3.0.dev0+83.g122d50246.dirty
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.2.3
setuptools : 49.2.1
Cython : 0.29.21
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@leonarduschen leonarduschen added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 16, 2020
@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 27, 2020
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Dec 27, 2020
@rhshadrach
Copy link
Member

Thanks for the report. Further investigations and PRs to fix are most welcome.

@jreback jreback modified the milestones: Contributions Welcome, 1.3 Jan 21, 2021
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.

3 participants