25
25
compat )
26
26
27
27
28
+ @pytest .fixture (params = ['D' , 's' , 'ms' , 'us' , 'ns' ])
29
+ def units (request ):
30
+ return request .param
31
+
32
+
33
+ @pytest .fixture
34
+ def epoch_1960 ():
35
+ # for origin as 1960-01-01
36
+ return Timestamp ('1960-01-01' )
37
+
38
+
39
+ @pytest .fixture
40
+ def units_from_epochs ():
41
+ return list (range (5 ))
42
+
43
+
44
+ @pytest .fixture (params = [epoch_1960 (),
45
+ epoch_1960 ().to_pydatetime (),
46
+ epoch_1960 ().to_datetime64 (),
47
+ str (epoch_1960 ())])
48
+ def epochs (request ):
49
+ return request .param
50
+
51
+
52
+ @pytest .fixture
53
+ def julian_dates ():
54
+ return pd .date_range ('2014-1-1' , periods = 10 ).to_julian_date ().values
55
+
56
+
28
57
class TimeConversionFormats (object ):
29
58
30
59
def test_to_datetime_format (self ):
@@ -306,25 +335,6 @@ def test_to_datetime_tz_psycopg2(self):
306
335
dtype = 'datetime64[ns, UTC]' )
307
336
tm .assert_index_equal (result , expected )
308
337
309
- def test_datetime_bool (self ):
310
- # GH13176
311
- with pytest .raises (TypeError ):
312
- to_datetime (False )
313
- assert to_datetime (False , errors = "coerce" ) is NaT
314
- assert to_datetime (False , errors = "ignore" ) is False
315
- with pytest .raises (TypeError ):
316
- to_datetime (True )
317
- assert to_datetime (True , errors = "coerce" ) is NaT
318
- assert to_datetime (True , errors = "ignore" ) is True
319
- with pytest .raises (TypeError ):
320
- to_datetime ([False , datetime .today ()])
321
- with pytest .raises (TypeError ):
322
- to_datetime (['20130101' , True ])
323
- tm .assert_index_equal (to_datetime ([0 , False , NaT , 0.0 ],
324
- errors = "coerce" ),
325
- DatetimeIndex ([to_datetime (0 ), NaT ,
326
- NaT , to_datetime (0 )]))
327
-
328
338
def test_datetime_invalid_datatype (self ):
329
339
# GH13176
330
340
@@ -334,7 +344,27 @@ def test_datetime_invalid_datatype(self):
334
344
pd .to_datetime (pd .to_datetime )
335
345
336
346
337
- class ToDatetimeUnit (object ):
347
+ class TestToDatetimeUnit (object ):
348
+
349
+ def test_datetime_bool (self , units ):
350
+ # GH13176
351
+ with pytest .raises (TypeError ):
352
+ to_datetime (False , unit = units )
353
+ assert to_datetime (False , unit = units , errors = "coerce" ) is NaT
354
+ assert to_datetime (False , unit = units , errors = "ignore" ) == False
355
+ with pytest .raises (TypeError ):
356
+ to_datetime (True , unit = units )
357
+ assert to_datetime (True , unit = units , errors = "coerce" ) is NaT
358
+ assert to_datetime (True , unit = units , errors = "ignore" ) == True
359
+ with pytest .raises (TypeError ):
360
+ to_datetime ([False , datetime .today ()], unit = units )
361
+ with pytest .raises (TypeError ):
362
+ to_datetime ([True , '20130101' ], unit = units )
363
+
364
+ tm .assert_index_equal (to_datetime ([0 , False , NaT , 0.0 ],
365
+ errors = "coerce" ),
366
+ DatetimeIndex ([to_datetime (0 , unit = units ), NaT ,
367
+ NaT , to_datetime (0 , unit = units )]))
338
368
339
369
def test_unit (self ):
340
370
# GH 11758
@@ -409,10 +439,10 @@ def test_unit_with_numeric(self):
409
439
arr1 = [1.434692e+18 , 1.432766e+18 ]
410
440
arr2 = np .array (arr1 ).astype ('int64' )
411
441
for errors in ['ignore' , 'raise' , 'coerce' ]:
412
- result = pd .to_datetime (arr1 , errors = errors )
442
+ result = pd .to_datetime (arr1 , unit = 'ns' , errors = errors )
413
443
tm .assert_index_equal (result , expected )
414
444
415
- result = pd .to_datetime (arr2 , errors = errors )
445
+ result = pd .to_datetime (arr2 , unit = 'ns' , errors = errors )
416
446
tm .assert_index_equal (result , expected )
417
447
418
448
# but we want to make sure that we are coercing
@@ -421,37 +451,37 @@ def test_unit_with_numeric(self):
421
451
'2015-06-19 05:33:20' ,
422
452
'2015-05-27 22:33:20' ])
423
453
arr = ['foo' , 1.434692e+18 , 1.432766e+18 ]
424
- result = pd .to_datetime (arr , errors = 'coerce' )
454
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
425
455
tm .assert_index_equal (result , expected )
426
456
427
457
expected = DatetimeIndex (['2015-06-19 05:33:20' ,
428
458
'2015-05-27 22:33:20' ,
429
459
'NaT' ,
430
460
'NaT' ])
431
461
arr = [1.434692e+18 , 1.432766e+18 , 'foo' , 'NaT' ]
432
- result = pd .to_datetime (arr , errors = 'coerce' )
462
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
433
463
tm .assert_index_equal (result , expected )
434
464
435
465
def test_unit_mixed (self ):
436
466
437
467
# mixed integers/datetimes
438
468
expected = DatetimeIndex (['2013-01-01' , 'NaT' , 'NaT' ])
439
469
arr = [pd .Timestamp ('20130101' ), 1.434692e+18 , 1.432766e+18 ]
440
- result = pd .to_datetime (arr , errors = 'coerce' )
470
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
441
471
tm .assert_index_equal (result , expected )
442
472
443
473
with pytest .raises (ValueError ):
444
- pd .to_datetime (arr , errors = 'raise' )
474
+ pd .to_datetime (arr , unit = 'ns' , errors = 'raise' )
445
475
446
476
expected = DatetimeIndex (['NaT' ,
447
477
'NaT' ,
448
478
'2013-01-01' ])
449
479
arr = [1.434692e+18 , 1.432766e+18 , pd .Timestamp ('20130101' )]
450
- result = pd .to_datetime (arr , errors = 'coerce' )
480
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
451
481
tm .assert_index_equal (result , expected )
452
482
453
483
with pytest .raises (ValueError ):
454
- pd .to_datetime (arr , errors = 'raise' )
484
+ pd .to_datetime (arr , unit = 'ns' , errors = 'raise' )
455
485
456
486
def test_dataframe (self ):
457
487
@@ -1488,35 +1518,6 @@ def test_normalize_date():
1488
1518
assert (result == datetime (2012 , 9 , 7 ))
1489
1519
1490
1520
1491
- @pytest .fixture (params = ['D' , 's' , 'ms' , 'us' , 'ns' ])
1492
- def units (request ):
1493
- return request .param
1494
-
1495
-
1496
- @pytest .fixture
1497
- def epoch_1960 ():
1498
- # for origin as 1960-01-01
1499
- return Timestamp ('1960-01-01' )
1500
-
1501
-
1502
- @pytest .fixture
1503
- def units_from_epochs ():
1504
- return list (range (5 ))
1505
-
1506
-
1507
- @pytest .fixture (params = [epoch_1960 (),
1508
- epoch_1960 ().to_pydatetime (),
1509
- epoch_1960 ().to_datetime64 (),
1510
- str (epoch_1960 ())])
1511
- def epochs (request ):
1512
- return request .param
1513
-
1514
-
1515
- @pytest .fixture
1516
- def julian_dates ():
1517
- return pd .date_range ('2014-1-1' , periods = 10 ).to_julian_date ().values
1518
-
1519
-
1520
1521
class TestOrigin (object ):
1521
1522
1522
1523
def test_to_basic (self , julian_dates ):
0 commit comments