Skip to content

Commit c440ee7

Browse files
authored
REF: test_searchorted for PeriodIndex (#33040)
1 parent 90127a8 commit c440ee7

File tree

3 files changed

+179
-172
lines changed

3 files changed

+179
-172
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas._libs.tslibs import IncompatibleFrequency
5+
6+
from pandas import NaT, Period, PeriodIndex, Series, array
7+
import pandas._testing as tm
8+
9+
10+
class TestSearchsorted:
11+
@pytest.mark.parametrize("freq", ["D", "2D"])
12+
def test_searchsorted(self, freq):
13+
pidx = PeriodIndex(
14+
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
15+
freq=freq,
16+
)
17+
18+
p1 = Period("2014-01-01", freq=freq)
19+
assert pidx.searchsorted(p1) == 0
20+
21+
p2 = Period("2014-01-04", freq=freq)
22+
assert pidx.searchsorted(p2) == 3
23+
24+
assert pidx.searchsorted(NaT) == 0
25+
26+
msg = "Input has different freq=H from PeriodArray"
27+
with pytest.raises(IncompatibleFrequency, match=msg):
28+
pidx.searchsorted(Period("2014-01-01", freq="H"))
29+
30+
msg = "Input has different freq=5D from PeriodArray"
31+
with pytest.raises(IncompatibleFrequency, match=msg):
32+
pidx.searchsorted(Period("2014-01-01", freq="5D"))
33+
34+
@pytest.mark.parametrize("klass", [list, np.array, array, Series])
35+
def test_searchsorted_different_argument_classes(self, klass):
36+
pidx = PeriodIndex(
37+
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
38+
freq="D",
39+
)
40+
result = pidx.searchsorted(klass(pidx))
41+
expected = np.arange(len(pidx), dtype=result.dtype)
42+
tm.assert_numpy_array_equal(result, expected)
43+
44+
result = pidx._data.searchsorted(klass(pidx))
45+
tm.assert_numpy_array_equal(result, expected)
46+
47+
def test_searchsorted_invalid(self):
48+
pidx = PeriodIndex(
49+
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
50+
freq="D",
51+
)
52+
53+
other = np.array([0, 1], dtype=np.int64)
54+
55+
msg = "|".join(
56+
[
57+
"searchsorted requires compatible dtype or scalar",
58+
"Unexpected type for 'value'",
59+
]
60+
)
61+
with pytest.raises(TypeError, match=msg):
62+
pidx.searchsorted(other)
63+
64+
with pytest.raises(TypeError, match=msg):
65+
pidx.searchsorted(other.astype("timedelta64[ns]"))
66+
67+
with pytest.raises(TypeError, match=msg):
68+
pidx.searchsorted(np.timedelta64(4))
69+
70+
with pytest.raises(TypeError, match=msg):
71+
pidx.searchsorted(np.timedelta64("NaT", "ms"))
72+
73+
with pytest.raises(TypeError, match=msg):
74+
pidx.searchsorted(np.datetime64(4, "ns"))
75+
76+
with pytest.raises(TypeError, match=msg):
77+
pidx.searchsorted(np.datetime64("NaT", "ns"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from datetime import datetime
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas import (
7+
DatetimeIndex,
8+
NaT,
9+
PeriodIndex,
10+
Timedelta,
11+
Timestamp,
12+
date_range,
13+
period_range,
14+
)
15+
import pandas._testing as tm
16+
17+
18+
class TestToTimestamp:
19+
def test_to_timestamp_freq(self):
20+
idx = period_range("2017", periods=12, freq="A-DEC")
21+
result = idx.to_timestamp()
22+
expected = date_range("2017", periods=12, freq="AS-JAN")
23+
tm.assert_index_equal(result, expected)
24+
25+
def test_to_timestamp_pi_nat(self):
26+
# GH#7228
27+
index = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
28+
29+
result = index.to_timestamp("D")
30+
expected = DatetimeIndex(
31+
[NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name="idx"
32+
)
33+
tm.assert_index_equal(result, expected)
34+
assert result.name == "idx"
35+
36+
result2 = result.to_period(freq="M")
37+
tm.assert_index_equal(result2, index)
38+
assert result2.name == "idx"
39+
40+
result3 = result.to_period(freq="3M")
41+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
42+
tm.assert_index_equal(result3, exp)
43+
assert result3.freqstr == "3M"
44+
45+
msg = "Frequency must be positive, because it represents span: -2A"
46+
with pytest.raises(ValueError, match=msg):
47+
result.to_period(freq="-2A")
48+
49+
def test_to_timestamp_preserve_name(self):
50+
index = period_range(freq="A", start="1/1/2001", end="12/1/2009", name="foo")
51+
assert index.name == "foo"
52+
53+
conv = index.to_timestamp("D")
54+
assert conv.name == "foo"
55+
56+
def test_to_timestamp_quarterly_bug(self):
57+
years = np.arange(1960, 2000).repeat(4)
58+
quarters = np.tile(list(range(1, 5)), 40)
59+
60+
pindex = PeriodIndex(year=years, quarter=quarters)
61+
62+
stamps = pindex.to_timestamp("D", "end")
63+
expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
64+
tm.assert_index_equal(stamps, expected)
65+
66+
def test_to_timestamp_pi_mult(self):
67+
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")
68+
69+
result = idx.to_timestamp()
70+
expected = DatetimeIndex(["2011-01-01", "NaT", "2011-02-01"], name="idx")
71+
tm.assert_index_equal(result, expected)
72+
73+
result = idx.to_timestamp(how="E")
74+
expected = DatetimeIndex(["2011-02-28", "NaT", "2011-03-31"], name="idx")
75+
expected = expected + Timedelta(1, "D") - Timedelta(1, "ns")
76+
tm.assert_index_equal(result, expected)
77+
78+
def test_to_timestamp_pi_combined(self):
79+
idx = period_range(start="2011", periods=2, freq="1D1H", name="idx")
80+
81+
result = idx.to_timestamp()
82+
expected = DatetimeIndex(["2011-01-01 00:00", "2011-01-02 01:00"], name="idx")
83+
tm.assert_index_equal(result, expected)
84+
85+
result = idx.to_timestamp(how="E")
86+
expected = DatetimeIndex(
87+
["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx"
88+
)
89+
expected = expected + Timedelta(1, "s") - Timedelta(1, "ns")
90+
tm.assert_index_equal(result, expected)
91+
92+
result = idx.to_timestamp(how="E", freq="H")
93+
expected = DatetimeIndex(["2011-01-02 00:00", "2011-01-03 01:00"], name="idx")
94+
expected = expected + Timedelta(1, "h") - Timedelta(1, "ns")
95+
tm.assert_index_equal(result, expected)
96+
97+
def test_to_timestamp_1703(self):
98+
index = period_range("1/1/2012", periods=4, freq="D")
99+
100+
result = index.to_timestamp()
101+
assert result[0] == Timestamp("1/1/2012")
+1-172
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
from datetime import datetime
2-
31
import numpy as np
42
import pytest
53

6-
from pandas._libs.tslibs import IncompatibleFrequency
7-
8-
from pandas import (
9-
DatetimeIndex,
10-
NaT,
11-
Period,
12-
PeriodIndex,
13-
Series,
14-
Timedelta,
15-
Timestamp,
16-
array,
17-
date_range,
18-
period_range,
19-
)
4+
from pandas import Period, PeriodIndex, period_range
205
import pandas._testing as tm
216

227

@@ -42,76 +27,6 @@ def test_freq(self, freq):
4227
self._check_freq(freq, "1970-01-01")
4328

4429

45-
class TestSearchsorted:
46-
@pytest.mark.parametrize("freq", ["D", "2D"])
47-
def test_searchsorted(self, freq):
48-
pidx = PeriodIndex(
49-
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
50-
freq=freq,
51-
)
52-
53-
p1 = Period("2014-01-01", freq=freq)
54-
assert pidx.searchsorted(p1) == 0
55-
56-
p2 = Period("2014-01-04", freq=freq)
57-
assert pidx.searchsorted(p2) == 3
58-
59-
assert pidx.searchsorted(NaT) == 0
60-
61-
msg = "Input has different freq=H from PeriodArray"
62-
with pytest.raises(IncompatibleFrequency, match=msg):
63-
pidx.searchsorted(Period("2014-01-01", freq="H"))
64-
65-
msg = "Input has different freq=5D from PeriodArray"
66-
with pytest.raises(IncompatibleFrequency, match=msg):
67-
pidx.searchsorted(Period("2014-01-01", freq="5D"))
68-
69-
@pytest.mark.parametrize("klass", [list, np.array, array, Series])
70-
def test_searchsorted_different_argument_classes(self, klass):
71-
pidx = PeriodIndex(
72-
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
73-
freq="D",
74-
)
75-
result = pidx.searchsorted(klass(pidx))
76-
expected = np.arange(len(pidx), dtype=result.dtype)
77-
tm.assert_numpy_array_equal(result, expected)
78-
79-
result = pidx._data.searchsorted(klass(pidx))
80-
tm.assert_numpy_array_equal(result, expected)
81-
82-
def test_searchsorted_invalid(self):
83-
pidx = PeriodIndex(
84-
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
85-
freq="D",
86-
)
87-
88-
other = np.array([0, 1], dtype=np.int64)
89-
90-
msg = "|".join(
91-
[
92-
"searchsorted requires compatible dtype or scalar",
93-
"Unexpected type for 'value'",
94-
]
95-
)
96-
with pytest.raises(TypeError, match=msg):
97-
pidx.searchsorted(other)
98-
99-
with pytest.raises(TypeError, match=msg):
100-
pidx.searchsorted(other.astype("timedelta64[ns]"))
101-
102-
with pytest.raises(TypeError, match=msg):
103-
pidx.searchsorted(np.timedelta64(4))
104-
105-
with pytest.raises(TypeError, match=msg):
106-
pidx.searchsorted(np.timedelta64("NaT", "ms"))
107-
108-
with pytest.raises(TypeError, match=msg):
109-
pidx.searchsorted(np.datetime64(4, "ns"))
110-
111-
with pytest.raises(TypeError, match=msg):
112-
pidx.searchsorted(np.datetime64("NaT", "ns"))
113-
114-
11530
class TestPeriodIndexConversion:
11631
def test_tolist(self):
11732
index = period_range(freq="A", start="1/1/2001", end="12/1/2009")
@@ -121,89 +36,3 @@ def test_tolist(self):
12136

12237
recon = PeriodIndex(rs)
12338
tm.assert_index_equal(index, recon)
124-
125-
126-
class TestToTimestamp:
127-
def test_to_timestamp_freq(self):
128-
idx = period_range("2017", periods=12, freq="A-DEC")
129-
result = idx.to_timestamp()
130-
expected = date_range("2017", periods=12, freq="AS-JAN")
131-
tm.assert_index_equal(result, expected)
132-
133-
def test_to_timestamp_pi_nat(self):
134-
# GH#7228
135-
index = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
136-
137-
result = index.to_timestamp("D")
138-
expected = DatetimeIndex(
139-
[NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name="idx"
140-
)
141-
tm.assert_index_equal(result, expected)
142-
assert result.name == "idx"
143-
144-
result2 = result.to_period(freq="M")
145-
tm.assert_index_equal(result2, index)
146-
assert result2.name == "idx"
147-
148-
result3 = result.to_period(freq="3M")
149-
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
150-
tm.assert_index_equal(result3, exp)
151-
assert result3.freqstr == "3M"
152-
153-
msg = "Frequency must be positive, because it represents span: -2A"
154-
with pytest.raises(ValueError, match=msg):
155-
result.to_period(freq="-2A")
156-
157-
def test_to_timestamp_preserve_name(self):
158-
index = period_range(freq="A", start="1/1/2001", end="12/1/2009", name="foo")
159-
assert index.name == "foo"
160-
161-
conv = index.to_timestamp("D")
162-
assert conv.name == "foo"
163-
164-
def test_to_timestamp_quarterly_bug(self):
165-
years = np.arange(1960, 2000).repeat(4)
166-
quarters = np.tile(list(range(1, 5)), 40)
167-
168-
pindex = PeriodIndex(year=years, quarter=quarters)
169-
170-
stamps = pindex.to_timestamp("D", "end")
171-
expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
172-
tm.assert_index_equal(stamps, expected)
173-
174-
def test_to_timestamp_pi_mult(self):
175-
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")
176-
177-
result = idx.to_timestamp()
178-
expected = DatetimeIndex(["2011-01-01", "NaT", "2011-02-01"], name="idx")
179-
tm.assert_index_equal(result, expected)
180-
181-
result = idx.to_timestamp(how="E")
182-
expected = DatetimeIndex(["2011-02-28", "NaT", "2011-03-31"], name="idx")
183-
expected = expected + Timedelta(1, "D") - Timedelta(1, "ns")
184-
tm.assert_index_equal(result, expected)
185-
186-
def test_to_timestamp_pi_combined(self):
187-
idx = period_range(start="2011", periods=2, freq="1D1H", name="idx")
188-
189-
result = idx.to_timestamp()
190-
expected = DatetimeIndex(["2011-01-01 00:00", "2011-01-02 01:00"], name="idx")
191-
tm.assert_index_equal(result, expected)
192-
193-
result = idx.to_timestamp(how="E")
194-
expected = DatetimeIndex(
195-
["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx"
196-
)
197-
expected = expected + Timedelta(1, "s") - Timedelta(1, "ns")
198-
tm.assert_index_equal(result, expected)
199-
200-
result = idx.to_timestamp(how="E", freq="H")
201-
expected = DatetimeIndex(["2011-01-02 00:00", "2011-01-03 01:00"], name="idx")
202-
expected = expected + Timedelta(1, "h") - Timedelta(1, "ns")
203-
tm.assert_index_equal(result, expected)
204-
205-
def test_to_timestamp_1703(self):
206-
index = period_range("1/1/2012", periods=4, freq="D")
207-
208-
result = index.to_timestamp()
209-
assert result[0] == Timestamp("1/1/2012")

0 commit comments

Comments
 (0)