-
-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REF: organize Series indexing tests (#31557)
- Loading branch information
1 parent
84c01bc
commit b310ed9
Showing
12 changed files
with
679 additions
and
665 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import numpy as np | ||
|
||
import pandas as pd | ||
from pandas import Series | ||
|
||
|
||
def test_get(): | ||
# GH 6383 | ||
s = Series( | ||
np.array( | ||
[ | ||
43, | ||
48, | ||
60, | ||
48, | ||
50, | ||
51, | ||
50, | ||
45, | ||
57, | ||
48, | ||
56, | ||
45, | ||
51, | ||
39, | ||
55, | ||
43, | ||
54, | ||
52, | ||
51, | ||
54, | ||
] | ||
) | ||
) | ||
|
||
result = s.get(25, 0) | ||
expected = 0 | ||
assert result == expected | ||
|
||
s = Series( | ||
np.array( | ||
[ | ||
43, | ||
48, | ||
60, | ||
48, | ||
50, | ||
51, | ||
50, | ||
45, | ||
57, | ||
48, | ||
56, | ||
45, | ||
51, | ||
39, | ||
55, | ||
43, | ||
54, | ||
52, | ||
51, | ||
54, | ||
] | ||
), | ||
index=pd.Float64Index( | ||
[ | ||
25.0, | ||
36.0, | ||
49.0, | ||
64.0, | ||
81.0, | ||
100.0, | ||
121.0, | ||
144.0, | ||
169.0, | ||
196.0, | ||
1225.0, | ||
1296.0, | ||
1369.0, | ||
1444.0, | ||
1521.0, | ||
1600.0, | ||
1681.0, | ||
1764.0, | ||
1849.0, | ||
1936.0, | ||
] | ||
), | ||
) | ||
|
||
result = s.get(25, 0) | ||
expected = 43 | ||
assert result == expected | ||
|
||
# GH 7407 | ||
# with a boolean accessor | ||
df = pd.DataFrame({"i": [0] * 3, "b": [False] * 3}) | ||
vc = df.i.value_counts() | ||
result = vc.get(99, default="Missing") | ||
assert result == "Missing" | ||
|
||
vc = df.b.value_counts() | ||
result = vc.get(False, default="Missing") | ||
assert result == 3 | ||
|
||
result = vc.get(True, default="Missing") | ||
assert result == "Missing" | ||
|
||
|
||
def test_get_nan(): | ||
# GH 8569 | ||
s = pd.Float64Index(range(10)).to_series() | ||
assert s.get(np.nan) is None | ||
assert s.get(np.nan, default="Missing") == "Missing" | ||
|
||
|
||
def test_get_nan_multiple(): | ||
# GH 8569 | ||
# ensure that fixing "test_get_nan" above hasn't broken get | ||
# with multiple elements | ||
s = pd.Float64Index(range(10)).to_series() | ||
|
||
idx = [2, 30] | ||
assert s.get(idx) is None | ||
|
||
idx = [2, np.nan] | ||
assert s.get(idx) is None | ||
|
||
# GH 17295 - all missing keys | ||
idx = [20, 30] | ||
assert s.get(idx) is None | ||
|
||
idx = [np.nan, np.nan] | ||
assert s.get(idx) is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import Series | ||
import pandas._testing as tm | ||
|
||
|
||
def test_mask(): | ||
# compare with tested results in test_where | ||
s = Series(np.random.randn(5)) | ||
cond = s > 0 | ||
|
||
rs = s.where(~cond, np.nan) | ||
tm.assert_series_equal(rs, s.mask(cond)) | ||
|
||
rs = s.where(~cond) | ||
rs2 = s.mask(cond) | ||
tm.assert_series_equal(rs, rs2) | ||
|
||
rs = s.where(~cond, -s) | ||
rs2 = s.mask(cond, -s) | ||
tm.assert_series_equal(rs, rs2) | ||
|
||
cond = Series([True, False, False, True, False], index=s.index) | ||
s2 = -(s.abs()) | ||
rs = s2.where(~cond[:3]) | ||
rs2 = s2.mask(cond[:3]) | ||
tm.assert_series_equal(rs, rs2) | ||
|
||
rs = s2.where(~cond[:3], -s2) | ||
rs2 = s2.mask(cond[:3], -s2) | ||
tm.assert_series_equal(rs, rs2) | ||
|
||
msg = "Array conditional must be same shape as self" | ||
with pytest.raises(ValueError, match=msg): | ||
s.mask(1) | ||
with pytest.raises(ValueError, match=msg): | ||
s.mask(cond[:3].values, -s) | ||
|
||
# dtype changes | ||
s = Series([1, 2, 3, 4]) | ||
result = s.mask(s > 2, np.nan) | ||
expected = Series([1, 2, np.nan, np.nan]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# see gh-21891 | ||
s = Series([1, 2]) | ||
res = s.mask([True, False]) | ||
|
||
exp = Series([np.nan, 2]) | ||
tm.assert_series_equal(res, exp) | ||
|
||
|
||
def test_mask_inplace(): | ||
s = Series(np.random.randn(5)) | ||
cond = s > 0 | ||
|
||
rs = s.copy() | ||
rs.mask(cond, inplace=True) | ||
tm.assert_series_equal(rs.dropna(), s[~cond]) | ||
tm.assert_series_equal(rs, s.mask(cond)) | ||
|
||
rs = s.copy() | ||
rs.mask(cond, -s, inplace=True) | ||
tm.assert_series_equal(rs, s.mask(cond, -s)) |
Oops, something went wrong.