Skip to content

Commit bde9b11

Browse files
jbrockmendelrhshadrach
authored andcommitted
TST/REF: collect/de-dup index tests (pandas-dev#43914)
1 parent d4ae657 commit bde9b11

File tree

8 files changed

+115
-232
lines changed

8 files changed

+115
-232
lines changed

pandas/tests/frame/methods/test_reindex.py

+32
Original file line numberDiff line numberDiff line change
@@ -1078,3 +1078,35 @@ def test_reindex_datetimelike_to_object(self, dtype):
10781078
assert res.iloc[-1, 0] is fv
10791079
assert res.iloc[-1, 1] is fv
10801080
tm.assert_frame_equal(res, expected)
1081+
1082+
@pytest.mark.parametrize(
1083+
"index_df,index_res,index_exp",
1084+
[
1085+
(
1086+
CategoricalIndex([], categories=["A"]),
1087+
Index(["A"]),
1088+
Index(["A"]),
1089+
),
1090+
(
1091+
CategoricalIndex([], categories=["A"]),
1092+
Index(["B"]),
1093+
Index(["B"]),
1094+
),
1095+
(
1096+
CategoricalIndex([], categories=["A"]),
1097+
CategoricalIndex(["A"]),
1098+
CategoricalIndex(["A"]),
1099+
),
1100+
(
1101+
CategoricalIndex([], categories=["A"]),
1102+
CategoricalIndex(["B"]),
1103+
CategoricalIndex(["B"]),
1104+
),
1105+
],
1106+
)
1107+
def test_reindex_not_category(self, index_df, index_res, index_exp):
1108+
# GH#28690
1109+
df = DataFrame(index=index_df)
1110+
result = df.reindex(index=index_res)
1111+
expected = DataFrame(index=index_exp)
1112+
tm.assert_frame_equal(result, expected)

pandas/tests/indexes/categorical/test_reindex.py

-42
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import numpy as np
2-
import pytest
32

43
from pandas import (
54
Categorical,
65
CategoricalIndex,
7-
DataFrame,
86
Index,
97
Interval,
10-
Series,
118
)
129
import pandas._testing as tm
1310

@@ -66,45 +63,6 @@ def test_reindex_empty_index(self):
6663
tm.assert_index_equal(res, Index(["a", "b"]), exact=True)
6764
tm.assert_numpy_array_equal(indexer, np.array([-1, -1], dtype=np.intp))
6865

69-
def test_reindex_missing_category(self):
70-
# GH: 18185
71-
ser = Series([1, 2, 3, 1], dtype="category")
72-
msg = r"Cannot setitem on a Categorical with a new category \(-1\)"
73-
with pytest.raises(TypeError, match=msg):
74-
ser.reindex([1, 2, 3, 4, 5], fill_value=-1)
75-
76-
@pytest.mark.parametrize(
77-
"index_df,index_res,index_exp",
78-
[
79-
(
80-
CategoricalIndex([], categories=["A"]),
81-
Index(["A"]),
82-
Index(["A"]),
83-
),
84-
(
85-
CategoricalIndex([], categories=["A"]),
86-
Index(["B"]),
87-
Index(["B"]),
88-
),
89-
(
90-
CategoricalIndex([], categories=["A"]),
91-
CategoricalIndex(["A"]),
92-
CategoricalIndex(["A"]),
93-
),
94-
(
95-
CategoricalIndex([], categories=["A"]),
96-
CategoricalIndex(["B"]),
97-
CategoricalIndex(["B"]),
98-
),
99-
],
100-
)
101-
def test_reindex_not_category(self, index_df, index_res, index_exp):
102-
# GH: 28690
103-
df = DataFrame(index=index_df)
104-
result = df.reindex(index=index_res)
105-
expected = DataFrame(index=index_exp)
106-
tm.assert_frame_equal(result, expected)
107-
10866
def test_reindex_categorical_added_category(self):
10967
# GH 42424
11068
ci = CategoricalIndex(

pandas/tests/indexes/datetimes/test_date_range.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,41 @@ def test_date_range_timestamp_equiv_preserve_frequency(self):
121121

122122

123123
class TestDateRanges:
124+
@pytest.mark.parametrize("freq", ["N", "U", "L", "T", "S", "H", "D"])
125+
def test_date_range_edges(self, freq):
126+
# GH#13672
127+
td = Timedelta(f"1{freq}")
128+
ts = Timestamp("1970-01-01")
129+
130+
idx = date_range(
131+
start=ts + td,
132+
end=ts + 4 * td,
133+
freq=freq,
134+
)
135+
exp = DatetimeIndex(
136+
[ts + n * td for n in range(1, 5)],
137+
freq=freq,
138+
)
139+
tm.assert_index_equal(idx, exp)
140+
141+
# start after end
142+
idx = date_range(
143+
start=ts + 4 * td,
144+
end=ts + td,
145+
freq=freq,
146+
)
147+
exp = DatetimeIndex([], freq=freq)
148+
tm.assert_index_equal(idx, exp)
149+
150+
# start matches end
151+
idx = date_range(
152+
start=ts + td,
153+
end=ts + td,
154+
freq=freq,
155+
)
156+
exp = DatetimeIndex([ts + td], freq=freq)
157+
tm.assert_index_equal(idx, exp)
158+
124159
def test_date_range_near_implementation_bound(self):
125160
# GH#???
126161
freq = Timedelta(1)
@@ -717,7 +752,7 @@ def test_timezone_comparaison_bug(self):
717752
result = date_range(start, periods=2, tz="US/Eastern")
718753
assert len(result) == 2
719754

720-
def test_timezone_comparaison_assert(self):
755+
def test_timezone_comparison_assert(self):
721756
start = Timestamp("20130220 10:00", tz="US/Eastern")
722757
msg = "Inferred time zone not equal to passed time zone"
723758
with pytest.raises(AssertionError, match=msg):

pandas/tests/indexes/datetimes/test_misc.py

-136
Original file line numberDiff line numberDiff line change
@@ -19,142 +19,6 @@
1919
from pandas.core.arrays import DatetimeArray
2020

2121

22-
class TestTimeSeries:
23-
def test_range_edges(self):
24-
# GH#13672
25-
idx = date_range(
26-
start=Timestamp("1970-01-01 00:00:00.000000001"),
27-
end=Timestamp("1970-01-01 00:00:00.000000004"),
28-
freq="N",
29-
)
30-
exp = DatetimeIndex(
31-
[
32-
"1970-01-01 00:00:00.000000001",
33-
"1970-01-01 00:00:00.000000002",
34-
"1970-01-01 00:00:00.000000003",
35-
"1970-01-01 00:00:00.000000004",
36-
],
37-
freq="N",
38-
)
39-
tm.assert_index_equal(idx, exp)
40-
41-
def test_range_edges2(self):
42-
43-
idx = date_range(
44-
start=Timestamp("1970-01-01 00:00:00.000000004"),
45-
end=Timestamp("1970-01-01 00:00:00.000000001"),
46-
freq="N",
47-
)
48-
exp = DatetimeIndex([], freq="N")
49-
tm.assert_index_equal(idx, exp)
50-
51-
def test_range_edges3(self):
52-
53-
idx = date_range(
54-
start=Timestamp("1970-01-01 00:00:00.000000001"),
55-
end=Timestamp("1970-01-01 00:00:00.000000001"),
56-
freq="N",
57-
)
58-
exp = DatetimeIndex(["1970-01-01 00:00:00.000000001"], freq="N")
59-
tm.assert_index_equal(idx, exp)
60-
61-
def test_range_edges4(self):
62-
63-
idx = date_range(
64-
start=Timestamp("1970-01-01 00:00:00.000001"),
65-
end=Timestamp("1970-01-01 00:00:00.000004"),
66-
freq="U",
67-
)
68-
exp = DatetimeIndex(
69-
[
70-
"1970-01-01 00:00:00.000001",
71-
"1970-01-01 00:00:00.000002",
72-
"1970-01-01 00:00:00.000003",
73-
"1970-01-01 00:00:00.000004",
74-
],
75-
freq="U",
76-
)
77-
tm.assert_index_equal(idx, exp)
78-
79-
def test_range_edges5(self):
80-
81-
idx = date_range(
82-
start=Timestamp("1970-01-01 00:00:00.001"),
83-
end=Timestamp("1970-01-01 00:00:00.004"),
84-
freq="L",
85-
)
86-
exp = DatetimeIndex(
87-
[
88-
"1970-01-01 00:00:00.001",
89-
"1970-01-01 00:00:00.002",
90-
"1970-01-01 00:00:00.003",
91-
"1970-01-01 00:00:00.004",
92-
],
93-
freq="L",
94-
)
95-
tm.assert_index_equal(idx, exp)
96-
97-
def test_range_edges6(self):
98-
idx = date_range(
99-
start=Timestamp("1970-01-01 00:00:01"),
100-
end=Timestamp("1970-01-01 00:00:04"),
101-
freq="S",
102-
)
103-
exp = DatetimeIndex(
104-
[
105-
"1970-01-01 00:00:01",
106-
"1970-01-01 00:00:02",
107-
"1970-01-01 00:00:03",
108-
"1970-01-01 00:00:04",
109-
],
110-
freq="S",
111-
)
112-
tm.assert_index_equal(idx, exp)
113-
114-
def test_range_edges7(self):
115-
idx = date_range(
116-
start=Timestamp("1970-01-01 00:01"),
117-
end=Timestamp("1970-01-01 00:04"),
118-
freq="T",
119-
)
120-
exp = DatetimeIndex(
121-
[
122-
"1970-01-01 00:01",
123-
"1970-01-01 00:02",
124-
"1970-01-01 00:03",
125-
"1970-01-01 00:04",
126-
],
127-
freq="T",
128-
)
129-
tm.assert_index_equal(idx, exp)
130-
131-
def test_range_edges8(self):
132-
idx = date_range(
133-
start=Timestamp("1970-01-01 01:00"),
134-
end=Timestamp("1970-01-01 04:00"),
135-
freq="H",
136-
)
137-
exp = DatetimeIndex(
138-
[
139-
"1970-01-01 01:00",
140-
"1970-01-01 02:00",
141-
"1970-01-01 03:00",
142-
"1970-01-01 04:00",
143-
],
144-
freq="H",
145-
)
146-
tm.assert_index_equal(idx, exp)
147-
148-
def test_range_edges9(self):
149-
idx = date_range(
150-
start=Timestamp("1970-01-01"), end=Timestamp("1970-01-04"), freq="D"
151-
)
152-
exp = DatetimeIndex(
153-
["1970-01-01", "1970-01-02", "1970-01-03", "1970-01-04"], freq="D"
154-
)
155-
tm.assert_index_equal(idx, exp)
156-
157-
15822
class TestDatetime64:
15923
def test_no_millisecond_field(self):
16024
msg = "type object 'DatetimeIndex' has no attribute 'millisecond'"

pandas/tests/indexes/period/test_partial_slicing.py

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas import (
55
DataFrame,
6+
PeriodIndex,
67
Series,
78
date_range,
89
period_range,
@@ -11,6 +12,31 @@
1112

1213

1314
class TestPeriodIndex:
15+
def test_getitem_periodindex_duplicates_string_slice(self):
16+
# monotonic
17+
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="A-JUN")
18+
ts = Series(np.random.randn(len(idx)), index=idx)
19+
20+
result = ts["2007"]
21+
expected = ts[1:3]
22+
tm.assert_series_equal(result, expected)
23+
result[:] = 1
24+
assert (ts[1:3] == 1).all()
25+
26+
# not monotonic
27+
idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq="A-JUN")
28+
ts = Series(np.random.randn(len(idx)), index=idx)
29+
30+
result = ts["2007"]
31+
expected = ts[idx == "2007"]
32+
tm.assert_series_equal(result, expected)
33+
34+
def test_getitem_periodindex_quarter_string(self):
35+
pi = PeriodIndex(["2Q05", "3Q05", "4Q05", "1Q06", "2Q06"], freq="Q")
36+
ser = Series(np.random.rand(len(pi)), index=pi).cumsum()
37+
# Todo: fix these accessors!
38+
assert ser["05Q4"] == ser[2]
39+
1440
def test_pindex_slice_index(self):
1541
pi = period_range(start="1/1/10", end="12/31/12", freq="M")
1642
s = Series(np.random.rand(len(pi)), index=pi)

pandas/tests/indexes/period/test_period.py

-25
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,6 @@ def test_is_(self):
245245
assert not index.is_(index - 2)
246246
assert not index.is_(index - 0)
247247

248-
def test_index_duplicate_periods(self):
249-
# monotonic
250-
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="A-JUN")
251-
ts = Series(np.random.randn(len(idx)), index=idx)
252-
253-
result = ts["2007"]
254-
expected = ts[1:3]
255-
tm.assert_series_equal(result, expected)
256-
result[:] = 1
257-
assert (ts[1:3] == 1).all()
258-
259-
# not monotonic
260-
idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq="A-JUN")
261-
ts = Series(np.random.randn(len(idx)), index=idx)
262-
263-
result = ts["2007"]
264-
expected = ts[idx == "2007"]
265-
tm.assert_series_equal(result, expected)
266-
267248
def test_index_unique(self):
268249
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="A-JUN")
269250
expected = PeriodIndex([2000, 2007, 2009], freq="A-JUN")
@@ -292,12 +273,6 @@ def test_pindex_fieldaccessor_nat(self):
292273
exp = Index([1, 2, -1, 3, 4], dtype=np.int64, name="name")
293274
tm.assert_index_equal(idx.month, exp)
294275

295-
def test_pindex_qaccess(self):
296-
pi = PeriodIndex(["2Q05", "3Q05", "4Q05", "1Q06", "2Q06"], freq="Q")
297-
s = Series(np.random.rand(len(pi)), index=pi).cumsum()
298-
# Todo: fix these accessors!
299-
assert s["05Q4"] == s[2]
300-
301276
def test_pindex_multiples(self):
302277
expected = PeriodIndex(
303278
["2011-01", "2011-03", "2011-05", "2011-07", "2011-09", "2011-11"],

0 commit comments

Comments
 (0)