@@ -30,52 +30,45 @@ def test_to_timedelta_readonly(self, readonly):
30
30
expected = to_timedelta ([])
31
31
tm .assert_index_equal (result , expected )
32
32
33
- def test_to_timedelta (self ):
34
-
33
+ def test_to_timedelta_null (self ):
35
34
result = to_timedelta (["" , "" ])
36
35
assert isna (result ).all ()
37
36
37
+ def test_to_timedelta_same_np_timedelta64 (self ):
38
38
# pass thru
39
39
result = to_timedelta (np .array ([np .timedelta64 (1 , "s" )]))
40
40
expected = pd .Index (np .array ([np .timedelta64 (1 , "s" )]))
41
41
tm .assert_index_equal (result , expected )
42
42
43
+ def test_to_timedelta_series (self ):
43
44
# Series
44
45
expected = Series ([timedelta (days = 1 ), timedelta (days = 1 , seconds = 1 )])
45
46
result = to_timedelta (Series (["1d" , "1days 00:00:01" ]))
46
47
tm .assert_series_equal (result , expected )
47
48
49
+ def test_to_timedelta_units (self ):
48
50
# with units
49
51
result = TimedeltaIndex (
50
52
[np .timedelta64 (0 , "ns" ), np .timedelta64 (10 , "s" ).astype ("m8[ns]" )]
51
53
)
52
54
expected = to_timedelta ([0 , 10 ], unit = "s" )
53
55
tm .assert_index_equal (result , expected )
54
56
57
+ @pytest .mark .parametrize (
58
+ "dtype, unit" ,
59
+ [
60
+ ["int64" , "s" ],
61
+ ["int64" , "m" ],
62
+ ["int64" , "h" ],
63
+ ["timedelta64[s]" , "s" ],
64
+ ["timedelta64[D]" , "D" ],
65
+ ],
66
+ )
67
+ def test_to_timedelta_units_dtypes (self , dtype , unit ):
55
68
# arrays of various dtypes
56
- arr = np .array ([1 ] * 5 , dtype = "int64" )
57
- result = to_timedelta (arr , unit = "s" )
58
- expected = TimedeltaIndex ([np .timedelta64 (1 , "s" )] * 5 )
59
- tm .assert_index_equal (result , expected )
60
-
61
- arr = np .array ([1 ] * 5 , dtype = "int64" )
62
- result = to_timedelta (arr , unit = "m" )
63
- expected = TimedeltaIndex ([np .timedelta64 (1 , "m" )] * 5 )
64
- tm .assert_index_equal (result , expected )
65
-
66
- arr = np .array ([1 ] * 5 , dtype = "int64" )
67
- result = to_timedelta (arr , unit = "h" )
68
- expected = TimedeltaIndex ([np .timedelta64 (1 , "h" )] * 5 )
69
- tm .assert_index_equal (result , expected )
70
-
71
- arr = np .array ([1 ] * 5 , dtype = "timedelta64[s]" )
72
- result = to_timedelta (arr )
73
- expected = TimedeltaIndex ([np .timedelta64 (1 , "s" )] * 5 )
74
- tm .assert_index_equal (result , expected )
75
-
76
- arr = np .array ([1 ] * 5 , dtype = "timedelta64[D]" )
77
- result = to_timedelta (arr )
78
- expected = TimedeltaIndex ([np .timedelta64 (1 , "D" )] * 5 )
69
+ arr = np .array ([1 ] * 5 , dtype = dtype )
70
+ result = to_timedelta (arr , unit = unit )
71
+ expected = TimedeltaIndex ([np .timedelta64 (1 , unit )] * 5 )
79
72
tm .assert_index_equal (result , expected )
80
73
81
74
def test_to_timedelta_oob_non_nano (self ):
@@ -91,31 +84,30 @@ def test_to_timedelta_oob_non_nano(self):
91
84
with pytest .raises (OutOfBoundsTimedelta , match = msg ):
92
85
TimedeltaArray ._from_sequence (arr )
93
86
94
- def test_to_timedelta_dataframe (self ):
87
+ @pytest .mark .parametrize (
88
+ "arg" , [np .arange (10 ).reshape (2 , 5 ), pd .DataFrame (np .arange (10 ).reshape (2 , 5 ))]
89
+ )
90
+ @pytest .mark .parametrize ("errors" , ["ignore" , "raise" , "coerce" ])
91
+ def test_to_timedelta_dataframe (self , arg , errors ):
95
92
# GH 11776
96
- arr = np .arange (10 ).reshape (2 , 5 )
97
- df = pd .DataFrame (np .arange (10 ).reshape (2 , 5 ))
98
- for arg in (arr , df ):
99
- with pytest .raises (TypeError , match = "1-d array" ):
100
- to_timedelta (arg )
101
- for errors in ["ignore" , "raise" , "coerce" ]:
102
- with pytest .raises (TypeError , match = "1-d array" ):
103
- to_timedelta (arg , errors = errors )
93
+ with pytest .raises (TypeError , match = "1-d array" ):
94
+ to_timedelta (arg , errors = errors )
104
95
105
- def test_to_timedelta_invalid (self ):
96
+ def test_to_timedelta_invalid_errors (self ):
106
97
107
98
# bad value for errors parameter
108
99
msg = "errors must be one of"
109
100
with pytest .raises (ValueError , match = msg ):
110
101
to_timedelta (["foo" ], errors = "never" )
111
102
103
+ @pytest .mark .parametrize ("arg" , [[1 , 2 ], 1 ])
104
+ def test_to_timedelta_invalid_unit (self , arg ):
112
105
# these will error
113
106
msg = "invalid unit abbreviation: foo"
114
107
with pytest .raises (ValueError , match = msg ):
115
- to_timedelta ([1 , 2 ], unit = "foo" )
116
- with pytest .raises (ValueError , match = msg ):
117
- to_timedelta (1 , unit = "foo" )
108
+ to_timedelta (arg , unit = "foo" )
118
109
110
+ def test_to_timedelta_time (self ):
119
111
# time not supported ATM
120
112
msg = (
121
113
"Value must be Timedelta, string, integer, float, timedelta or convertible"
@@ -124,10 +116,12 @@ def test_to_timedelta_invalid(self):
124
116
to_timedelta (time (second = 1 ))
125
117
assert to_timedelta (time (second = 1 ), errors = "coerce" ) is pd .NaT
126
118
119
+ def test_to_timedelta_bad_value (self ):
127
120
msg = "Could not convert 'foo' to NumPy timedelta"
128
121
with pytest .raises (ValueError , match = msg ):
129
122
to_timedelta (["foo" , "bar" ])
130
123
124
+ def test_to_timedelta_bad_value_coerce (self ):
131
125
tm .assert_index_equal (
132
126
TimedeltaIndex ([pd .NaT , pd .NaT ]),
133
127
to_timedelta (["foo" , "bar" ], errors = "coerce" ),
@@ -138,6 +132,7 @@ def test_to_timedelta_invalid(self):
138
132
to_timedelta (["1 day" , "bar" , "1 min" ], errors = "coerce" ),
139
133
)
140
134
135
+ def test_to_timedelta_invalid_errors_ignore (self ):
141
136
# gh-13613: these should not error because errors='ignore'
142
137
invalid_data = "apple"
143
138
assert invalid_data == to_timedelta (invalid_data , errors = "ignore" )
@@ -213,11 +208,10 @@ def test_to_timedelta_on_missing_values(self):
213
208
actual = to_timedelta (ser )
214
209
tm .assert_series_equal (actual , expected )
215
210
216
- actual = to_timedelta (np .nan )
217
- assert actual .value == timedelta_NaT .astype ("int64" )
218
-
219
- actual = to_timedelta (pd .NaT )
220
- assert actual .value == timedelta_NaT .astype ("int64" )
211
+ @pytest .mark .parametrize ("val" , [np .nan , pd .NaT ])
212
+ def test_to_timedelta_on_missing_values_scalar (self , val ):
213
+ actual = to_timedelta (val )
214
+ assert actual .value == np .timedelta64 ("NaT" ).astype ("int64" )
221
215
222
216
def test_to_timedelta_float (self ):
223
217
# https://github.com/pandas-dev/pandas/issues/25077
@@ -237,16 +231,13 @@ def test_to_timedelta_ignore_strings_unit(self):
237
231
result = to_timedelta (arr , unit = "ns" , errors = "ignore" )
238
232
tm .assert_numpy_array_equal (result , arr )
239
233
240
- def test_to_timedelta_nullable_int64_dtype (self ):
234
+ @pytest .mark .parametrize (
235
+ "expected_val, result_val" , [[timedelta (days = 2 ), 2 ], [None , None ]]
236
+ )
237
+ def test_to_timedelta_nullable_int64_dtype (self , expected_val , result_val ):
241
238
# GH 35574
242
- expected = Series ([timedelta (days = 1 ), timedelta (days = 2 )])
243
- result = to_timedelta (Series ([1 , 2 ], dtype = "Int64" ), unit = "days" )
244
-
245
- tm .assert_series_equal (result , expected )
246
-
247
- # IntegerArray Series with nulls
248
- expected = Series ([timedelta (days = 1 ), None ])
249
- result = to_timedelta (Series ([1 , None ], dtype = "Int64" ), unit = "days" )
239
+ expected = Series ([timedelta (days = 1 ), expected_val ])
240
+ result = to_timedelta (Series ([1 , result_val ], dtype = "Int64" ), unit = "days" )
250
241
251
242
tm .assert_series_equal (result , expected )
252
243
0 commit comments