Skip to content

Commit 1d18e95

Browse files
authored
implement test_to_period (#32270)
1 parent 217a428 commit 1d18e95

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import numpy as np
2+
3+
from pandas import (
4+
DataFrame,
5+
DatetimeIndex,
6+
PeriodIndex,
7+
Series,
8+
date_range,
9+
period_range,
10+
)
11+
import pandas._testing as tm
12+
13+
14+
class TestToPeriod:
15+
def test_to_period(self):
16+
rng = date_range("1/1/2000", "1/1/2001", freq="D")
17+
ts = Series(np.random.randn(len(rng)), index=rng)
18+
19+
pts = ts.to_period()
20+
exp = ts.copy()
21+
exp.index = period_range("1/1/2000", "1/1/2001")
22+
tm.assert_series_equal(pts, exp)
23+
24+
pts = ts.to_period("M")
25+
exp.index = exp.index.asfreq("M")
26+
tm.assert_index_equal(pts.index, exp.index.asfreq("M"))
27+
tm.assert_series_equal(pts, exp)
28+
29+
# GH#7606 without freq
30+
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"])
31+
exp_idx = PeriodIndex(
32+
["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"], freq="D"
33+
)
34+
35+
s = Series(np.random.randn(4), index=idx)
36+
expected = s.copy()
37+
expected.index = exp_idx
38+
tm.assert_series_equal(s.to_period(), expected)
39+
40+
df = DataFrame(np.random.randn(4, 4), index=idx, columns=idx)
41+
expected = df.copy()
42+
expected.index = exp_idx
43+
tm.assert_frame_equal(df.to_period(), expected)
44+
45+
expected = df.copy()
46+
expected.columns = exp_idx
47+
tm.assert_frame_equal(df.to_period(axis=1), expected)

pandas/tests/series/test_timeseries.py

-35
Original file line numberDiff line numberDiff line change
@@ -173,41 +173,6 @@ def test_format_pre_1900_dates(self):
173173
ts = Series(1, index=rng)
174174
repr(ts)
175175

176-
def test_to_period(self):
177-
from pandas.core.indexes.period import period_range
178-
179-
ts = _simple_ts("1/1/2000", "1/1/2001")
180-
181-
pts = ts.to_period()
182-
exp = ts.copy()
183-
exp.index = period_range("1/1/2000", "1/1/2001")
184-
tm.assert_series_equal(pts, exp)
185-
186-
pts = ts.to_period("M")
187-
exp.index = exp.index.asfreq("M")
188-
tm.assert_index_equal(pts.index, exp.index.asfreq("M"))
189-
tm.assert_series_equal(pts, exp)
190-
191-
# GH 7606 without freq
192-
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"])
193-
exp_idx = pd.PeriodIndex(
194-
["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"], freq="D"
195-
)
196-
197-
s = Series(np.random.randn(4), index=idx)
198-
expected = s.copy()
199-
expected.index = exp_idx
200-
tm.assert_series_equal(s.to_period(), expected)
201-
202-
df = DataFrame(np.random.randn(4, 4), index=idx, columns=idx)
203-
expected = df.copy()
204-
expected.index = exp_idx
205-
tm.assert_frame_equal(df.to_period(), expected)
206-
207-
expected = df.copy()
208-
expected.columns = exp_idx
209-
tm.assert_frame_equal(df.to_period(axis=1), expected)
210-
211176
def test_groupby_count_dateparseerror(self):
212177
dr = date_range(start="1/1/2012", freq="5min", periods=10)
213178

0 commit comments

Comments
 (0)