25
25
DatetimeIndex , TimedeltaIndex )
26
26
27
27
28
+ def assert_all (obj ):
29
+ """
30
+ Test helper to call call obj.all() the appropriate number of times on
31
+ a Series or DataFrame.
32
+ """
33
+ if isinstance (obj , pd .DataFrame ):
34
+ assert obj .all ().all ()
35
+ else :
36
+ assert obj .all ()
37
+
38
+
28
39
# ------------------------------------------------------------------
29
40
# Comparisons
30
41
@@ -86,11 +97,16 @@ def test_comparison_invalid(self, box_with_array):
86
97
[Period ('2011-01' , freq = 'M' ), NaT , Period ('2011-03' , freq = 'M' )]
87
98
])
88
99
@pytest .mark .parametrize ('dtype' , [None , object ])
89
- def test_nat_comparisons_scalar (self , dtype , data , box ):
90
- xbox = box if box is not pd .Index else np .ndarray
100
+ def test_nat_comparisons_scalar (self , dtype , data , box_with_array ):
101
+ if box_with_array is tm .to_array and dtype is object :
102
+ # dont bother testing ndarray comparison methods as this fails
103
+ # on older numpys (since they check object identity)
104
+ return
105
+
106
+ xbox = box_with_array if box_with_array is not pd .Index else np .ndarray
91
107
92
108
left = Series (data , dtype = dtype )
93
- left = tm .box_expected (left , box )
109
+ left = tm .box_expected (left , box_with_array )
94
110
95
111
expected = [False , False , False ]
96
112
expected = tm .box_expected (expected , xbox )
@@ -290,23 +306,24 @@ def test_dti_cmp_datetimelike(self, other, tz_naive_fixture):
290
306
expected = np .array ([True , False ])
291
307
tm .assert_numpy_array_equal (result , expected )
292
308
293
- def dti_cmp_non_datetime (self , tz_naive_fixture ):
309
+ def dt64arr_cmp_non_datetime (self , tz_naive_fixture , box_with_array ):
294
310
# GH#19301 by convention datetime.date is not considered comparable
295
311
# to Timestamp or DatetimeIndex. This may change in the future.
296
312
tz = tz_naive_fixture
297
313
dti = pd .date_range ('2016-01-01' , periods = 2 , tz = tz )
314
+ dtarr = tm .box_expected (dti , box_with_array )
298
315
299
316
other = datetime (2016 , 1 , 1 ).date ()
300
- assert not (dti == other ).any ()
301
- assert (dti != other ).all ()
317
+ assert not (dtarr == other ).any ()
318
+ assert (dtarr != other ).all ()
302
319
with pytest .raises (TypeError ):
303
- dti < other
320
+ dtarr < other
304
321
with pytest .raises (TypeError ):
305
- dti <= other
322
+ dtarr <= other
306
323
with pytest .raises (TypeError ):
307
- dti > other
324
+ dtarr > other
308
325
with pytest .raises (TypeError ):
309
- dti >= other
326
+ dtarr >= other
310
327
311
328
@pytest .mark .parametrize ('other' , [None , np .nan , pd .NaT ])
312
329
def test_dti_eq_null_scalar (self , other , tz_naive_fixture ):
@@ -323,49 +340,67 @@ def test_dti_ne_null_scalar(self, other, tz_naive_fixture):
323
340
assert (dti != other ).all ()
324
341
325
342
@pytest .mark .parametrize ('other' , [None , np .nan ])
326
- def test_dti_cmp_null_scalar_inequality (self , tz_naive_fixture , other ):
343
+ def test_dti_cmp_null_scalar_inequality (self , tz_naive_fixture , other ,
344
+ box_with_array ):
327
345
# GH#19301
328
346
tz = tz_naive_fixture
329
347
dti = pd .date_range ('2016-01-01' , periods = 2 , tz = tz )
348
+ # FIXME: ValueError with transpose
349
+ dtarr = tm .box_expected (dti , box_with_array , transpose = False )
330
350
331
351
with pytest .raises (TypeError ):
332
- dti < other
352
+ dtarr < other
333
353
with pytest .raises (TypeError ):
334
- dti <= other
354
+ dtarr <= other
335
355
with pytest .raises (TypeError ):
336
- dti > other
356
+ dtarr > other
337
357
with pytest .raises (TypeError ):
338
- dti >= other
358
+ dtarr >= other
339
359
340
360
@pytest .mark .parametrize ('dtype' , [None , object ])
341
- def test_dti_cmp_nat (self , dtype ):
361
+ def test_dti_cmp_nat (self , dtype , box_with_array ):
362
+ if box_with_array is tm .to_array and dtype is object :
363
+ # dont bother testing ndarray comparison methods as this fails
364
+ # on older numpys (since they check object identity)
365
+ return
366
+
367
+ xbox = box_with_array if box_with_array is not pd .Index else np .ndarray
368
+
342
369
left = pd .DatetimeIndex ([pd .Timestamp ('2011-01-01' ), pd .NaT ,
343
370
pd .Timestamp ('2011-01-03' )])
344
371
right = pd .DatetimeIndex ([pd .NaT , pd .NaT , pd .Timestamp ('2011-01-03' )])
345
372
373
+ left = tm .box_expected (left , box_with_array )
374
+ right = tm .box_expected (right , box_with_array )
375
+
346
376
lhs , rhs = left , right
347
377
if dtype is object :
348
378
lhs , rhs = left .astype (object ), right .astype (object )
349
379
350
380
result = rhs == lhs
351
381
expected = np .array ([False , False , True ])
352
- tm .assert_numpy_array_equal (result , expected )
382
+ expected = tm .box_expected (expected , xbox )
383
+ tm .assert_equal (result , expected )
353
384
354
385
result = lhs != rhs
355
386
expected = np .array ([True , True , False ])
356
- tm .assert_numpy_array_equal (result , expected )
387
+ expected = tm .box_expected (expected , xbox )
388
+ tm .assert_equal (result , expected )
357
389
358
390
expected = np .array ([False , False , False ])
359
- tm .assert_numpy_array_equal (lhs == pd .NaT , expected )
360
- tm .assert_numpy_array_equal (pd .NaT == rhs , expected )
391
+ expected = tm .box_expected (expected , xbox )
392
+ tm .assert_equal (lhs == pd .NaT , expected )
393
+ tm .assert_equal (pd .NaT == rhs , expected )
361
394
362
395
expected = np .array ([True , True , True ])
363
- tm .assert_numpy_array_equal (lhs != pd .NaT , expected )
364
- tm .assert_numpy_array_equal (pd .NaT != lhs , expected )
396
+ expected = tm .box_expected (expected , xbox )
397
+ tm .assert_equal (lhs != pd .NaT , expected )
398
+ tm .assert_equal (pd .NaT != lhs , expected )
365
399
366
400
expected = np .array ([False , False , False ])
367
- tm .assert_numpy_array_equal (lhs < pd .NaT , expected )
368
- tm .assert_numpy_array_equal (pd .NaT > lhs , expected )
401
+ expected = tm .box_expected (expected , xbox )
402
+ tm .assert_equal (lhs < pd .NaT , expected )
403
+ tm .assert_equal (pd .NaT > lhs , expected )
369
404
370
405
def test_dti_cmp_nat_behaves_like_float_cmp_nan (self ):
371
406
fidx1 = pd .Index ([1.0 , np .nan , 3.0 , np .nan , 5.0 , 7.0 ])
@@ -459,36 +494,47 @@ def test_dti_cmp_nat_behaves_like_float_cmp_nan(self):
459
494
@pytest .mark .parametrize ('op' , [operator .eq , operator .ne ,
460
495
operator .gt , operator .ge ,
461
496
operator .lt , operator .le ])
462
- def test_comparison_tzawareness_compat (self , op ):
497
+ def test_comparison_tzawareness_compat (self , op , box_with_array ):
463
498
# GH#18162
464
499
dr = pd .date_range ('2016-01-01' , periods = 6 )
465
500
dz = dr .tz_localize ('US/Pacific' )
466
501
502
+ # FIXME: ValueError with transpose
503
+ dr = tm .box_expected (dr , box_with_array , transpose = False )
504
+ dz = tm .box_expected (dz , box_with_array , transpose = False )
505
+
467
506
with pytest .raises (TypeError ):
468
507
op (dr , dz )
469
- with pytest .raises (TypeError ):
470
- op (dr , list (dz ))
508
+ if box_with_array is not pd .DataFrame :
509
+ # DataFrame op is invalid until transpose bug is fixed
510
+ with pytest .raises (TypeError ):
511
+ op (dr , list (dz ))
471
512
with pytest .raises (TypeError ):
472
513
op (dz , dr )
473
- with pytest .raises (TypeError ):
474
- op (dz , list (dr ))
514
+ if box_with_array is not pd .DataFrame :
515
+ # DataFrame op is invalid until transpose bug is fixed
516
+ with pytest .raises (TypeError ):
517
+ op (dz , list (dr ))
475
518
476
519
# Check that there isn't a problem aware-aware and naive-naive do not
477
520
# raise
478
- assert (dr == dr ).all ()
479
- assert (dr == list (dr )).all ()
480
- assert (dz == dz ).all ()
481
- assert (dz == list (dz )).all ()
521
+ assert_all (dr == dr )
522
+ assert_all (dz == dz )
523
+ if box_with_array is not pd .DataFrame :
524
+ # DataFrame doesn't align the lists correctly unless we transpose,
525
+ # which we cannot do at the moment
526
+ assert (dr == list (dr )).all ()
527
+ assert (dz == list (dz )).all ()
482
528
483
529
# Check comparisons against scalar Timestamps
484
530
ts = pd .Timestamp ('2000-03-14 01:59' )
485
531
ts_tz = pd .Timestamp ('2000-03-14 01:59' , tz = 'Europe/Amsterdam' )
486
532
487
- assert (dr > ts ). all ( )
533
+ assert_all (dr > ts )
488
534
with pytest .raises (TypeError ):
489
535
op (dr , ts_tz )
490
536
491
- assert (dz > ts_tz ). all ( )
537
+ assert_all (dz > ts_tz )
492
538
with pytest .raises (TypeError ):
493
539
op (dz , ts )
494
540
@@ -502,13 +548,18 @@ def test_comparison_tzawareness_compat(self, op):
502
548
@pytest .mark .parametrize ('other' , [datetime (2016 , 1 , 1 ),
503
549
Timestamp ('2016-01-01' ),
504
550
np .datetime64 ('2016-01-01' )])
505
- def test_scalar_comparison_tzawareness (self , op , other , tz_aware_fixture ):
551
+ def test_scalar_comparison_tzawareness (self , op , other , tz_aware_fixture ,
552
+ box_with_array ):
506
553
tz = tz_aware_fixture
507
554
dti = pd .date_range ('2016-01-01' , periods = 2 , tz = tz )
555
+
556
+ # FIXME: ValueError with transpose
557
+ dtarr = tm .box_expected (dti , box_with_array , transpose = False )
558
+
508
559
with pytest .raises (TypeError ):
509
- op (dti , other )
560
+ op (dtarr , other )
510
561
with pytest .raises (TypeError ):
511
- op (other , dti )
562
+ op (other , dtarr )
512
563
513
564
@pytest .mark .parametrize ('op' , [operator .eq , operator .ne ,
514
565
operator .gt , operator .ge ,
@@ -558,18 +609,25 @@ def test_dti_cmp_str(self, tz_naive_fixture):
558
609
559
610
@pytest .mark .parametrize ('other' , ['foo' , 99 , 4.0 ,
560
611
object (), timedelta (days = 2 )])
561
- def test_dti_cmp_scalar_invalid (self , other , tz_naive_fixture ):
612
+ def test_dt64arr_cmp_scalar_invalid (self , other , tz_naive_fixture ,
613
+ box_with_array ):
562
614
# GH#22074
563
615
tz = tz_naive_fixture
616
+ xbox = box_with_array if box_with_array is not pd .Index else np .ndarray
617
+
564
618
rng = date_range ('1/1/2000' , periods = 10 , tz = tz )
619
+ # FIXME: ValueError with transpose
620
+ rng = tm .box_expected (rng , box_with_array , transpose = False )
565
621
566
622
result = rng == other
567
623
expected = np .array ([False ] * 10 )
568
- tm .assert_numpy_array_equal (result , expected )
624
+ expected = tm .box_expected (expected , xbox , transpose = False )
625
+ tm .assert_equal (result , expected )
569
626
570
627
result = rng != other
571
628
expected = np .array ([True ] * 10 )
572
- tm .assert_numpy_array_equal (result , expected )
629
+ expected = tm .box_expected (expected , xbox , transpose = False )
630
+ tm .assert_equal (result , expected )
573
631
574
632
with pytest .raises (TypeError ):
575
633
rng < other
0 commit comments