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

BUG: DataFrame.__setitem__: ValueError: Expected a 1D array, got an array with shape (256, 2) #46545

Closed
2 of 3 tasks
anmyachev opened this issue Mar 28, 2022 · 1 comment
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@anmyachev
Copy link
Contributor

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

df = pd.DataFrame(np.zeros((256, 10)))
array_2d = np.zeros((256, 2))
df["col33"] = array_2d  # failed, see the exception below
df[0] = array_2d  # works, the case where column name already exist in `df`

# Note: the same behavior is observed for `loc` function

Issue Description

Inconsistent behavior for cases where the column name is already used in the dataframe and when not.

Exception:

Traceback (most recent call last):
  File "C:\Users\amyachev\Miniconda3\envs\modin\lib\site-packages\pandas\core\frame.py", line 3799, in _set_item_mgr
    loc = self._info_axis.get_loc(key)
  File "C:\Users\amyachev\Miniconda3\envs\modin\lib\site-packages\pandas\core\indexes\range.py", line 389, in get_loc
    raise KeyError(key)
KeyError: 'col33'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\amyachev\Miniconda3\envs\modin\lib\site-packages\pandas\core\frame.py", line 3655, in __setitem__
    self._set_item(key, value)
  File "C:\Users\amyachev\Miniconda3\envs\modin\lib\site-packages\pandas\core\frame.py", line 3845, in _set_item
    self._set_item_mgr(key, value)
  File "C:\Users\amyachev\Miniconda3\envs\modin\lib\site-packages\pandas\core\frame.py", line 3802, in _set_item_mgr
    self._mgr.insert(len(self._info_axis), key, value)
  File "C:\Users\amyachev\Miniconda3\envs\modin\lib\site-packages\pandas\core\internals\managers.py", line 1235, in insert
    raise ValueError(
ValueError: Expected a 1D array, got an array with shape (256, 2)

Expected Behavior

Same behavior for both cases.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.4.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 59.8.0
Cython : None
pytest : 7.0.0
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.2.2
numba : None
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : 0.17.0
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : 2022.01.0
scipy : 1.8.0
sqlalchemy : 1.4.31
tables : 3.7.0
tabulate : None
xarray : 0.21.1
xlrd : 2.0.1
xlwt : None
zstandard : None

@anmyachev anmyachev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 28, 2022
@phofl
Copy link
Member

phofl commented Mar 30, 2022

Added this to #46544

@phofl phofl closed this as completed Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants