-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: more documentation that pandas by-default aligns when setting #13950
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
I think this comes down to pandas aligning by row labels before updating. In [7]: values = pd.DataFrame({'a': [4, 5], 'b': [9, 10]}, index=[3, 4])
In [8]: values
Out[8]:
a b
3 4 9
4 5 10
In [9]: a.iloc[0:2] = values
In [10]: a
Out[10]:
a b
0 NaN NaN
1 NaN NaN
2 3.0 8.0
3 4.0 9.0
4 5.0 10.0 Here's the alignment In [28]: l, r = a.iloc[0:2].align(values)
In [29]: l
Out[29]:
a b
0 1.0 6.0
1 2.0 7.0
3 NaN NaN
4 NaN NaN
In [30]: r
Out[30]:
a b
0 NaN NaN
1 NaN NaN
3 40.0 90.0 So you can see where the NaNs come in. What you probably want is In [33]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [34]: values = pd.DataFrame({'a': [4, 5], 'b': [9, 10]}, index=[3, 4])
In [35]: a.iloc[0:2] = np.asarray(values) # drops the row & column labels
In [36]: a
Out[36]:
a b
0 4 9
1 5 10
2 3 8
3 4 9
4 5 10 I belive aligning is indented for |
The is as-expected. pandas by definition will align on assignment. Doesn't matter how you select the values. If you don't want to align, then you can pass a same shaped non-pandas object.
The docs were updated w.r.t. to a very similar issue here, and appears here I suppose an additional doc note could be added, maybe as a separate sub-section, maybe after here. Or as a note. So will repurpose this issue. |
take |
In the latest official documentation (see the warning note) here, it is mentioned that:
As seen in example below,
|
* #13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
* pandas-dev#13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
#10) * pandas-dev#13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst --------- Co-authored-by: raj-thapa <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
* pandas-dev#13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
#12) * pandas-dev#13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst --------- Co-authored-by: raj-thapa <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
* pandas-dev#13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
#24) * pandas-dev#13950 - Updated documentation on pandas alignment when setting * Update doc/source/user_guide/indexing.rst --------- Co-authored-by: raj-thapa <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
Hi,
I'm not sure this is a bug but the behaviour is far from intuitive. I'm trying to assign the values of a slice of a
DataFrame
into another slice (of same size) of the sameDataFrame
.Specifically, in the current implementation the data type is not preserved (
int
tofloat
) and the value copied arenp.NaN
instead of the correct values.The expected output can be obtained using this counter-intuitive command:
This is the environment I'm working on:
The text was updated successfully, but these errors were encountered: