@@ -94,6 +94,16 @@ def test_take(self):
94
94
95
95
tm .assert_index_equal (self .index_cls (result ), expected )
96
96
97
+ @pytest .mark .parametrize ("fill_value" , [2 , 2.0 , pd .Timestamp .now ().time ])
98
+ def test_take_fill_raises (self , fill_value ):
99
+ data = np .arange (10 , dtype = "i8" ) * 24 * 3600 * 10 ** 9
100
+
101
+ arr = self .array_cls ._simple_new (data , freq = "D" )
102
+
103
+ msg = f"'fill_value' should be a { self .dtype } . Got '{ fill_value } '"
104
+ with pytest .raises (ValueError , match = msg ):
105
+ arr .take ([0 , 1 ], allow_fill = True , fill_value = fill_value )
106
+
97
107
def test_take_fill (self ):
98
108
data = np .arange (10 , dtype = "i8" ) * 24 * 3600 * 10 ** 9
99
109
@@ -108,15 +118,6 @@ def test_take_fill(self):
108
118
result = arr .take ([- 1 , 1 ], allow_fill = True , fill_value = pd .NaT )
109
119
assert result [0 ] is pd .NaT
110
120
111
- with pytest .raises (ValueError ):
112
- arr .take ([0 , 1 ], allow_fill = True , fill_value = 2 )
113
-
114
- with pytest .raises (ValueError ):
115
- arr .take ([0 , 1 ], allow_fill = True , fill_value = 2.0 )
116
-
117
- with pytest .raises (ValueError ):
118
- arr .take ([0 , 1 ], allow_fill = True , fill_value = pd .Timestamp .now ().time )
119
-
120
121
def test_concat_same_type (self ):
121
122
data = np .arange (10 , dtype = "i8" ) * 24 * 3600 * 10 ** 9
122
123
@@ -139,7 +140,8 @@ def test_unbox_scalar(self):
139
140
result = arr ._unbox_scalar (pd .NaT )
140
141
assert isinstance (result , int )
141
142
142
- with pytest .raises (ValueError ):
143
+ msg = f"'value' should be a { self .dtype .__name__ } ."
144
+ with pytest .raises (ValueError , match = msg ):
143
145
arr ._unbox_scalar ("foo" )
144
146
145
147
def test_check_compatible_with (self ):
@@ -261,6 +263,7 @@ def test_shift_fill_int_deprecated(self):
261
263
class TestDatetimeArray (SharedTests ):
262
264
index_cls = pd .DatetimeIndex
263
265
array_cls = DatetimeArray
266
+ dtype = pd .Timestamp
264
267
265
268
def test_round (self , tz_naive_fixture ):
266
269
# GH#24064
@@ -452,23 +455,28 @@ def test_take_fill_valid(self, datetime_index, tz_naive_fixture):
452
455
result = arr .take ([- 1 , 1 ], allow_fill = True , fill_value = now )
453
456
assert result [0 ] == now
454
457
455
- with pytest .raises (ValueError ):
458
+ msg = f"'fill_value' should be a { self .dtype } . Got '0 days 00:00:00'."
459
+ with pytest .raises (ValueError , match = msg ):
456
460
# fill_value Timedelta invalid
457
461
arr .take ([- 1 , 1 ], allow_fill = True , fill_value = now - now )
458
462
459
- with pytest .raises (ValueError ):
463
+ msg = f"'fill_value' should be a { self .dtype } . Got '2014Q1'."
464
+ with pytest .raises (ValueError , match = msg ):
460
465
# fill_value Period invalid
461
466
arr .take ([- 1 , 1 ], allow_fill = True , fill_value = pd .Period ("2014Q1" ))
462
467
463
468
tz = None if dti .tz is not None else "US/Eastern"
464
469
now = pd .Timestamp .now ().tz_localize (tz )
465
- with pytest .raises (TypeError ):
470
+ msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
471
+ with pytest .raises (TypeError , match = msg ):
466
472
# Timestamp with mismatched tz-awareness
467
473
arr .take ([- 1 , 1 ], allow_fill = True , fill_value = now )
468
474
469
- with pytest .raises (ValueError ):
475
+ value = pd .NaT .value
476
+ msg = f"'fill_value' should be a { self .dtype } . Got '{ value } '."
477
+ with pytest .raises (ValueError , match = msg ):
470
478
# require NaT, not iNaT, as it could be confused with an integer
471
- arr .take ([- 1 , 1 ], allow_fill = True , fill_value = pd . NaT . value )
479
+ arr .take ([- 1 , 1 ], allow_fill = True , fill_value = value )
472
480
473
481
def test_concat_same_type_invalid (self , datetime_index ):
474
482
# different timezones
@@ -520,6 +528,7 @@ def test_strftime_nat(self):
520
528
class TestTimedeltaArray (SharedTests ):
521
529
index_cls = pd .TimedeltaIndex
522
530
array_cls = TimedeltaArray
531
+ dtype = pd .Timedelta
523
532
524
533
def test_from_tdi (self ):
525
534
tdi = pd .TimedeltaIndex (["1 Day" , "3 Hours" ])
@@ -618,18 +627,23 @@ def test_take_fill_valid(self, timedelta_index):
618
627
assert result [0 ] == td1
619
628
620
629
now = pd .Timestamp .now ()
621
- with pytest .raises (ValueError ):
630
+ value = now
631
+ msg = f"'fill_value' should be a { self .dtype } . Got '{ value } '."
632
+ with pytest .raises (ValueError , match = msg ):
622
633
# fill_value Timestamp invalid
623
- arr .take ([0 , 1 ], allow_fill = True , fill_value = now )
634
+ arr .take ([0 , 1 ], allow_fill = True , fill_value = value )
624
635
625
- with pytest .raises (ValueError ):
636
+ value = now .to_period ("D" )
637
+ msg = f"'fill_value' should be a { self .dtype } . Got '{ value } '."
638
+ with pytest .raises (ValueError , match = msg ):
626
639
# fill_value Period invalid
627
- arr .take ([0 , 1 ], allow_fill = True , fill_value = now . to_period ( "D" ) )
640
+ arr .take ([0 , 1 ], allow_fill = True , fill_value = value )
628
641
629
642
630
643
class TestPeriodArray (SharedTests ):
631
644
index_cls = pd .PeriodIndex
632
645
array_cls = PeriodArray
646
+ dtype = pd .Period
633
647
634
648
def test_from_pi (self , period_index ):
635
649
pi = period_index
@@ -665,10 +679,11 @@ def test_to_timestamp(self, how, period_index):
665
679
def test_to_timestamp_out_of_bounds (self ):
666
680
# GH#19643 previously overflowed silently
667
681
pi = pd .period_range ("1500" , freq = "Y" , periods = 3 )
668
- with pytest .raises (OutOfBoundsDatetime ):
682
+ msg = "Out of bounds nanosecond timestamp: 1500-01-01 00:00:00"
683
+ with pytest .raises (OutOfBoundsDatetime , match = msg ):
669
684
pi .to_timestamp ()
670
685
671
- with pytest .raises (OutOfBoundsDatetime ):
686
+ with pytest .raises (OutOfBoundsDatetime , match = msg ):
672
687
pi ._data .to_timestamp ()
673
688
674
689
@pytest .mark .parametrize ("propname" , PeriodArray ._bool_ops )
@@ -708,7 +723,8 @@ def test_array_interface(self, period_index):
708
723
tm .assert_numpy_array_equal (result , arr .asi8 )
709
724
710
725
# to other dtypes
711
- with pytest .raises (TypeError ):
726
+ msg = r"float\(\) argument must be a string or a number, not 'Period'"
727
+ with pytest .raises (TypeError , match = msg ):
712
728
np .asarray (arr , dtype = "float64" )
713
729
714
730
result = np .asarray (arr , dtype = "S20" )
@@ -774,8 +790,13 @@ def test_casting_nat_setitem_array(array, casting_nats):
774
790
ids = lambda x : type (x ).__name__ ,
775
791
)
776
792
def test_invalid_nat_setitem_array (array , non_casting_nats ):
793
+ msg = (
794
+ "'value' should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. "
795
+ "Got '(timedelta64|datetime64|int)' instead."
796
+ )
797
+
777
798
for nat in non_casting_nats :
778
- with pytest .raises (TypeError ):
799
+ with pytest .raises (TypeError , match = msg ):
779
800
array [0 ] = nat
780
801
781
802
0 commit comments