Friday, 14 September 2018

Unexpected behavior in assigning 2d numpy array to pandas DataFrame

I have the following code:

x = pd.DataFrame(np.zeros((4, 1)), columns=['A'])
y = np.random.randn(4, 2)
x['A'] = y

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

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

I tried both pandas 0.21 and 0.23.



from Unexpected behavior in assigning 2d numpy array to pandas DataFrame

No comments:

Post a Comment