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,28 @@ 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
+ #import pdb; pdb.set_trace()
352
+ with pytest .raises (TypeError ):
353
+ to_datetime (False , unit = units )
354
+ assert to_datetime (False , unit = units , errors = "coerce" ) is NaT
355
+ assert to_datetime (False , unit = units , errors = "ignore" ) == False
356
+ with pytest .raises (TypeError ):
357
+ to_datetime (True , unit = units )
358
+ assert to_datetime (True , unit = units , errors = "coerce" ) is NaT
359
+ assert to_datetime (True , unit = units , errors = "ignore" ) == True
360
+ with pytest .raises (TypeError ):
361
+ to_datetime ([False , datetime .today ()], unit = units )
362
+ with pytest .raises (TypeError ):
363
+ to_datetime ([True , '20130101' ], unit = units )
364
+
365
+ tm .assert_index_equal (to_datetime ([0 , False , NaT , 0.0 ],
366
+ errors = "coerce" ),
367
+ DatetimeIndex ([to_datetime (0 , unit = units ), NaT ,
368
+ NaT , to_datetime (0 , unit = units )]))
338
369
339
370
def test_unit (self ):
340
371
# GH 11758
@@ -409,10 +440,10 @@ def test_unit_with_numeric(self):
409
440
arr1 = [1.434692e+18 , 1.432766e+18 ]
410
441
arr2 = np .array (arr1 ).astype ('int64' )
411
442
for errors in ['ignore' , 'raise' , 'coerce' ]:
412
- result = pd .to_datetime (arr1 , errors = errors )
443
+ result = pd .to_datetime (arr1 , unit = 'ns' , errors = errors )
413
444
tm .assert_index_equal (result , expected )
414
445
415
- result = pd .to_datetime (arr2 , errors = errors )
446
+ result = pd .to_datetime (arr2 , unit = 'ns' , errors = errors )
416
447
tm .assert_index_equal (result , expected )
417
448
418
449
# but we want to make sure that we are coercing
@@ -421,37 +452,37 @@ def test_unit_with_numeric(self):
421
452
'2015-06-19 05:33:20' ,
422
453
'2015-05-27 22:33:20' ])
423
454
arr = ['foo' , 1.434692e+18 , 1.432766e+18 ]
424
- result = pd .to_datetime (arr , errors = 'coerce' )
455
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
425
456
tm .assert_index_equal (result , expected )
426
457
427
458
expected = DatetimeIndex (['2015-06-19 05:33:20' ,
428
459
'2015-05-27 22:33:20' ,
429
460
'NaT' ,
430
461
'NaT' ])
431
462
arr = [1.434692e+18 , 1.432766e+18 , 'foo' , 'NaT' ]
432
- result = pd .to_datetime (arr , errors = 'coerce' )
463
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
433
464
tm .assert_index_equal (result , expected )
434
465
435
466
def test_unit_mixed (self ):
436
467
437
468
# mixed integers/datetimes
438
469
expected = DatetimeIndex (['2013-01-01' , 'NaT' , 'NaT' ])
439
470
arr = [pd .Timestamp ('20130101' ), 1.434692e+18 , 1.432766e+18 ]
440
- result = pd .to_datetime (arr , errors = 'coerce' )
471
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
441
472
tm .assert_index_equal (result , expected )
442
473
443
474
with pytest .raises (ValueError ):
444
- pd .to_datetime (arr , errors = 'raise' )
475
+ pd .to_datetime (arr , unit = 'ns' , errors = 'raise' )
445
476
446
477
expected = DatetimeIndex (['NaT' ,
447
478
'NaT' ,
448
479
'2013-01-01' ])
449
480
arr = [1.434692e+18 , 1.432766e+18 , pd .Timestamp ('20130101' )]
450
- result = pd .to_datetime (arr , errors = 'coerce' )
481
+ result = pd .to_datetime (arr , unit = 'ns' , errors = 'coerce' )
451
482
tm .assert_index_equal (result , expected )
452
483
453
484
with pytest .raises (ValueError ):
454
- pd .to_datetime (arr , errors = 'raise' )
485
+ pd .to_datetime (arr , unit = 'ns' , errors = 'raise' )
455
486
456
487
def test_dataframe (self ):
457
488
@@ -1488,35 +1519,6 @@ def test_normalize_date():
1488
1519
assert (result == datetime (2012 , 9 , 7 ))
1489
1520
1490
1521
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
1522
class TestOrigin (object ):
1521
1523
1522
1524
def test_to_basic (self , julian_dates ):
0 commit comments