Skip to content

Commit 0078120

Browse files
authored
TST/REF: collect tests by method (#37430)
1 parent fabf03a commit 0078120

File tree

8 files changed

+113
-122
lines changed

8 files changed

+113
-122
lines changed

pandas/tests/frame/methods/test_reset_index.py

+26
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,32 @@ def test_reset_index_with_drop(
516516
assert len(deleveled.columns) == len(ymd.columns)
517517
assert deleveled.index.name == ymd.index.name
518518

519+
@pytest.mark.parametrize(
520+
"ix_data, exp_data",
521+
[
522+
(
523+
[(pd.NaT, 1), (pd.NaT, 2)],
524+
{"a": [pd.NaT, pd.NaT], "b": [1, 2], "x": [11, 12]},
525+
),
526+
(
527+
[(pd.NaT, 1), (pd.Timestamp("2020-01-01"), 2)],
528+
{"a": [pd.NaT, pd.Timestamp("2020-01-01")], "b": [1, 2], "x": [11, 12]},
529+
),
530+
(
531+
[(pd.NaT, 1), (pd.Timedelta(123, "d"), 2)],
532+
{"a": [pd.NaT, pd.Timedelta(123, "d")], "b": [1, 2], "x": [11, 12]},
533+
),
534+
],
535+
)
536+
def test_reset_index_nat_multiindex(self, ix_data, exp_data):
537+
# GH#36541: that reset_index() does not raise ValueError
538+
ix = MultiIndex.from_tuples(ix_data, names=["a", "b"])
539+
result = DataFrame({"x": [11, 12]}, index=ix)
540+
result = result.reset_index()
541+
542+
expected = DataFrame(exp_data)
543+
tm.assert_frame_equal(result, expected)
544+
519545

520546
@pytest.mark.parametrize(
521547
"array, dtype",

pandas/tests/indexing/test_loc.py

+30
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,36 @@ def test_loc_preserve_names(self, multiindex_year_month_day_dataframe_random_dat
10321032
assert result.index.name == ymd.index.names[2]
10331033
assert result2.index.name == ymd.index.names[2]
10341034

1035+
def test_loc_getitem_multiindex_nonunique_len_zero(self):
1036+
# GH#13691
1037+
mi = MultiIndex.from_product([[0], [1, 1]])
1038+
ser = Series(0, index=mi)
1039+
1040+
res = ser.loc[[]]
1041+
1042+
expected = ser[:0]
1043+
tm.assert_series_equal(res, expected)
1044+
1045+
res2 = ser.loc[ser.iloc[0:0]]
1046+
tm.assert_series_equal(res2, expected)
1047+
1048+
def test_loc_getitem_access_none_value_in_multiindex(self):
1049+
# GH#34318: test that you can access a None value using .loc
1050+
# through a Multiindex
1051+
1052+
ser = Series([None], pd.MultiIndex.from_arrays([["Level1"], ["Level2"]]))
1053+
result = ser.loc[("Level1", "Level2")]
1054+
assert result is None
1055+
1056+
midx = MultiIndex.from_product([["Level1"], ["Level2_a", "Level2_b"]])
1057+
ser = Series([None] * len(midx), dtype=object, index=midx)
1058+
result = ser.loc[("Level1", "Level2_a")]
1059+
assert result is None
1060+
1061+
ser = Series([1] * len(midx), dtype=object, index=midx)
1062+
result = ser.loc[("Level1", "Level2_a")]
1063+
assert result == 1
1064+
10351065

10361066
def test_series_loc_getitem_label_list_missing_values():
10371067
# gh-11428

pandas/tests/series/indexing/test_callable.py

-33
This file was deleted.

pandas/tests/series/indexing/test_getitem.py

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@
1717

1818

1919
class TestSeriesGetitemScalars:
20+
def test_getitem_keyerror_with_int64index(self):
21+
ser = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2])
22+
23+
with pytest.raises(KeyError, match=r"^5$"):
24+
ser[5]
25+
26+
with pytest.raises(KeyError, match=r"^'c'$"):
27+
ser["c"]
28+
29+
# not monotonic
30+
ser = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1])
31+
32+
with pytest.raises(KeyError, match=r"^5$"):
33+
ser[5]
34+
35+
with pytest.raises(KeyError, match=r"^'c'$"):
36+
ser["c"]
37+
38+
def test_getitem_int64(self, datetime_series):
39+
idx = np.int64(5)
40+
assert datetime_series[idx] == datetime_series[5]
2041

2142
# TODO: better name/GH ref?
2243
def test_getitem_regression(self):
@@ -228,6 +249,22 @@ def test_getitem_boolean_different_order(self, string_series):
228249
tm.assert_series_equal(sel, exp)
229250

230251

252+
class TestGetitemCallable:
253+
def test_getitem_callable(self):
254+
# GH#12533
255+
ser = Series(4, index=list("ABCD"))
256+
result = ser[lambda x: "A"]
257+
assert result == ser.loc["A"]
258+
259+
result = ser[lambda x: ["A", "B"]]
260+
expected = ser.loc[["A", "B"]]
261+
tm.assert_series_equal(result, expected)
262+
263+
result = ser[lambda x: [True, False, True, True]]
264+
expected = ser.iloc[[0, 2, 3]]
265+
tm.assert_series_equal(result, expected)
266+
267+
231268
def test_getitem_generator(string_series):
232269
gen = (x > 0 for x in string_series)
233270
result = string_series[gen]

pandas/tests/series/indexing/test_multiindex.py

-65
This file was deleted.

pandas/tests/series/indexing/test_numeric.py

-24
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,3 @@ def test_slice_floats2():
110110
s.index = i
111111
assert len(s.loc[12.0:]) == 8
112112
assert len(s.loc[12.5:]) == 7
113-
114-
115-
def test_int_indexing():
116-
s = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2])
117-
118-
with pytest.raises(KeyError, match=r"^5$"):
119-
s[5]
120-
121-
with pytest.raises(KeyError, match=r"^'c'$"):
122-
s["c"]
123-
124-
# not monotonic
125-
s = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1])
126-
127-
with pytest.raises(KeyError, match=r"^5$"):
128-
s[5]
129-
130-
with pytest.raises(KeyError, match=r"^'c'$"):
131-
s["c"]
132-
133-
134-
def test_getitem_int64(datetime_series):
135-
idx = np.int64(5)
136-
assert datetime_series[idx] == datetime_series[5]

pandas/tests/series/indexing/test_setitem.py

+20
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,23 @@ def test_dt64tz_setitem_does_not_mutate_dti(self):
142142
ser[::3] = NaT
143143
assert ser[0] is NaT
144144
assert dti[0] == ts
145+
146+
147+
class TestSetitemCallable:
148+
def test_setitem_callable_key(self):
149+
# GH#12533
150+
ser = Series([1, 2, 3, 4], index=list("ABCD"))
151+
ser[lambda x: "A"] = -1
152+
153+
expected = Series([-1, 2, 3, 4], index=list("ABCD"))
154+
tm.assert_series_equal(ser, expected)
155+
156+
def test_setitem_callable_other(self):
157+
# GH#13299
158+
inc = lambda x: x + 1
159+
160+
ser = Series([1, 2, -1, 4])
161+
ser[ser < 0] = inc
162+
163+
expected = Series([1, 2, inc, 4])
164+
tm.assert_series_equal(ser, expected)

0 commit comments

Comments
 (0)