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: Unexpected behavior when assigning multi-dimensional array to DataFrame column #40827

Open
2 tasks done
Socob opened this issue Apr 7, 2021 · 2 comments
Open
2 tasks done
Assignees
Labels
Bug Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@Socob
Copy link

Socob commented Apr 7, 2021

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

My question is essentially the same as the StackOverflow question “Unexpected behavior in assigning 2d numpy array to pandas DataFrame”, which, although it has an accepted answer, states in an edit to the question text:

Since the bounty is going to expiry [sic], I accepted the most voted answer. But it does not provide a answer to the above questions.

I agree, and still don’t understand the behavior described in the question, which I will restate here:
Given the following code:

import pandas
import numpy
df = pandas.DataFrame(numpy.zeros((4, 1)), columns=['A'])
y = numpy.random.randn(4, 2)
df['A'] = y

which results in (e.g.)

df
#           A
# 0  0.994449
# 1 -0.280376
# 2 -0.993288
# 3  0.009625
y
# array([[ 0.99444899, -0.80571928],
#        [-0.28037615, -0.47071158],
#        [-0.99328776, -0.15674664],
#        [ 0.00962502,  1.24896221]])

I expect it to throw an exception because of shape mismatch. But pandas silently accepted the assignment: y's first column is assigned to df.

Is this an intentional design? If yes, what is the rationale behind?

@Socob Socob added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Apr 7, 2021
@phofl
Copy link
Member

phofl commented Apr 7, 2021

Yeah this is a bug, would agree with anerror here

@phofl phofl added Bug 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 Usage Question labels Apr 7, 2021
@phofl phofl changed the title QST: Unexpected behavior when assigning multi-dimensional array to DataFrame column BUG: Unexpected behavior when assigning multi-dimensional array to DataFrame column Apr 7, 2021
@Mxchaeltrxn
Copy link

take

Mxchaeltrxn added a commit to Mxchaeltrxn/pandas that referenced this issue Apr 17, 2021
Mxchaeltrxn added a commit to Mxchaeltrxn/pandas that referenced this issue Apr 17, 2021
Mxchaeltrxn added a commit to Mxchaeltrxn/pandas that referenced this issue Apr 17, 2021
Mxchaeltrxn added a commit to Mxchaeltrxn/pandas that referenced this issue Apr 17, 2021
Mxchaeltrxn added a commit to Mxchaeltrxn/pandas that referenced this issue Apr 17, 2021
@simonjayhawkins simonjayhawkins added this to the 1.3 milestone May 25, 2021
@jreback jreback modified the milestones: 1.3, Contributions Welcome May 26, 2021
@mroeschke mroeschke added the Error Reporting Incorrect or improved errors from pandas label Aug 19, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
6 participants