@@ -762,6 +762,15 @@ def test_to_datetime_unit(self):
762
762
with self .assertRaises (ValueError ):
763
763
to_datetime ([1 , 2 , 111111111 ], unit = 'D' )
764
764
765
+ # coerce we can process
766
+ expected = DatetimeIndex ([Timestamp ('1970-01-02' ),
767
+ Timestamp ('1970-01-03' )] + ['NaT' ] * 1 )
768
+ result = to_datetime ([1 , 2 , 'foo' ], unit = 'D' , errors = 'coerce' )
769
+ tm .assert_index_equal (result , expected )
770
+
771
+ result = to_datetime ([1 , 2 , 111111111 ], unit = 'D' , errors = 'coerce' )
772
+ tm .assert_index_equal (result , expected )
773
+
765
774
def test_series_ctor_datetime64 (self ):
766
775
rng = date_range ('1/1/2000 00:00:00' , '1/1/2000 1:59:50' , freq = '10s' )
767
776
dates = np .asarray (rng )
@@ -2283,6 +2292,123 @@ def test_to_datetime_tz_psycopg2(self):
2283
2292
dtype = 'datetime64[ns, UTC]' )
2284
2293
tm .assert_index_equal (result , expected )
2285
2294
2295
+ def test_unit (self ):
2296
+ # GH 11758
2297
+ # test proper behavior with erros
2298
+
2299
+ with self .assertRaises (ValueError ):
2300
+ to_datetime ([1 ], unit = 'D' , format = '%Y%m%d' )
2301
+
2302
+ values = [11111111 , 1 , 1.0 , tslib .iNaT , pd .NaT , np .nan ,
2303
+ 'NaT' , '' ]
2304
+ result = to_datetime (values , unit = 'D' , errors = 'ignore' )
2305
+ expected = Index ([11111111 , Timestamp ('1970-01-02' ),
2306
+ Timestamp ('1970-01-02' ), pd .NaT ,
2307
+ pd .NaT , pd .NaT , pd .NaT , pd .NaT ],
2308
+ dtype = object )
2309
+ tm .assert_index_equal (result , expected )
2310
+
2311
+ result = to_datetime (values , unit = 'D' , errors = 'coerce' )
2312
+ expected = DatetimeIndex (['NaT' , '1970-01-02' , '1970-01-02' ,
2313
+ 'NaT' , 'NaT' , 'NaT' , 'NaT' , 'NaT' ])
2314
+ tm .assert_index_equal (result , expected )
2315
+
2316
+ with self .assertRaises (tslib .OutOfBoundsDatetime ):
2317
+ to_datetime (values , unit = 'D' , errors = 'raise' )
2318
+
2319
+ values = [1420043460000 , tslib .iNaT , pd .NaT , np .nan , 'NaT' ]
2320
+
2321
+ result = to_datetime (values , errors = 'ignore' , unit = 's' )
2322
+ expected = Index ([1420043460000 , pd .NaT , pd .NaT ,
2323
+ pd .NaT , pd .NaT ], dtype = object )
2324
+ tm .assert_index_equal (result , expected )
2325
+
2326
+ result = to_datetime (values , errors = 'coerce' , unit = 's' )
2327
+ expected = DatetimeIndex (['NaT' , 'NaT' , 'NaT' , 'NaT' , 'NaT' ])
2328
+ tm .assert_index_equal (result , expected )
2329
+
2330
+ with self .assertRaises (tslib .OutOfBoundsDatetime ):
2331
+ to_datetime (values , errors = 'raise' , unit = 's' )
2332
+
2333
+ # if we have a string, then we raise a ValueError
2334
+ # and NOT an OutOfBoundsDatetime
2335
+ for val in ['foo' , Timestamp ('20130101' )]:
2336
+ try :
2337
+ to_datetime (val , errors = 'raise' , unit = 's' )
2338
+ except tslib .OutOfBoundsDatetime :
2339
+ raise AssertionError ("incorrect exception raised" )
2340
+ except ValueError :
2341
+ pass
2342
+
2343
+ def test_unit_consistency (self ):
2344
+
2345
+ # consistency of conversions
2346
+ expected = Timestamp ('1970-05-09 14:25:11' )
2347
+ result = pd .to_datetime (11111111 , unit = 's' , errors = 'raise' )
2348
+ self .assertEqual (result , expected )
2349
+ self .assertIsInstance (result , Timestamp )
2350
+
2351
+ result = pd .to_datetime (11111111 , unit = 's' , errors = 'coerce' )
2352
+ self .assertEqual (result , expected )
2353
+ self .assertIsInstance (result , Timestamp )
2354
+
2355
+ result = pd .to_datetime (11111111 , unit = 's' , errors = 'ignore' )
2356
+ self .assertEqual (result , expected )
2357
+ self .assertIsInstance (result , Timestamp )
2358
+
2359
+ def test_unit_with_numeric (self ):
2360
+
2361
+ # GH 13180
2362
+ # coercions from floats/ints are ok
2363
+ expected = DatetimeIndex (['2015-06-19 05:33:20' ,
2364
+ '2015-05-27 22:33:20' ])
2365
+ arr1 = [1.434692e+18 , 1.432766e+18 ]
2366
+ arr2 = np .array (arr1 ).astype (int )
2367
+ for errors in ['ignore' , 'raise' , 'coerce' ]:
2368
+ result = pd .to_datetime (arr1 , errors = errors )
2369
+ tm .assert_index_equal (result , expected )
2370
+
2371
+ result = pd .to_datetime (arr2 , errors = errors )
2372
+ tm .assert_index_equal (result , expected )
2373
+
2374
+ # but we want to make sure that we are coercing
2375
+ # if we have ints/strings
2376
+ expected = DatetimeIndex (['NaT' ,
2377
+ '2015-06-19 05:33:20' ,
2378
+ '2015-05-27 22:33:20' ])
2379
+ arr = ['foo' , 1.434692e+18 , 1.432766e+18 ]
2380
+ result = pd .to_datetime (arr , errors = 'coerce' )
2381
+ tm .assert_index_equal (result , expected )
2382
+
2383
+ expected = DatetimeIndex (['2015-06-19 05:33:20' ,
2384
+ '2015-05-27 22:33:20' ,
2385
+ 'NaT' ,
2386
+ 'NaT' ])
2387
+ arr = [1.434692e+18 , 1.432766e+18 , 'foo' , 'NaT' ]
2388
+ result = pd .to_datetime (arr , errors = 'coerce' )
2389
+ tm .assert_index_equal (result , expected )
2390
+
2391
+ def test_unit_mixed (self ):
2392
+
2393
+ # mixed integers/datetimes
2394
+ expected = DatetimeIndex (['2013-01-01' , 'NaT' , 'NaT' ])
2395
+ arr = [pd .Timestamp ('20130101' ), 1.434692e+18 , 1.432766e+18 ]
2396
+ result = pd .to_datetime (arr , errors = 'coerce' )
2397
+ tm .assert_index_equal (result , expected )
2398
+
2399
+ with self .assertRaises (ValueError ):
2400
+ pd .to_datetime (arr , errors = 'raise' )
2401
+
2402
+ expected = DatetimeIndex (['NaT' ,
2403
+ 'NaT' ,
2404
+ '2013-01-01' ])
2405
+ arr = [1.434692e+18 , 1.432766e+18 , pd .Timestamp ('20130101' )]
2406
+ result = pd .to_datetime (arr , errors = 'coerce' )
2407
+ tm .assert_index_equal (result , expected )
2408
+
2409
+ with self .assertRaises (ValueError ):
2410
+ pd .to_datetime (arr , errors = 'raise' )
2411
+
2286
2412
def test_index_to_datetime (self ):
2287
2413
idx = Index (['1/1/2000' , '1/2/2000' , '1/3/2000' ])
2288
2414
@@ -4229,68 +4355,6 @@ def check(val, unit=None, h=1, s=1, us=0):
4229
4355
result = Timestamp ('NaT' )
4230
4356
self .assertIs (result , NaT )
4231
4357
4232
- def test_unit_errors (self ):
4233
- # GH 11758
4234
- # test proper behavior with erros
4235
-
4236
- with self .assertRaises (ValueError ):
4237
- to_datetime ([1 ], unit = 'D' , format = '%Y%m%d' )
4238
-
4239
- values = [11111111 , 1 , 1.0 , tslib .iNaT , pd .NaT , np .nan ,
4240
- 'NaT' , '' ]
4241
- result = to_datetime (values , unit = 'D' , errors = 'ignore' )
4242
- expected = Index ([11111111 , Timestamp ('1970-01-02' ),
4243
- Timestamp ('1970-01-02' ), pd .NaT ,
4244
- pd .NaT , pd .NaT , pd .NaT , pd .NaT ],
4245
- dtype = object )
4246
- tm .assert_index_equal (result , expected )
4247
-
4248
- result = to_datetime (values , unit = 'D' , errors = 'coerce' )
4249
- expected = DatetimeIndex (['NaT' , '1970-01-02' , '1970-01-02' ,
4250
- 'NaT' , 'NaT' , 'NaT' , 'NaT' , 'NaT' ])
4251
- tm .assert_index_equal (result , expected )
4252
-
4253
- with self .assertRaises (tslib .OutOfBoundsDatetime ):
4254
- to_datetime (values , unit = 'D' , errors = 'raise' )
4255
-
4256
- values = [1420043460000 , tslib .iNaT , pd .NaT , np .nan , 'NaT' ]
4257
-
4258
- result = to_datetime (values , errors = 'ignore' , unit = 's' )
4259
- expected = Index ([1420043460000 , pd .NaT , pd .NaT ,
4260
- pd .NaT , pd .NaT ], dtype = object )
4261
- tm .assert_index_equal (result , expected )
4262
-
4263
- result = to_datetime (values , errors = 'coerce' , unit = 's' )
4264
- expected = DatetimeIndex (['NaT' , 'NaT' , 'NaT' , 'NaT' , 'NaT' ])
4265
- tm .assert_index_equal (result , expected )
4266
-
4267
- with self .assertRaises (tslib .OutOfBoundsDatetime ):
4268
- to_datetime (values , errors = 'raise' , unit = 's' )
4269
-
4270
- # if we have a string, then we raise a ValueError
4271
- # and NOT an OutOfBoundsDatetime
4272
- for val in ['foo' , Timestamp ('20130101' )]:
4273
- try :
4274
- to_datetime (val , errors = 'raise' , unit = 's' )
4275
- except tslib .OutOfBoundsDatetime :
4276
- raise AssertionError ("incorrect exception raised" )
4277
- except ValueError :
4278
- pass
4279
-
4280
- # consistency of conversions
4281
- expected = Timestamp ('1970-05-09 14:25:11' )
4282
- result = pd .to_datetime (11111111 , unit = 's' , errors = 'raise' )
4283
- self .assertEqual (result , expected )
4284
- self .assertIsInstance (result , Timestamp )
4285
-
4286
- result = pd .to_datetime (11111111 , unit = 's' , errors = 'coerce' )
4287
- self .assertEqual (result , expected )
4288
- self .assertIsInstance (result , Timestamp )
4289
-
4290
- result = pd .to_datetime (11111111 , unit = 's' , errors = 'ignore' )
4291
- self .assertEqual (result , expected )
4292
- self .assertIsInstance (result , Timestamp )
4293
-
4294
4358
def test_roundtrip (self ):
4295
4359
4296
4360
# test value to string and back conversions
0 commit comments