7
7
8
8
import pandas as pd
9
9
from pandas import Index , Period , PeriodIndex , Series , date_range , offsets , period_range
10
- import pandas .core .indexes . period as period
10
+ from pandas .core .arrays import PeriodArray
11
11
import pandas .util .testing as tm
12
12
13
13
14
14
class TestPeriodIndex :
15
- def setup_method (self , method ):
16
- pass
17
-
18
15
def test_construction_base_constructor (self ):
19
16
# GH 13664
20
17
arr = [pd .Period ("2011-01" , freq = "M" ), pd .NaT , pd .Period ("2011-03" , freq = "M" )]
@@ -32,6 +29,30 @@ def test_construction_base_constructor(self):
32
29
pd .Index (np .array (arr )), pd .Index (np .array (arr ), dtype = object )
33
30
)
34
31
32
+ def test_base_constructor_with_period_dtype (self ):
33
+ dtype = PeriodDtype ("D" )
34
+ values = ["2011-01-01" , "2012-03-04" , "2014-05-01" ]
35
+ result = pd .Index (values , dtype = dtype )
36
+
37
+ expected = pd .PeriodIndex (values , dtype = dtype )
38
+ tm .assert_index_equal (result , expected )
39
+
40
+ @pytest .mark .parametrize (
41
+ "values_constructor" , [list , np .array , PeriodIndex , PeriodArray ._from_sequence ]
42
+ )
43
+ def test_index_object_dtype (self , values_constructor ):
44
+ # Index(periods, dtype=object) is an Index (not an PeriodIndex)
45
+ periods = [
46
+ pd .Period ("2011-01" , freq = "M" ),
47
+ pd .NaT ,
48
+ pd .Period ("2011-03" , freq = "M" ),
49
+ ]
50
+ values = values_constructor (periods )
51
+ result = Index (values , dtype = object )
52
+
53
+ assert type (result ) is Index
54
+ tm .assert_numpy_array_equal (result .values , np .array (values ))
55
+
35
56
def test_constructor_use_start_freq (self ):
36
57
# GH #1118
37
58
p = Period ("4/2/2012" , freq = "B" )
@@ -201,7 +222,7 @@ def test_constructor_dtype(self):
201
222
assert res .dtype == "period[M]"
202
223
203
224
msg = "specified freq and dtype are different"
204
- with pytest .raises (period . IncompatibleFrequency , match = msg ):
225
+ with pytest .raises (IncompatibleFrequency , match = msg ):
205
226
PeriodIndex (["2011-01" ], freq = "M" , dtype = "period[D]" )
206
227
207
228
def test_constructor_empty (self ):
@@ -261,25 +282,25 @@ def test_constructor_pi_nat(self):
261
282
def test_constructor_incompat_freq (self ):
262
283
msg = "Input has different freq=D from PeriodIndex\\ (freq=M\\ )"
263
284
264
- with pytest .raises (period . IncompatibleFrequency , match = msg ):
285
+ with pytest .raises (IncompatibleFrequency , match = msg ):
265
286
PeriodIndex (
266
287
[Period ("2011-01" , freq = "M" ), pd .NaT , Period ("2011-01" , freq = "D" )]
267
288
)
268
289
269
- with pytest .raises (period . IncompatibleFrequency , match = msg ):
290
+ with pytest .raises (IncompatibleFrequency , match = msg ):
270
291
PeriodIndex (
271
292
np .array (
272
293
[Period ("2011-01" , freq = "M" ), pd .NaT , Period ("2011-01" , freq = "D" )]
273
294
)
274
295
)
275
296
276
297
# first element is pd.NaT
277
- with pytest .raises (period . IncompatibleFrequency , match = msg ):
298
+ with pytest .raises (IncompatibleFrequency , match = msg ):
278
299
PeriodIndex (
279
300
[pd .NaT , Period ("2011-01" , freq = "M" ), Period ("2011-01" , freq = "D" )]
280
301
)
281
302
282
- with pytest .raises (period . IncompatibleFrequency , match = msg ):
303
+ with pytest .raises (IncompatibleFrequency , match = msg ):
283
304
PeriodIndex (
284
305
np .array (
285
306
[pd .NaT , Period ("2011-01" , freq = "M" ), Period ("2011-01" , freq = "D" )]
0 commit comments