-
-
Couldn't load subscription status.
- Fork 19.2k
BUG/DEPR: loc.__setitem__ incorrectly accepting positional slices #31840
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
Changes from 17 commits
a076e6a
dad937e
ccd7ad8
9d6514d
b4c63ee
6b7ba98
1970398
02ece46
7ee2edd
6a0ebb4
5f550bf
a1a7778
aa5a6a1
87f424d
f84f837
8d2a19a
75f2db6
2933f2c
acb87aa
1589b27
46402f8
32cfe70
a14aa8d
e9a89a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -863,6 +863,7 @@ def test_loc_setitem_empty_append_raises(self): | |
|
|
||
| data = [1, 2] | ||
| df = DataFrame(columns=["x", "y"]) | ||
| df.index = df.index.astype(np.int64) | ||
| msg = ( | ||
| r"None of \[Int64Index\(\[0, 1\], dtype='int64'\)\] " | ||
| r"are in the \[index\]" | ||
|
|
@@ -975,3 +976,42 @@ def test_loc_mixed_int_float(): | |
|
|
||
| result = ser.loc[1] | ||
| assert result == 0 | ||
|
|
||
|
|
||
| def test_loc_with_positional_slice_deprecation(): | ||
| # GH#31840 | ||
| ser = pd.Series(range(4), index=["A", "B", "C", "D"]) | ||
|
|
||
| with tm.assert_produces_warning(FutureWarning): | ||
| ser.loc[:3] = 2 | ||
|
|
||
| expected = pd.Series([2, 2, 2, 3], index=["A", "B", "C", "D"]) | ||
| tm.assert_series_equal(ser, expected) | ||
|
|
||
|
|
||
| def test_loc_slice_disallows_positional(): | ||
| # GH#16121, GH#24612, GH#31810 | ||
| dti = pd.date_range("2016-01-01", periods=3) | ||
| df = pd.DataFrame(np.random.random((3, 2)), index=dti) | ||
|
|
||
| ser = df[0] | ||
|
|
||
| msg = ( | ||
| "cannot do slice indexing on DatetimeIndex with these " | ||
| r"indexers \[1\] of type int" | ||
| ) | ||
|
|
||
| for obj in [df, ser]: | ||
| with pytest.raises(TypeError, match=msg): | ||
| obj.loc[1:3] | ||
|
|
||
| with tm.assert_produces_warning(FutureWarning): | ||
| # GH#31840 deprecated incorrect behavior | ||
| obj.loc[1:3] = 1 | ||
|
|
||
| with pytest.raises(TypeError, match=msg): | ||
| df.loc[1:3, 1] | ||
|
|
||
| with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): | ||
|
||
| # GH#31840 deprecated incorrect behavior | ||
| df.loc[1:3, 1] = 2 | ||
Uh oh!
There was an error while loading. Please reload this page.