Skip to content

Commit a8817ba

Browse files
authored
REF/TST: misplaced tests in tests.indexes.period (#31758)
1 parent c389222 commit a8817ba

17 files changed

+619
-591
lines changed

pandas/tests/indexes/datetimes/test_astype.py

+10-73
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22

33
import dateutil
4-
from dateutil.tz import tzlocal
54
import numpy as np
65
import pytest
76
import pytz
@@ -12,7 +11,7 @@
1211
Index,
1312
Int64Index,
1413
NaT,
15-
Period,
14+
PeriodIndex,
1615
Series,
1716
Timestamp,
1817
date_range,
@@ -278,81 +277,19 @@ def test_integer_index_astype_datetime(self, tz, dtype):
278277
expected = pd.DatetimeIndex(["2018-01-01"], tz=tz)
279278
tm.assert_index_equal(result, expected)
280279

280+
def test_dti_astype_period(self):
281+
idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx")
281282

282-
class TestToPeriod:
283-
def setup_method(self, method):
284-
data = [
285-
Timestamp("2007-01-01 10:11:12.123456Z"),
286-
Timestamp("2007-01-01 10:11:13.789123Z"),
287-
]
288-
self.index = DatetimeIndex(data)
289-
290-
def test_to_period_millisecond(self):
291-
index = self.index
292-
293-
with tm.assert_produces_warning(UserWarning):
294-
# warning that timezone info will be lost
295-
period = index.to_period(freq="L")
296-
assert 2 == len(period)
297-
assert period[0] == Period("2007-01-01 10:11:12.123Z", "L")
298-
assert period[1] == Period("2007-01-01 10:11:13.789Z", "L")
299-
300-
def test_to_period_microsecond(self):
301-
index = self.index
283+
res = idx.astype("period[M]")
284+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
285+
tm.assert_index_equal(res, exp)
302286

303-
with tm.assert_produces_warning(UserWarning):
304-
# warning that timezone info will be lost
305-
period = index.to_period(freq="U")
306-
assert 2 == len(period)
307-
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "U")
308-
assert period[1] == Period("2007-01-01 10:11:13.789123Z", "U")
309-
310-
@pytest.mark.parametrize(
311-
"tz",
312-
["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()],
313-
)
314-
def test_to_period_tz(self, tz):
315-
ts = date_range("1/1/2000", "2/1/2000", tz=tz)
316-
317-
with tm.assert_produces_warning(UserWarning):
318-
# GH#21333 warning that timezone info will be lost
319-
result = ts.to_period()[0]
320-
expected = ts[0].to_period()
321-
322-
assert result == expected
323-
324-
expected = date_range("1/1/2000", "2/1/2000").to_period()
325-
326-
with tm.assert_produces_warning(UserWarning):
327-
# GH#21333 warning that timezone info will be lost
328-
result = ts.to_period()
329-
330-
tm.assert_index_equal(result, expected)
287+
res = idx.astype("period[3M]")
288+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
289+
tm.assert_index_equal(res, exp)
331290

332-
@pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"])
333-
def test_to_period_tz_utc_offset_consistency(self, tz):
334-
# GH 22905
335-
ts = pd.date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
336-
with tm.assert_produces_warning(UserWarning):
337-
result = ts.to_period()[0]
338-
expected = ts[0].to_period()
339-
assert result == expected
340-
341-
def test_to_period_nofreq(self):
342-
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"])
343-
with pytest.raises(ValueError):
344-
idx.to_period()
345-
346-
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer")
347-
assert idx.freqstr == "D"
348-
expected = pd.PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D")
349-
tm.assert_index_equal(idx.to_period(), expected)
350-
351-
# GH 7606
352-
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
353-
assert idx.freqstr is None
354-
tm.assert_index_equal(idx.to_period(), expected)
355291

292+
class TestAstype:
356293
@pytest.mark.parametrize("tz", [None, "US/Central"])
357294
def test_astype_category(self, tz):
358295
obj = pd.date_range("2000", periods=2, tz=tz)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import dateutil.tz
2+
from dateutil.tz import tzlocal
3+
import pytest
4+
import pytz
5+
6+
from pandas._libs.tslibs.ccalendar import MONTHS
7+
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
8+
9+
from pandas import (
10+
DatetimeIndex,
11+
Period,
12+
PeriodIndex,
13+
Timestamp,
14+
date_range,
15+
period_range,
16+
)
17+
import pandas._testing as tm
18+
19+
20+
class TestToPeriod:
21+
def test_dti_to_period(self):
22+
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
23+
pi1 = dti.to_period()
24+
pi2 = dti.to_period(freq="D")
25+
pi3 = dti.to_period(freq="3D")
26+
27+
assert pi1[0] == Period("Jan 2005", freq="M")
28+
assert pi2[0] == Period("1/31/2005", freq="D")
29+
assert pi3[0] == Period("1/31/2005", freq="3D")
30+
31+
assert pi1[-1] == Period("Nov 2005", freq="M")
32+
assert pi2[-1] == Period("11/30/2005", freq="D")
33+
assert pi3[-1], Period("11/30/2005", freq="3D")
34+
35+
tm.assert_index_equal(pi1, period_range("1/1/2005", "11/1/2005", freq="M"))
36+
tm.assert_index_equal(
37+
pi2, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("D")
38+
)
39+
tm.assert_index_equal(
40+
pi3, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("3D")
41+
)
42+
43+
@pytest.mark.parametrize("month", MONTHS)
44+
def test_to_period_quarterly(self, month):
45+
# make sure we can make the round trip
46+
freq = "Q-{month}".format(month=month)
47+
rng = period_range("1989Q3", "1991Q3", freq=freq)
48+
stamps = rng.to_timestamp()
49+
result = stamps.to_period(freq)
50+
tm.assert_index_equal(rng, result)
51+
52+
@pytest.mark.parametrize("off", ["BQ", "QS", "BQS"])
53+
def test_to_period_quarterlyish(self, off):
54+
rng = date_range("01-Jan-2012", periods=8, freq=off)
55+
prng = rng.to_period()
56+
assert prng.freq == "Q-DEC"
57+
58+
@pytest.mark.parametrize("off", ["BA", "AS", "BAS"])
59+
def test_to_period_annualish(self, off):
60+
rng = date_range("01-Jan-2012", periods=8, freq=off)
61+
prng = rng.to_period()
62+
assert prng.freq == "A-DEC"
63+
64+
def test_to_period_monthish(self):
65+
offsets = ["MS", "BM"]
66+
for off in offsets:
67+
rng = date_range("01-Jan-2012", periods=8, freq=off)
68+
prng = rng.to_period()
69+
assert prng.freq == "M"
70+
71+
rng = date_range("01-Jan-2012", periods=8, freq="M")
72+
prng = rng.to_period()
73+
assert prng.freq == "M"
74+
75+
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
76+
date_range("01-Jan-2012", periods=8, freq="EOM")
77+
78+
def test_period_dt64_round_trip(self):
79+
dti = date_range("1/1/2000", "1/7/2002", freq="B")
80+
pi = dti.to_period()
81+
tm.assert_index_equal(pi.to_timestamp(), dti)
82+
83+
dti = date_range("1/1/2000", "1/7/2002", freq="B")
84+
pi = dti.to_period(freq="H")
85+
tm.assert_index_equal(pi.to_timestamp(), dti)
86+
87+
def test_to_period_millisecond(self):
88+
index = DatetimeIndex(
89+
[
90+
Timestamp("2007-01-01 10:11:12.123456Z"),
91+
Timestamp("2007-01-01 10:11:13.789123Z"),
92+
]
93+
)
94+
95+
with tm.assert_produces_warning(UserWarning):
96+
# warning that timezone info will be lost
97+
period = index.to_period(freq="L")
98+
assert 2 == len(period)
99+
assert period[0] == Period("2007-01-01 10:11:12.123Z", "L")
100+
assert period[1] == Period("2007-01-01 10:11:13.789Z", "L")
101+
102+
def test_to_period_microsecond(self):
103+
index = DatetimeIndex(
104+
[
105+
Timestamp("2007-01-01 10:11:12.123456Z"),
106+
Timestamp("2007-01-01 10:11:13.789123Z"),
107+
]
108+
)
109+
110+
with tm.assert_produces_warning(UserWarning):
111+
# warning that timezone info will be lost
112+
period = index.to_period(freq="U")
113+
assert 2 == len(period)
114+
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "U")
115+
assert period[1] == Period("2007-01-01 10:11:13.789123Z", "U")
116+
117+
@pytest.mark.parametrize(
118+
"tz",
119+
["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()],
120+
)
121+
def test_to_period_tz(self, tz):
122+
ts = date_range("1/1/2000", "2/1/2000", tz=tz)
123+
124+
with tm.assert_produces_warning(UserWarning):
125+
# GH#21333 warning that timezone info will be lost
126+
result = ts.to_period()[0]
127+
expected = ts[0].to_period()
128+
129+
assert result == expected
130+
131+
expected = date_range("1/1/2000", "2/1/2000").to_period()
132+
133+
with tm.assert_produces_warning(UserWarning):
134+
# GH#21333 warning that timezone info will be lost
135+
result = ts.to_period()
136+
137+
tm.assert_index_equal(result, expected)
138+
139+
@pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"])
140+
def test_to_period_tz_utc_offset_consistency(self, tz):
141+
# GH#22905
142+
ts = date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
143+
with tm.assert_produces_warning(UserWarning):
144+
result = ts.to_period()[0]
145+
expected = ts[0].to_period()
146+
assert result == expected
147+
148+
def test_to_period_nofreq(self):
149+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"])
150+
with pytest.raises(ValueError):
151+
idx.to_period()
152+
153+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer")
154+
assert idx.freqstr == "D"
155+
expected = PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D")
156+
tm.assert_index_equal(idx.to_period(), expected)
157+
158+
# GH#7606
159+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
160+
assert idx.freqstr is None
161+
tm.assert_index_equal(idx.to_period(), expected)

pandas/tests/indexes/period/test_asfreq.py

+4-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import numpy as np
21
import pytest
32

4-
import pandas as pd
5-
from pandas import DataFrame, PeriodIndex, Series, period_range
3+
from pandas import PeriodIndex, period_range
64
import pandas._testing as tm
75

86

@@ -98,46 +96,26 @@ def test_asfreq_mult_pi(self, freq):
9896
assert result.freq == exp.freq
9997

10098
def test_asfreq_combined_pi(self):
101-
pi = pd.PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="H")
99+
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="H")
102100
exp = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="25H")
103101
for freq, how in zip(["1D1H", "1H1D"], ["S", "E"]):
104102
result = pi.asfreq(freq, how=how)
105103
tm.assert_index_equal(result, exp)
106104
assert result.freq == exp.freq
107105

