-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Assignment via .loc #27286
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
Comments
Simpler example In [23]: df = pd.DataFrame({"A": [1, 2, 3]})
In [24]: df.loc[:, 'B'] = pd.DataFrame({"C": [1, 2, 3]})
In [25]: df
Out[25]:
A B
0 1 NaN
1 2 NaN
2 3 NaN You're getting tripped up by automatic alignment. The names in the However, I would think it has the same semantics as In [30]: df = pd.DataFrame({"A": [1, 2, 3]})
In [31]: df[['B']] = pd.DataFrame({"C": [4, 5, 6]})
In [32]: df
Out[32]:
A B
0 1 4
1 2 5
2 3 6 |
Thanks for your response. If it's not a bug I wasn't aware the behaviour of loc was as above. |
This doesnt appear to actually matter. For example, you can have different names in the value df compared to the target df and get the expected results. What appears to matter is that the name in the column index in loc already exists in the target dataframe. EDITED case below to values 9,9,9 in order to show df updating See case: df = pd.DataFrame({"A": [1, 2, 3]})
df.loc[:, 'A'] = pd.DataFrame({"ZZZ": [9, 9, 9]})
print df
A
0 9
1 9
2 9 compared to: df = pd.DataFrame({"A": [1, 2, 3]})
df.loc[:, 'D'] = pd.DataFrame({"ZZZ": [1, 2, 3]})
print df
A D
0 1 NaN
1 2 NaN
2 3 NaN It could be my ignorance of using .loc but I was not aware there was constraint that the column name had to exist in advance. If that's the case, then why does the below work: df = pd.DataFrame({"A": [1, 2, 3]})
df.loc[:, 'E'] = 4
print df
A EE
0 1 4
1 2 4
2 3 4 |
While I certainly won't exclude there might be inconsistency with aligning indexes/inserting missing columns, my simple explanation of why
won't work is simply that you're trying to fit a 2D object (
works. Sure, the slot and the value happen to have the same number of cells in your example, but I would never expect this broadcasting to lower dimensionality to be supported. And it is the |
I think this can closed. While broadcasting a 1D structure to a 2D slot is something we can support, squeezing a nx1 structure into a 1D slot is not worth the effort, I think. |
Code Sample, a copy-pastable example if possible
Problem description
When printing df2['foo2'] I get:
0 NaN
1 NaN
2 NaN
If instead I change the line assigning to 'foo2' to:
When printing df2['foo2'] I get:
0 egs
1 gsg
2 ghi
I would have expected df.loc[:, 'foo2'] and df['foo2'] assignments to behave the same way.
Output of
pd.show_versions()
pandas: 0.23.4
numpy: 1.15.4
python: 2.7.14.final.0
The text was updated successfully, but these errors were encountered: