19
19
from pandas .core .arrays import PeriodArray
20
20
21
21
22
+ class TestPeriodIndexDisallowedFreqs :
23
+ @pytest .mark .parametrize (
24
+ "freq,freq_depr" ,
25
+ [
26
+ ("2M" , "2ME" ),
27
+ ("2Q-MAR" , "2QE-MAR" ),
28
+ ("2Y-FEB" , "2YE-FEB" ),
29
+ ],
30
+ )
31
+ def test_period_index_frequency_ME_error_message (self , freq , freq_depr ):
32
+ # GH#52064
33
+ msg = f"for Period, please use '{ freq [1 :]} ' instead of '{ freq_depr [1 :]} '"
34
+
35
+ with pytest .raises (ValueError , match = msg ):
36
+ PeriodIndex (["2020-01-01" , "2020-01-02" ], freq = freq_depr )
37
+
38
+ with pytest .raises (ValueError , match = msg ):
39
+ period_range (start = "2020-01-01" , end = "2020-01-02" , freq = freq_depr )
40
+
41
+ @pytest .mark .parametrize ("freq_depr" , ["2SME" , "2CBME" , "2BYE" ])
42
+ def test_period_index_frequency_invalid_freq (self , freq_depr ):
43
+ # GH#9586
44
+ msg = f"Invalid frequency: { freq_depr [1 :]} "
45
+
46
+ with pytest .raises (ValueError , match = msg ):
47
+ period_range ("2020-01" , "2020-05" , freq = freq_depr )
48
+ with pytest .raises (ValueError , match = msg ):
49
+ PeriodIndex (["2020-01" , "2020-05" ], freq = freq_depr )
50
+
51
+
22
52
class TestPeriodIndex :
53
+ def test_from_ordinals (self ):
54
+ Period (ordinal = - 1000 , freq = "Y" )
55
+ Period (ordinal = 0 , freq = "Y" )
56
+
57
+ msg = "The 'ordinal' keyword in PeriodIndex is deprecated"
58
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
59
+ idx1 = PeriodIndex (ordinal = [- 1 , 0 , 1 ], freq = "Y" )
60
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
61
+ idx2 = PeriodIndex (ordinal = np .array ([- 1 , 0 , 1 ]), freq = "Y" )
62
+ tm .assert_index_equal (idx1 , idx2 )
63
+
64
+ alt1 = PeriodIndex .from_ordinals ([- 1 , 0 , 1 ], freq = "Y" )
65
+ tm .assert_index_equal (alt1 , idx1 )
66
+
67
+ alt2 = PeriodIndex .from_ordinals (np .array ([- 1 , 0 , 1 ]), freq = "Y" )
68
+ tm .assert_index_equal (alt2 , idx2 )
69
+
23
70
def test_keyword_mismatch (self ):
24
71
# GH#55961 we should get exactly one of data/ordinals/**fields
25
72
per = Period ("2016-01-01" , "D" )
@@ -131,11 +178,6 @@ def test_constructor_field_arrays(self):
131
178
exp = period_range ("2007-01" , periods = 3 , freq = "M" )
132
179
tm .assert_index_equal (idx , exp )
133
180
134
- def test_constructor_U (self ):
135
- # U was used as undefined period
136
- with pytest .raises (ValueError , match = "Invalid frequency: X" ):
137
- period_range ("2007-1-1" , periods = 500 , freq = "X" )
138
-
139
181
def test_constructor_nano (self ):
140
182
idx = period_range (
141
183
start = Period (ordinal = 1 , freq = "ns" ),
@@ -371,49 +413,12 @@ def test_constructor_mixed(self):
371
413
exp = PeriodIndex (["2011-01-01" , "NaT" , "2012-01-01" ], freq = "D" )
372
414
tm .assert_index_equal (idx , exp )
373
415
374
- def test_constructor_simple_new (self ):
375
- idx = period_range ("2007-01" , name = "p" , periods = 2 , freq = "M" )
376
-
377
- with pytest .raises (AssertionError , match = "<class .*PeriodIndex'>" ):
378
- idx ._simple_new (idx , name = "p" )
379
-
380
- result = idx ._simple_new (idx ._data , name = "p" )
381
- tm .assert_index_equal (result , idx )
382
-
383
- msg = "Should be numpy array of type i8"
384
- with pytest .raises (AssertionError , match = msg ):
385
- # Need ndarray, not int64 Index
386
- type (idx ._data )._simple_new (Index (idx .asi8 ), dtype = idx .dtype )
387
-
388
- arr = type (idx ._data )._simple_new (idx .asi8 , dtype = idx .dtype )
389
- result = idx ._simple_new (arr , name = "p" )
390
- tm .assert_index_equal (result , idx )
391
-
392
- def test_constructor_simple_new_empty (self ):
393
- # GH13079
394
- idx = PeriodIndex ([], freq = "M" , name = "p" )
395
- with pytest .raises (AssertionError , match = "<class .*PeriodIndex'>" ):
396
- idx ._simple_new (idx , name = "p" )
397
-
398
- result = idx ._simple_new (idx ._data , name = "p" )
399
- tm .assert_index_equal (result , idx )
400
-
401
416
@pytest .mark .parametrize ("floats" , [[1.1 , 2.1 ], np .array ([1.1 , 2.1 ])])
402
417
def test_constructor_floats (self , floats ):
403
- with pytest .raises (AssertionError , match = "<class " ):
404
- PeriodIndex ._simple_new (floats )
405
-
406
418
msg = "PeriodIndex does not allow floating point in construction"
407
419
with pytest .raises (TypeError , match = msg ):
408
420
PeriodIndex (floats )
409
421
410
- def test_constructor_nat (self ):
411
- msg = "start and end must not be NaT"
412
- with pytest .raises (ValueError , match = msg ):
413
- period_range (start = "NaT" , end = "2011-01-01" , freq = "M" )
414
- with pytest .raises (ValueError , match = msg ):
415
- period_range (start = "2011-01-01" , end = "NaT" , freq = "M" )
416
-
417
422
def test_constructor_year_and_quarter (self ):
418
423
year = Series ([2001 , 2002 , 2003 ])
419
424
quarter = year - 2000
@@ -493,7 +498,7 @@ def test_constructor_freq_combined(self):
493
498
expected = PeriodIndex (["2016-01-01 00:00" , "2016-01-02 01:00" ], freq = "25h" )
494
499
tm .assert_index_equal (pidx , expected )
495
500
496
- def test_constructor (self ):
501
+ def test_period_range_length (self ):
497
502
pi = period_range (freq = "Y" , start = "1/1/2001" , end = "12/1/2009" )
498
503
assert len (pi ) == 9
499
504
@@ -539,9 +544,12 @@ def test_constructor(self):
539
544
assert (i1 == i2 ).all ()
540
545
assert i1 .freq == i2 .freq
541
546
547
+ def test_infer_freq_from_first_element (self ):
548
+ msg = "Period with BDay freq is deprecated"
542
549
with tm .assert_produces_warning (FutureWarning , match = msg ):
550
+ start = Period ("02-Apr-2005" , "B" )
543
551
end_intv = Period ("2005-05-01" , "B" )
544
- i1 = period_range (start = start , end = end_intv )
552
+ period_range (start = start , end = end_intv )
545
553
546
554
# infer freq from first element
547
555
i2 = PeriodIndex ([end_intv , Period ("2005-05-05" , "B" )])
@@ -553,7 +561,12 @@ def test_constructor(self):
553
561
assert len (i2 ) == 2
554
562
assert i2 [0 ] == end_intv
555
563
564
+ def test_mixed_freq_raises (self ):
556
565
# Mixed freq should fail
566
+ msg = "Period with BDay freq is deprecated"
567
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
568
+ end_intv = Period ("2005-05-01" , "B" )
569
+
557
570
vals = [end_intv , Period ("2006-12-31" , "w" )]
558
571
msg = r"Input has different freq=W-SUN from PeriodIndex\(freq=B\)"
559
572
with pytest .raises (IncompatibleFrequency , match = msg ):
@@ -562,10 +575,6 @@ def test_constructor(self):
562
575
with pytest .raises (IncompatibleFrequency , match = msg ):
563
576
PeriodIndex (vals )
564
577
565
- # tuple freq disallowed GH#34703
566
- with pytest .raises (TypeError , match = "pass as a string instead" ):
567
- Period ("2006-12-31" , ("w" , 1 ))
568
-
569
578
@pytest .mark .parametrize (
570
579
"freq" , ["M" , "Q" , "Y" , "D" , "B" , "min" , "s" , "ms" , "us" , "ns" , "h" ]
571
580
)
@@ -594,20 +603,39 @@ def test_map_with_string_constructor(self):
594
603
# lastly, values should compare equal
595
604
tm .assert_index_equal (res , expected )
596
605
597
- @pytest .mark .parametrize (
598
- "freq, freq_msg" ,
599
- [
600
- (offsets .BYearBegin (), "BYearBegin" ),
601
- (offsets .YearBegin (2 ), "YearBegin" ),
602
- (offsets .QuarterBegin (startingMonth = 12 ), "QuarterBegin" ),
603
- (offsets .BusinessMonthEnd (2 ), "BusinessMonthEnd" ),
604
- ],
605
- )
606
- def test_offsets_not_supported (self , freq , freq_msg ):
607
- # GH#55785
608
- msg = f"{ freq_msg } is not supported as period frequency"
609
- with pytest .raises (TypeError , match = msg ):
610
- Period (year = 2014 , freq = freq )
606
+
607
+ class TestSimpleNew :
608
+ def test_constructor_simple_new (self ):
609
+ idx = period_range ("2007-01" , name = "p" , periods = 2 , freq = "M" )
610
+
611
+ with pytest .raises (AssertionError , match = "<class .*PeriodIndex'>" ):
612
+ idx ._simple_new (idx , name = "p" )
613
+
614
+ result = idx ._simple_new (idx ._data , name = "p" )
615
+ tm .assert_index_equal (result , idx )
616
+
617
+ msg = "Should be numpy array of type i8"
618
+ with pytest .raises (AssertionError , match = msg ):
619
+ # Need ndarray, not int64 Index
620
+ type (idx ._data )._simple_new (Index (idx .asi8 ), dtype = idx .dtype )
621
+
622
+ arr = type (idx ._data )._simple_new (idx .asi8 , dtype = idx .dtype )
623
+ result = idx ._simple_new (arr , name = "p" )
624
+ tm .assert_index_equal (result , idx )
625
+
626
+ def test_constructor_simple_new_empty (self ):
627
+ # GH13079
628
+ idx = PeriodIndex ([], freq = "M" , name = "p" )
629
+ with pytest .raises (AssertionError , match = "<class .*PeriodIndex'>" ):
630
+ idx ._simple_new (idx , name = "p" )
631
+
632
+ result = idx ._simple_new (idx ._data , name = "p" )
633
+ tm .assert_index_equal (result , idx )
634
+
635
+ @pytest .mark .parametrize ("floats" , [[1.1 , 2.1 ], np .array ([1.1 , 2.1 ])])
636
+ def test_period_index_simple_new_disallows_floats (self , floats ):
637
+ with pytest .raises (AssertionError , match = "<class " ):
638
+ PeriodIndex ._simple_new (floats )
611
639
612
640
613
641
class TestShallowCopy :
@@ -638,6 +666,7 @@ def test_constructor_cant_cast_period(self):
638
666
Series (period_range ("2000-01-01" , periods = 10 , freq = "D" ), dtype = float )
639
667
640
668
def test_constructor_cast_object (self ):
641
- s = Series (period_range ("1/1/2000" , periods = 10 ), dtype = PeriodDtype ("D" ))
642
- exp = Series (period_range ("1/1/2000" , periods = 10 ))
643
- tm .assert_series_equal (s , exp )
669
+ pi = period_range ("1/1/2000" , periods = 10 )
670
+ ser = Series (pi , dtype = PeriodDtype ("D" ))
671
+ exp = Series (pi )
672
+ tm .assert_series_equal (ser , exp )
0 commit comments