108106
for freq in ["1D1H", "1H1D"]:
109-
pi = pd.PeriodIndex(
110-
["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq
111-
)
107+
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq)
112108
result = pi.asfreq("H")
113109
exp = PeriodIndex(["2001-01-02 00:00", "2001-01-03 02:00", "NaT"], freq="H")
114110
tm.assert_index_equal(result, exp)
115111
assert result.freq == exp.freq
116112

117-
pi = pd.PeriodIndex(
118-
["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq
119-
)
113+
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq)
120114
result = pi.asfreq("H", how="S")
121115
exp = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="H")
122116
tm.assert_index_equal(result, exp)
123117
assert result.freq == exp.freq
124118

125-
def test_asfreq_ts(self):
126-
index = period_range(freq="A", start="1/1/2001", end="12/31/2010")
127-
ts = Series(np.random.randn(len(index)), index=index)
128-
df = DataFrame(np.random.randn(len(index), 3), index=index)
129-
130-
result = ts.asfreq("D", how="end")
131-
df_result = df.asfreq("D", how="end")
132-
exp_index = index.asfreq("D", how="end")
133-
assert len(result) == len(ts)
134-
tm.assert_index_equal(result.index, exp_index)
135-
tm.assert_index_equal(df_result.index, exp_index)
136-
137-
result = ts.asfreq("D", how="start")
138-
assert len(result) == len(ts)
139-
tm.assert_index_equal(result.index, index.asfreq("D", how="start"))
140-
141119
def test_astype_asfreq(self):
142120
pi1 = PeriodIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="D")
143121
exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")

0 commit comments

Comments
 (0)