@@ -160,108 +160,117 @@ def test_nat_converters(self):
160
160
assert result .astype ("int64" ) == iNaT
161
161
162
162
@pytest .mark .parametrize (
163
- "units , np_unit" ,
164
- [
165
- ([ "W " , "w" ] , "W" ),
166
- ([ "D" , "d" , "days" , "day" , "Days" , "Day" ], "D" ),
167
- (
168
- [ "m" , "minute" , "min" , "minutes" , "t" , "Minute" , "Min" , "Minutes" , "T" ],
163
+ "unit , np_unit" ,
164
+ [( value , "W" ) for value in [ "W" , "w" ]]
165
+ + [( value , "D" ) for value in [ "D " , "d" , "days" , "day" , "Days" , "Day" ]]
166
+ + [
167
+ (value , "m" )
168
+ for value in [
169
169
"m" ,
170
- ),
171
- (["s" , "seconds" , "sec" , "second" , "S" , "Seconds" , "Sec" , "Second" ], "s" ),
172
- (
173
- [
174
- "ms" ,
175
- "milliseconds" ,
176
- "millisecond" ,
177
- "milli" ,
178
- "millis" ,
179
- "l" ,
180
- "MS" ,
181
- "Milliseconds" ,
182
- "Millisecond" ,
183
- "Milli" ,
184
- "Millis" ,
185
- "L" ,
186
- ],
170
+ "minute" ,
171
+ "min" ,
172
+ "minutes" ,
173
+ "t" ,
174
+ "Minute" ,
175
+ "Min" ,
176
+ "Minutes" ,
177
+ "T" ,
178
+ ]
179
+ ]
180
+ + [
181
+ (value , "s" )
182
+ for value in [
183
+ "s" ,
184
+ "seconds" ,
185
+ "sec" ,
186
+ "second" ,
187
+ "S" ,
188
+ "Seconds" ,
189
+ "Sec" ,
190
+ "Second" ,
191
+ ]
192
+ ]
193
+ + [
194
+ (value , "ms" )
195
+ for value in [
187
196
"ms" ,
188
- ) ,
189
- (
190
- [
191
- "us " ,
192
- "microseconds " ,
193
- "microsecond " ,
194
- "micro " ,
195
- "micros " ,
196
- "u " ,
197
- "US " ,
198
- "Microseconds " ,
199
- "Microsecond" ,
200
- "Micro" ,
201
- "Micros" ,
202
- "U" ,
203
- ],
197
+ "milliseconds" ,
198
+ "millisecond" ,
199
+ "milli" ,
200
+ "millis " ,
201
+ "l " ,
202
+ "MS " ,
203
+ "Milliseconds " ,
204
+ "Millisecond " ,
205
+ "Milli " ,
206
+ "Millis " ,
207
+ "L " ,
208
+ ]
209
+ ]
210
+ + [
211
+ ( value , "us" )
212
+ for value in [
204
213
"us" ,
205
- ) ,
206
- (
207
- [
208
- "ns " ,
209
- "nanoseconds " ,
210
- "nanosecond " ,
211
- "nano " ,
212
- "nanos " ,
213
- "n " ,
214
- "NS " ,
215
- "Nanoseconds " ,
216
- "Nanosecond" ,
217
- "Nano" ,
218
- "Nanos" ,
219
- "N" ,
220
- ],
214
+ "microseconds" ,
215
+ "microsecond" ,
216
+ "micro" ,
217
+ "micros " ,
218
+ "u " ,
219
+ "US " ,
220
+ "Microseconds " ,
221
+ "Microsecond " ,
222
+ "Micro " ,
223
+ "Micros " ,
224
+ "U " ,
225
+ ]
226
+ ]
227
+ + [
228
+ ( value , "ns" )
229
+ for value in [
221
230
"ns" ,
222
- ),
231
+ "nanoseconds" ,
232
+ "nanosecond" ,
233
+ "nano" ,
234
+ "nanos" ,
235
+ "n" ,
236
+ "NS" ,
237
+ "Nanoseconds" ,
238
+ "Nanosecond" ,
239
+ "Nano" ,
240
+ "Nanos" ,
241
+ "N" ,
242
+ ]
223
243
],
224
244
)
225
245
@pytest .mark .parametrize ("wrapper" , [np .array , list , pd .Index ])
226
- def test_unit_parser (self , units , np_unit , wrapper ):
246
+ def test_unit_parser (self , unit , np_unit , wrapper ):
227
247
# validate all units, GH 6855, GH 21762
228
- for unit in units :
229
- # array-likes
230
- expected = TimedeltaIndex (
231
- [np .timedelta64 (i , np_unit ) for i in np .arange (5 ).tolist ()]
232
- )
233
- result = to_timedelta (wrapper (range (5 )), unit = unit )
234
- tm .assert_index_equal (result , expected )
235
- result = TimedeltaIndex (wrapper (range (5 )), unit = unit )
236
- tm .assert_index_equal (result , expected )
237
-
238
- if unit == "M" :
239
- # M is treated as minutes in string repr
240
- expected = TimedeltaIndex (
241
- [np .timedelta64 (i , "m" ) for i in np .arange (5 ).tolist ()]
242
- )
243
-
244
- str_repr = [f"{ x } { unit } " for x in np .arange (5 )]
245
- result = to_timedelta (wrapper (str_repr ))
246
- tm .assert_index_equal (result , expected )
247
- result = TimedeltaIndex (wrapper (str_repr ))
248
- tm .assert_index_equal (result , expected )
249
-
250
- # scalar
251
- expected = Timedelta (np .timedelta64 (2 , np_unit ).astype ("timedelta64[ns]" ))
252
-
253
- result = to_timedelta (2 , unit = unit )
254
- assert result == expected
255
- result = Timedelta (2 , unit = unit )
256
- assert result == expected
257
-
258
- if unit == "M" :
259
- expected = Timedelta (np .timedelta64 (2 , "m" ).astype ("timedelta64[ns]" ))
260
-
261
- result = to_timedelta (f"2{ unit } " )
262
- assert result == expected
263
- result = Timedelta (f"2{ unit } " )
264
- assert result == expected
248
+ # array-likes
249
+ expected = TimedeltaIndex (
250
+ [np .timedelta64 (i , np_unit ) for i in np .arange (5 ).tolist ()]
251
+ )
252
+ result = to_timedelta (wrapper (range (5 )), unit = unit )
253
+ tm .assert_index_equal (result , expected )
254
+ result = TimedeltaIndex (wrapper (range (5 )), unit = unit )
255
+ tm .assert_index_equal (result , expected )
256
+
257
+ str_repr = [f"{ x } { unit } " for x in np .arange (5 )]
258
+ result = to_timedelta (wrapper (str_repr ))
259
+ tm .assert_index_equal (result , expected )
260
+ result = to_timedelta (wrapper (str_repr ))
261
+ tm .assert_index_equal (result , expected )
262
+
263
+ # scalar
264
+ expected = Timedelta (np .timedelta64 (2 , np_unit ).astype ("timedelta64[ns]" ))
265
+ result = to_timedelta (2 , unit = unit )
266
+ assert result == expected
267
+ result = Timedelta (2 , unit = unit )
268
+ assert result == expected
269
+
270
+ result = to_timedelta (f"2{ unit } " )
271
+ assert result == expected
272
+ result = Timedelta (f"2{ unit } " )
273
+ assert result == expected
265
274
266
275
@pytest .mark .parametrize ("unit" , ["Y" , "y" , "M" ])
267
276
def test_unit_m_y_raises (self , unit ):
0 commit comments