@@ -306,25 +306,6 @@ def test_to_datetime_tz_psycopg2(self):
306
306
dtype = 'datetime64[ns, UTC]' )
307
307
tm .assert_index_equal (result , expected )
308
308
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
309
def test_datetime_invalid_datatype (self ):
329
310
# GH13176
330
311
@@ -409,10 +390,10 @@ def test_unit_with_numeric(self):
409
390
arr1 = [1.434692e+18 , 1.432766e+18 ]
410
391
arr2 = np .array (arr1 ).astype ('int64' )
411
392
for errors in ['ignore' , 'raise' , 'coerce' ]:
412
- result = pd .to_datetime (arr1 , errors = errors )
393
+ result = pd .to_datetime (arr1 , unit = 'ns' , errors = errors )
413
394
tm .assert_index_equal (result , expected )
414
395
415
- result = pd .to_datetime (arr2 , errors = errors )
396
+ result = pd .to_datetime (arr2 , unit = 'ns' , errors = errors )
416
397
tm .assert_index_equal (result , expected )
417
398
418
399
# but we want to make sure that we are coercing
@@ -421,15 +402,15 @@ def test_unit_with_numeric(self):
421
402
'2015-06-19 05:33:20' ,
422
403
'2015-05-27 22:33:20' ])
423
404
arr = ['foo' , 1.434692e+18 , 1.432766e+18 ]
424
- result = pd .to_datetime (arr , errors = 'coerce' )
405
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
425
406
tm .assert_index_equal (result , expected )
426
407
427
408
expected = DatetimeIndex (['2015-06-19 05:33:20' ,
428
409
'2015-05-27 22:33:20' ,
429
410
'NaT' ,
430
411
'NaT' ])
431
412
arr = [1.434692e+18 , 1.432766e+18 , 'foo' , 'NaT' ]
432
- result = pd .to_datetime (arr , errors = 'coerce' )
413
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
433
414
tm .assert_index_equal (result , expected )
434
415
435
416
def test_unit_mixed (self ):
@@ -441,17 +422,17 @@ def test_unit_mixed(self):
441
422
tm .assert_index_equal (result , expected )
442
423
443
424
with pytest .raises (ValueError ):
444
- pd .to_datetime (arr , errors = 'raise' )
425
+ pd .to_datetime (arr , unit = 'ns' , errors = 'raise' )
445
426
446
427
expected = DatetimeIndex (['NaT' ,
447
428
'NaT' ,
448
429
'2013-01-01' ])
449
430
arr = [1.434692e+18 , 1.432766e+18 , pd .Timestamp ('20130101' )]
450
- result = pd .to_datetime (arr , errors = 'coerce' )
431
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
451
432
tm .assert_index_equal (result , expected )
452
433
453
434
with pytest .raises (ValueError ):
454
- pd .to_datetime (arr , errors = 'raise' )
435
+ pd .to_datetime (arr , unit = 'ns' , errors = 'raise' )
455
436
456
437
def test_dataframe (self ):
457
438
@@ -676,36 +657,6 @@ def test_to_datetime_with_apply(self):
676
657
lambda x : pd .to_datetime (x , format = '%b %y' , errors = 'coerce' ))
677
658
assert_series_equal (result , expected )
678
659
679
- def test_to_datetime_types (self ):
680
-
681
- # empty string
682
- result = to_datetime ('' )
683
- assert result is NaT
684
-
685
- result = to_datetime (['' , '' ])
686
- assert isnull (result ).all ()
687
-
688
- # ints
689
- result = Timestamp (0 )
690
- expected = to_datetime (0 )
691
- assert result == expected
692
-
693
- # GH 3888 (strings)
694
- expected = to_datetime (['2012' ])[0 ]
695
- result = to_datetime ('2012' )
696
- assert result == expected
697
-
698
- # array = ['2012','20120101','20120101 12:01:01']
699
- array = ['20120101' , '20120101 12:01:01' ]
700
- expected = list (to_datetime (array ))
701
- result = lmap (Timestamp , array )
702
- tm .assert_almost_equal (result , expected )
703
-
704
- # currently fails ###
705
- # result = Timestamp('2012')
706
- # expected = to_datetime('2012')
707
- # assert result == expected
708
-
709
660
def test_to_datetime_unprocessable_input (self ):
710
661
# GH 4928
711
662
tm .assert_numpy_array_equal (
@@ -1517,6 +1468,16 @@ def julian_dates():
1517
1468
return pd .date_range ('2014-1-1' , periods = 10 ).to_julian_date ().values
1518
1469
1519
1470
1471
+ @pytest .fixture (params = [True , False ])
1472
+ def bool_values (request ):
1473
+ return request .param
1474
+
1475
+
1476
+ @pytest .fixture (params = ['coerce' , 'ignore' ])
1477
+ def errors_values (request ):
1478
+ return request .param
1479
+
1480
+
1520
1481
class TestOrigin (object ):
1521
1482
1522
1483
def test_to_basic (self , julian_dates ):
@@ -1604,3 +1565,63 @@ def test_processing_order(self):
1604
1565
result = pd .to_datetime (300 * 365 , unit = 'D' , origin = '1870-01-01' )
1605
1566
expected = Timestamp ('2169-10-20 00:00:00' )
1606
1567
assert result == expected
1568
+
1569
+ def test_datetime_dataframe_with_unit (self , units ):
1570
+ df = DataFrame ({'year' : [2000 , 2001 ],
1571
+ 'month' : [1.5 , 1 ],
1572
+ 'day' : [1 , 1 ]})
1573
+ with pytest .raises (ValueError ):
1574
+ to_datetime (df , unit = units )
1575
+
1576
+ def test_datetime_bool (self , units , bool_values , errors_values ):
1577
+ # GH13176
1578
+ with pytest .raises (TypeError ):
1579
+ to_datetime (bool_values , unit = units )
1580
+ with pytest .raises (TypeError ):
1581
+ to_datetime (bool_values , unit = units , errors = errors_values )
1582
+ with pytest .raises (TypeError ):
1583
+ to_datetime ([bool_values , datetime .today ()], unit = units )
1584
+ with pytest .raises (TypeError ):
1585
+ to_datetime (['20130101' , bool_values ])
1586
+
1587
+ tm .assert_index_equal (to_datetime ([0 , bool_values , NaT , 0.0 ],
1588
+ errors = "coerce" ),
1589
+ DatetimeIndex ([to_datetime (0 , unit = units ), NaT ,
1590
+ NaT , to_datetime (0 , unit = units )]))
1591
+
1592
+ def test_to_datetime_types (self , units ):
1593
+
1594
+ # empty string
1595
+ result = to_datetime ('' )
1596
+ assert result is NaT
1597
+
1598
+ result = to_datetime (['' , '' ])
1599
+ assert (isnull (result ).all ())
1600
+
1601
+ # ints
1602
+ result = Timestamp (0 )
1603
+ expected = to_datetime (0 , unit = units )
1604
+ assert result == expected
1605
+
1606
+ # GH 3888 (strings)
1607
+ expected = to_datetime (['2012' ])[0 ]
1608
+ result = to_datetime ('2012' )
1609
+ assert result == expected
1610
+
1611
+ # array = ['2012','20120101','20120101 12:01:01']
1612
+ array = ['20120101' , '20120101 12:01:01' ]
1613
+ expected = list (to_datetime (array ))
1614
+ result = lmap (Timestamp , array )
1615
+ tm .assert_almost_equal (result , expected )
1616
+
1617
+ # currently fails ###
1618
+ # result = Timestamp('2012')
1619
+ # expected = to_datetime('2012')
1620
+ # assert result == expected
1621
+
1622
+ @pytest .mark .parametrize ('arg' , [int (1 ), float (1 ), range (5 ),
1623
+ np .array (range (5 ), 'd' )])
1624
+ def test_to_datetime_numerical_input (self , arg ):
1625
+ # GH15836
1626
+ with pytest .raises (ValueError ):
1627
+ pd .to_datetime (arg )
0 commit comments