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: iloc.__setitem__ with a numpy array throws a TypeError expecting 1-D list #33828

Closed
2 of 3 tasks
Tracked by #7
devin-petersohn opened this issue Apr 27, 2020 · 3 comments · Fixed by #52744
Closed
2 of 3 tasks
Tracked by #7
Assignees
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions

Comments

@devin-petersohn
Copy link
Contributor

devin-petersohn commented Apr 27, 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import numpy as np
import pandas as pd

dfx = pd.DataFrame({'a': np.ones(10)}) 
dfx.iloc[np.array([0]), np.array([0])] = np.array([[2]]) # works
dfx['a_I'] = dfx['a'].astype('Int64') 
dfx = dfx[["a_I"]] 
dfx.iloc[np.array([0]), np.array([0])] = np.array([[2]]) # doesn't work

Problem description

The second case should set the value to 2.

Expected Output

I have narrowed the problem down to here:

indexer = maybe_convert_ix(*indexer)

The dimension of the indexer is being increased in that call.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Darwin
OS-release : 19.3.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2018.5
dateutil : 2.7.3
pip : 19.2.3
setuptools : 42.0.2
Cython : 0.29.10
pytest : 5.4.1
hypothesis : None
sphinx : 2.4.4
blosc : None
feather : 0.4.0
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 7.0.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.0
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : 0.13.1
pyarrow : 0.16.0
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : 0.4.0
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : 3.6.1
tabulate : None
xarray : 0.15.0
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : 0.48.0

@devin-petersohn devin-petersohn added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 27, 2020
@devin-petersohn devin-petersohn changed the title BUG: iloc.__setitem__ with a numpy arrow throws a TypeError expecting 1-D lst BUG: iloc.__setitem__ with a numpy arrow throws a TypeError expecting 1-D list Apr 27, 2020
@devin-petersohn devin-petersohn changed the title BUG: iloc.__setitem__ with a numpy arrow throws a TypeError expecting 1-D list BUG: iloc.__setitem__ with a numpy array throws a TypeError expecting 1-D list Apr 27, 2020
@jorisvandenbossche jorisvandenbossche added ExtensionArray Extending pandas with custom dtypes or arrays. 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 Apr 28, 2020
@jorisvandenbossche jorisvandenbossche added this to the Contributions Welcome milestone Apr 28, 2020
@jorisvandenbossche
Copy link
Member

@devin-petersohn thanks for the report!

The increase in dimensionality of indexer might be a problem as well, but the actual error is coming from trying to coerce the LHS values (np.array([[2]])) to an IntegerArray. The integer array constructor expects a 1D array, not a 2D like that.
So when indexing a dataframe, the 2D array is fine, but when passing this value to Block.setitem we should reduce the dimension of the value.

@CloseChoice
Copy link
Member

CloseChoice commented May 1, 2020

Actually this happens to more than just the IntegerArray:

dfx = pd.DataFrame({'a': np.ones(10)})
dfx['Categorical'] = pd.Categorical(dfx['a'])
dfx[['Categorical']].iloc[np.array([0]), np.array([0])] = np.array([[3]])

The BooleanArray shares the same checks in the coerce function. We should check all other array types to be consistent here.
Maybe implementing a more general coerce function (coerce2d) is the way to go here. Without using masks (and commenting out the dimensionality checks) the following works:

dfx = pd.DataFrame({'a': np.ones(10)})
dfx['Integer'] = dfx['a'].astype('Int64')
dfx[['Integer']].iloc[np.array([0]), np.array([0])] = np.array([[2]])

and gives the correct result:

     a  Integer 
0  1.0        2 
1  1.0        1 
2  1.0        1 
3  1.0        1 
4  1.0        1 
5  1.0        1 
6  1.0        1 
7  1.0        1 
8  1.0        1 
9  1.0        1 

What do you think @jorisvandenbossche @devin-petersohn ?

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@phofl phofl added Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Apr 18, 2023
@ArnaudChanoine
Copy link
Contributor

take

ArnaudChanoine added a commit to ArnaudChanoine/pandas that referenced this issue Apr 18, 2023
ArnaudChanoine added a commit to ArnaudChanoine/pandas that referenced this issue Apr 18, 2023
mroeschke pushed a commit that referenced this issue Apr 19, 2023
* Add non-regression test for GH-33828

* Reformat some lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants