17
17
18
18
# TODO: more freq variants
19
19
@pytest .fixture (params = ["D" , "B" , "W" , "M" , "Q" , "Y" ])
20
- def period_index (request ):
20
+ def freqstr (request ):
21
+ return request .param
22
+
23
+
24
+ @pytest .fixture
25
+ def period_index (freqstr ):
21
26
"""
22
27
A fixture to provide PeriodIndex objects with different frequencies.
23
28
24
29
Most PeriodArray behavior is already tested in PeriodIndex tests,
25
30
so here we just test that the PeriodArray behavior matches
26
31
the PeriodIndex behavior.
27
32
"""
28
- freqstr = request .param
29
33
# TODO: non-monotone indexes; NaTs, different start dates
30
34
pi = pd .period_range (start = pd .Timestamp ("2000-01-01" ), periods = 100 , freq = freqstr )
31
35
return pi
32
36
33
37
34
- @pytest .fixture ( params = [ "D" , "B" , "W" , "M" , "Q" , "Y" ])
35
- def datetime_index (request ):
38
+ @pytest .fixture
39
+ def datetime_index (freqstr ):
36
40
"""
37
41
A fixture to provide DatetimeIndex objects with different frequencies.
38
42
39
43
Most DatetimeArray behavior is already tested in DatetimeIndex tests,
40
44
so here we just test that the DatetimeArray behavior matches
41
45
the DatetimeIndex behavior.
42
46
"""
43
- freqstr = request .param
44
47
# TODO: non-monotone indexes; NaTs, different start dates, timezones
45
48
dti = pd .date_range (start = pd .Timestamp ("2000-01-01" ), periods = 100 , freq = freqstr )
46
49
return dti
47
50
48
51
49
52
@pytest .fixture
50
- def timedelta_index (request ):
53
+ def timedelta_index ():
51
54
"""
52
55
A fixture to provide TimedeltaIndex objects with different frequencies.
53
56
Most TimedeltaArray behavior is already tested in TimedeltaIndex tests,
@@ -448,16 +451,15 @@ class TestDatetimeArray(SharedTests):
448
451
dtype = pd .Timestamp
449
452
450
453
@pytest .fixture
451
- def arr1d (self , tz_naive_fixture ):
454
+ def arr1d (self , tz_naive_fixture , freqstr ):
452
455
tz = tz_naive_fixture
453
- dti = pd .date_range ("2016-01-01 01:01:00" , periods = 3 , freq = "H" , tz = tz )
456
+ dti = pd .date_range ("2016-01-01 01:01:00" , periods = 3 , freq = freqstr , tz = tz )
454
457
dta = dti ._data
455
458
return dta
456
459
457
- def test_round (self , tz_naive_fixture ):
460
+ def test_round (self , arr1d ):
458
461
# GH#24064
459
- tz = tz_naive_fixture
460
- dti = pd .date_range ("2016-01-01 01:01:00" , periods = 3 , freq = "H" , tz = tz )
462
+ dti = self .index_cls (arr1d )
461
463
462
464
result = dti .round (freq = "2T" )
463
465
expected = dti - pd .Timedelta (minutes = 1 )
@@ -511,11 +513,10 @@ def test_array_interface(self, datetime_index):
511
513
expected = np .asarray (arr ).astype (dtype )
512
514
tm .assert_numpy_array_equal (result , expected )
513
515
514
- def test_array_object_dtype (self , tz_naive_fixture ):
516
+ def test_array_object_dtype (self , arr1d ):
515
517
# GH#23524
516
- tz = tz_naive_fixture
517
- dti = pd .date_range ("2016-01-01" , periods = 3 , tz = tz )
518
- arr = DatetimeArray (dti )
518
+ arr = arr1d
519
+ dti = self .index_cls (arr1d )
519
520
520
521
expected = np .array (list (dti ))
521
522
@@ -526,11 +527,10 @@ def test_array_object_dtype(self, tz_naive_fixture):
526
527
result = np .array (dti , dtype = object )
527
528
tm .assert_numpy_array_equal (result , expected )
528
529
529
- def test_array_tz (self , tz_naive_fixture ):
530
+ def test_array_tz (self , arr1d ):
530
531
# GH#23524
531
- tz = tz_naive_fixture
532
- dti = pd .date_range ("2016-01-01" , periods = 3 , tz = tz )
533
- arr = DatetimeArray (dti )
532
+ arr = arr1d
533
+ dti = self .index_cls (arr1d )
534
534
535
535
expected = dti .asi8 .view ("M8[ns]" )
536
536
result = np .array (arr , dtype = "M8[ns]" )
@@ -547,10 +547,9 @@ def test_array_tz(self, tz_naive_fixture):
547
547
assert result .base is expected .base
548
548
assert result .base is not None
549
549
550
- def test_array_i8_dtype (self , tz_naive_fixture ):
551
- tz = tz_naive_fixture
552
- dti = pd .date_range ("2016-01-01" , periods = 3 , tz = tz )
553
- arr = DatetimeArray (dti )
550
+ def test_array_i8_dtype (self , arr1d ):
551
+ arr = arr1d
552
+ dti = self .index_cls (arr1d )
554
553
555
554
expected = dti .asi8
556
555
result = np .array (arr , dtype = "i8" )
@@ -573,27 +572,25 @@ def test_from_array_keeps_base(self):
573
572
dta = DatetimeArray (arr [:0 ])
574
573
assert dta ._data .base is arr
575
574
576
- def test_from_dti (self , tz_naive_fixture ):
577
- tz = tz_naive_fixture
578
- dti = pd .date_range ("2016-01-01" , periods = 3 , tz = tz )
579
- arr = DatetimeArray (dti )
575
+ def test_from_dti (self , arr1d ):
576
+ arr = arr1d
577
+ dti = self .index_cls (arr1d )
580
578
assert list (dti ) == list (arr )
581
579
582
580
# Check that Index.__new__ knows what to do with DatetimeArray
583
581
dti2 = pd .Index (arr )
584
582
assert isinstance (dti2 , pd .DatetimeIndex )
585
583
assert list (dti2 ) == list (arr )
586
584
587
- def test_astype_object (self , tz_naive_fixture ):
588
- tz = tz_naive_fixture
589
- dti = pd . date_range ( "2016-01-01" , periods = 3 , tz = tz )
590
- arr = DatetimeArray ( dti )
585
+ def test_astype_object (self , arr1d ):
586
+ arr = arr1d
587
+ dti = self . index_cls ( arr1d )
588
+
591
589
asobj = arr .astype ("O" )
592
590
assert isinstance (asobj , np .ndarray )
593
591
assert asobj .dtype == "O"
594
592
assert list (asobj ) == list (dti )
595
593
596
- @pytest .mark .parametrize ("freqstr" , ["D" , "B" , "W" , "M" , "Q" , "Y" ])
597
594
def test_to_perioddelta (self , datetime_index , freqstr ):
598
595
# GH#23113
599
596
dti = datetime_index
@@ -612,7 +609,6 @@ def test_to_perioddelta(self, datetime_index, freqstr):
612
609
# an EA-specific tm.assert_ function
613
610
tm .assert_index_equal (pd .Index (result ), pd .Index (expected ))
614
611
615
- @pytest .mark .parametrize ("freqstr" , ["D" , "B" , "W" , "M" , "Q" , "Y" ])
616
612
def test_to_period (self , datetime_index , freqstr ):
617
613
dti = datetime_index
618
614
arr = DatetimeArray (dti )
@@ -626,10 +622,10 @@ def test_to_period(self, datetime_index, freqstr):
626
622
tm .assert_index_equal (pd .Index (result ), pd .Index (expected ))
627
623
628
624
@pytest .mark .parametrize ("propname" , pd .DatetimeIndex ._bool_ops )
629
- def test_bool_properties (self , datetime_index , propname ):
625
+ def test_bool_properties (self , arr1d , propname ):
630
626
# in this case _bool_ops is just `is_leap_year`
631
- dti = datetime_index
632
- arr = DatetimeArray ( dti )
627
+ dti = self . index_cls ( arr1d )
628
+ arr = arr1d
633
629
assert dti .freq == arr .freq
634
630
635
631
result = getattr (arr , propname )
@@ -638,21 +634,21 @@ def test_bool_properties(self, datetime_index, propname):
638
634
tm .assert_numpy_array_equal (result , expected )
639
635
640
636
@pytest .mark .parametrize ("propname" , pd .DatetimeIndex ._field_ops )
641
- def test_int_properties (self , datetime_index , propname ):
637
+ def test_int_properties (self , arr1d , propname ):
642
638
if propname in ["week" , "weekofyear" ]:
643
639
# GH#33595 Deprecate week and weekofyear
644
640
return
645
- dti = datetime_index
646
- arr = DatetimeArray ( dti )
641
+ dti = self . index_cls ( arr1d )
642
+ arr = arr1d
647
643
648
644
result = getattr (arr , propname )
649
645
expected = np .array (getattr (dti , propname ), dtype = result .dtype )
650
646
651
647
tm .assert_numpy_array_equal (result , expected )
652
648
653
- def test_take_fill_valid (self , datetime_index , tz_naive_fixture ):
654
- dti = datetime_index . tz_localize ( tz_naive_fixture )
655
- arr = DatetimeArray ( dti )
649
+ def test_take_fill_valid (self , arr1d ):
650
+ arr = arr1d
651
+ dti = self . index_cls ( arr1d )
656
652
657
653
now = pd .Timestamp .now ().tz_localize (dti .tz )
658
654
result = arr .take ([- 1 , 1 ], allow_fill = True , fill_value = now )
@@ -687,10 +683,9 @@ def test_take_fill_valid(self, datetime_index, tz_naive_fixture):
687
683
# require appropriate-dtype if we have a NA value
688
684
arr .take ([- 1 , 1 ], allow_fill = True , fill_value = value )
689
685
690
- def test_concat_same_type_invalid (self , datetime_index ):
686
+ def test_concat_same_type_invalid (self , arr1d ):
691
687
# different timezones
692
- dti = datetime_index
693
- arr = DatetimeArray (dti )
688
+ arr = arr1d
694
689
695
690
if arr .tz is None :
696
691
other = arr .tz_localize ("UTC" )
@@ -718,8 +713,8 @@ def test_concat_same_type_different_freq(self):
718
713
719
714
tm .assert_datetime_array_equal (result , expected )
720
715
721
- def test_strftime (self , datetime_index ):
722
- arr = DatetimeArray ( datetime_index )
716
+ def test_strftime (self , arr1d ):
717
+ arr = arr1d
723
718
724
719
result = arr .strftime ("%Y %b" )
725
720
expected = np .array ([ts .strftime ("%Y %b" ) for ts in arr ], dtype = object )
@@ -864,27 +859,26 @@ class TestPeriodArray(SharedTests):
864
859
def arr1d (self , period_index ):
865
860
return period_index ._data
866
861
867
- def test_from_pi (self , period_index ):
868
- pi = period_index
869
- arr = PeriodArray ( pi )
862
+ def test_from_pi (self , arr1d ):
863
+ pi = self . index_cls ( arr1d )
864
+ arr = arr1d
870
865
assert list (arr ) == list (pi )
871
866
872
867
# Check that Index.__new__ knows what to do with PeriodArray
873
868
pi2 = pd .Index (arr )
874
869
assert isinstance (pi2 , pd .PeriodIndex )
875
870
assert list (pi2 ) == list (arr )
876
871
877
- def test_astype_object (self , period_index ):
878
- pi = period_index
879
- arr = PeriodArray ( pi )
872
+ def test_astype_object (self , arr1d ):
873
+ pi = self . index_cls ( arr1d )
874
+ arr = arr1d
880
875
asobj = arr .astype ("O" )
881
876
assert isinstance (asobj , np .ndarray )
882
877
assert asobj .dtype == "O"
883
878
assert list (asobj ) == list (pi )
884
879
885
- def test_take_fill_valid (self , period_index ):
886
- pi = period_index
887
- arr = PeriodArray (pi )
880
+ def test_take_fill_valid (self , arr1d ):
881
+ arr = arr1d
888
882
889
883
value = pd .NaT .value
890
884
msg = f"'fill_value' should be a { self .dtype } . Got '{ value } '."
@@ -899,9 +893,9 @@ def test_take_fill_valid(self, period_index):
899
893
arr .take ([- 1 , 1 ], allow_fill = True , fill_value = value )
900
894
901
895
@pytest .mark .parametrize ("how" , ["S" , "E" ])
902
- def test_to_timestamp (self , how , period_index ):
903
- pi = period_index
904
- arr = PeriodArray ( pi )
896
+ def test_to_timestamp (self , how , arr1d ):
897
+ pi = self . index_cls ( arr1d )
898
+ arr = arr1d
905
899
906
900
expected = DatetimeArray (pi .to_timestamp (how = how ))
907
901
result = arr .to_timestamp (how = how )
@@ -922,28 +916,28 @@ def test_to_timestamp_out_of_bounds(self):
922
916
pi ._data .to_timestamp ()
923
917
924
918
@pytest .mark .parametrize ("propname" , PeriodArray ._bool_ops )
925
- def test_bool_properties (self , period_index , propname ):
919
+ def test_bool_properties (self , arr1d , propname ):
926
920
# in this case _bool_ops is just `is_leap_year`
927
- pi = period_index
928
- arr = PeriodArray ( pi )
921
+ pi = self . index_cls ( arr1d )
922
+ arr = arr1d
929
923
930
924
result = getattr (arr , propname )
931
925
expected = np .array (getattr (pi , propname ))
932
926
933
927
tm .assert_numpy_array_equal (result , expected )
934
928
935
929
@pytest .mark .parametrize ("propname" , PeriodArray ._field_ops )
936
- def test_int_properties (self , period_index , propname ):
937
- pi = period_index
938
- arr = PeriodArray ( pi )
930
+ def test_int_properties (self , arr1d , propname ):
931
+ pi = self . index_cls ( arr1d )
932
+ arr = arr1d
939
933
940
934
result = getattr (arr , propname )
941
935
expected = np .array (getattr (pi , propname ))
942
936
943
937
tm .assert_numpy_array_equal (result , expected )
944
938
945
- def test_array_interface (self , period_index ):
946
- arr = PeriodArray ( period_index )
939
+ def test_array_interface (self , arr1d ):
940
+ arr = arr1d
947
941
948
942
# default asarray gives objects
949
943
result = np .asarray (arr )
@@ -966,8 +960,8 @@ def test_array_interface(self, period_index):
966
960
expected = np .asarray (arr ).astype ("S20" )
967
961
tm .assert_numpy_array_equal (result , expected )
968
962
969
- def test_strftime (self , period_index ):
970
- arr = PeriodArray ( period_index )
963
+ def test_strftime (self , arr1d ):
964
+ arr = arr1d
971
965
972
966
result = arr .strftime ("%Y" )
973
967
expected = np .array ([per .strftime ("%Y" ) for per in arr ], dtype = object )
0 commit comments