Skip to content

Commit b310ed9

Browse files
authored
REF: organize Series indexing tests (#31557)
1 parent 84c01bc commit b310ed9

12 files changed

+679
-665
lines changed

pandas/tests/series/indexing/test_boolean.py

+1-493
Large diffs are not rendered by default.

pandas/tests/series/indexing/test_datetime.py

-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def test_frame_datetime64_duplicated():
148148

149149
def test_getitem_setitem_datetime_tz_pytz():
150150
from pytz import timezone as tz
151-
from pandas import date_range
152151

153152
N = 50
154153
# testing with timezone, GH #2785
@@ -189,8 +188,6 @@ def test_getitem_setitem_datetime_tz_dateutil():
189188
lambda x: tzutc() if x == "UTC" else gettz(x)
190189
) # handle special case for utc in dateutil
191190

192-
from pandas import date_range
193-
194191
N = 50
195192

196193
# testing with timezone, GH #2785
@@ -373,7 +370,6 @@ def test_getitem_median_slice_bug():
373370

374371

375372
def test_datetime_indexing():
376-
from pandas import date_range
377373

378374
index = date_range("1/1/2000", "1/7/2000")
379375
index = index.repeat(3)
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import numpy as np
2+
3+
import pandas as pd
4+
from pandas import Series
5+
6+
7+
def test_get():
8+
# GH 6383
9+
s = Series(
10+
np.array(
11+
[
12+
43,
13+
48,
14+
60,
15+
48,
16+
50,
17+
51,
18+
50,
19+
45,
20+
57,
21+
48,
22+
56,
23+
45,
24+
51,
25+
39,
26+
55,
27+
43,
28+
54,
29+
52,
30+
51,
31+
54,
32+
]
33+
)
34+
)
35+
36+
result = s.get(25, 0)
37+
expected = 0
38+
assert result == expected
39+
40+
s = Series(
41+
np.array(
42+
[
43+
43,
44+
48,
45+
60,
46+
48,
47+
50,
48+
51,
49+
50,
50+
45,
51+
57,
52+
48,
53+
56,
54+
45,
55+
51,
56+
39,
57+
55,
58+
43,
59+
54,
60+
52,
61+
51,
62+
54,
63+
]
64+
),
65+
index=pd.Float64Index(
66+
[
67+
25.0,
68+
36.0,
69+
49.0,
70+
64.0,
71+
81.0,
72+
100.0,
73+
121.0,
74+
144.0,
75+
169.0,
76+
196.0,
77+
1225.0,
78+
1296.0,
79+
1369.0,
80+
1444.0,
81+
1521.0,
82+
1600.0,
83+
1681.0,
84+
1764.0,
85+
1849.0,
86+
1936.0,
87+
]
88+
),
89+
)
90+
91+
result = s.get(25, 0)
92+
expected = 43
93+
assert result == expected
94+
95+
# GH 7407
96+
# with a boolean accessor
97+
df = pd.DataFrame({"i": [0] * 3, "b": [False] * 3})
98+
vc = df.i.value_counts()
99+
result = vc.get(99, default="Missing")
100+
assert result == "Missing"
101+
102+
vc = df.b.value_counts()
103+
result = vc.get(False, default="Missing")
104+
assert result == 3
105+
106+
result = vc.get(True, default="Missing")
107+
assert result == "Missing"
108+
109+
110+
def test_get_nan():
111+
# GH 8569
112+
s = pd.Float64Index(range(10)).to_series()
113+
assert s.get(np.nan) is None
114+
assert s.get(np.nan, default="Missing") == "Missing"
115+
116+
117+
def test_get_nan_multiple():
118+
# GH 8569
119+
# ensure that fixing "test_get_nan" above hasn't broken get
120+
# with multiple elements
121+
s = pd.Float64Index(range(10)).to_series()
122+
123+
idx = [2, 30]
124+
assert s.get(idx) is None
125+
126+
idx = [2, np.nan]
127+
assert s.get(idx) is None
128+
129+
# GH 17295 - all missing keys
130+
idx = [20, 30]
131+
assert s.get(idx) is None
132+
133+
idx = [np.nan, np.nan]
134+
assert s.get(idx) is None

pandas/tests/series/indexing/test_indexing.py

