1- from datetime import datetime
2-
31import numpy as np
42import 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
205import 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-
11530class 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