I have an question about how shape broadcasting works in Pandas. Suppose I have a dataframe:
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [11, 22, 33, 44, 55]})
And I try to replace the first two rows of column 'A' with their corresponding values in column 'B'.
When I try assigning the values in column B explicitly as a list:
df.loc[[0,1], 'A'] = list(df['B'])
I get the obvious shape broadcast error:
ValueError: shape mismatch: value array of shape (5,) could not be broadcast to indexing result of shape (2,)
But when I assign column B directly:
df.loc[[0,1], 'A'] = df['B']
I don't get any errors and Pandas implicitly subsets column B and assigns to column A. The final output is
A B
0 11 11
1 22 22
2 3 33
3 4 44
4 5 55
Is this expected behavior? Why does Pandas not raise a shape mismatch error in this case?