-35
Original file line numberDiff line numberDiff line change
@@ -883,41 +883,6 @@ def test_pop():
883883
tm.assert_series_equal(k, expected)
884884

885885

886-
def test_take():
887-
s = Series([-1, 5, 6, 2, 4])
888-
889-
actual = s.take([1, 3, 4])
890-
expected = Series([5, 2, 4], index=[1, 3, 4])
891-
tm.assert_series_equal(actual, expected)
892-
893-
actual = s.take([-1, 3, 4])
894-
expected = Series([4, 2, 4], index=[4, 3, 4])
895-
tm.assert_series_equal(actual, expected)
896-
897-
msg = "index {} is out of bounds for( axis 0 with)? size 5"
898-
with pytest.raises(IndexError, match=msg.format(10)):
899-
s.take([1, 10])
900-
with pytest.raises(IndexError, match=msg.format(5)):
901-
s.take([2, 5])
902-
903-
904-
def test_take_categorical():
905-
# https://github.com/pandas-dev/pandas/issues/20664
906-
s = Series(pd.Categorical(["a", "b", "c"]))
907-
result = s.take([-2, -2, 0])
908-
expected = Series(
909-
pd.Categorical(["b", "b", "a"], categories=["a", "b", "c"]), index=[1, 1, 0]
910-
)
911-
tm.assert_series_equal(result, expected)
912-
913-
914-
def test_head_tail(string_series):
915-
tm.assert_series_equal(string_series.head(), string_series[:5])
916-
tm.assert_series_equal(string_series.head(0), string_series[0:0])
917-
tm.assert_series_equal(string_series.tail(), string_series[-5:])
918-
tm.assert_series_equal(string_series.tail(0), string_series[0:0])
919-
920-
921886
def test_uint_drop(any_int_dtype):
922887
# see GH18311
923888
# assigning series.loc[0] = 4 changed series.dtype to int
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Series
5+
import pandas._testing as tm
6+
7+
8+
def test_mask():
9+
# compare with tested results in test_where
10+
s = Series(np.random.randn(5))
11+
cond = s > 0
12+
13+
rs = s.where(~cond, np.nan)
14+
tm.assert_series_equal(rs, s.mask(cond))
15+
16+
rs = s.where(~cond)
17+
rs2 = s.mask(cond)
18+
tm.assert_series_equal(rs, rs2)
19+
20+
rs = s.where(~cond, -s)
21+
rs2 = s.mask(cond, -s)
22+
tm.assert_series_equal(rs, rs2)
23+
24+
cond = Series([True, False, False, True, False], index=s.index)
25+
s2 = -(s.abs())
26+
rs = s2.where(~cond[:3])
27+
rs2 = s2.mask(cond[:3])
28+
tm.assert_series_equal(rs, rs2)
29+
30+
rs = s2.where(~cond[:3], -s2)
31+
rs2 = s2.mask(cond[:3], -s2)
32+
tm.assert_series_equal(rs, rs2)
33+
34+
msg = "Array conditional must be same shape as self"
35+
with pytest.raises(ValueError, match=msg):
36+
s.mask(1)
37+
with pytest.raises(ValueError, match=msg):
38+
s.mask(cond[:3].values, -s)
39+
40+
# dtype changes
41+
s = Series([1, 2, 3, 4])
42+
result = s.mask(s > 2, np.nan)
43+
expected = Series([1, 2, np.nan, np.nan])
44+
tm.assert_series_equal(result, expected)
45+
46+
# see gh-21891
47+
s = Series([1, 2])
48+
res = s.mask([True, False])
49+
50+
exp = Series([np.nan, 2])
51+
tm.assert_series_equal(res, exp)
52+
53+
54+
def test_mask_inplace():
55+
s = Series(np.random.randn(5))
56+
cond = s > 0
57+
58+
rs = s.copy()
59+
rs.mask(cond, inplace=True)
60+
tm.assert_series_equal(rs.dropna(), s[~cond])
61+
tm.assert_series_equal(rs, s.mask(cond))
62+
63+
rs = s.copy()
64+
rs.mask(cond, -s, inplace=True)
65+
tm.assert_series_equal(rs, s.mask(cond, -s))

0 commit comments

Comments
 (0)