@@ -433,7 +433,7 @@ def test_hash_vs_equality(self):
433
433
assert dtype2 == dtype
434
434
assert dtype3 == dtype
435
435
assert dtype is dtype2
436
- assert dtype2 is dtype
436
+ assert dtype2 is dtype3
437
437
assert dtype3 is dtype
438
438
assert hash (dtype ) == hash (dtype2 )
439
439
assert hash (dtype ) == hash (dtype3 )
@@ -451,14 +451,19 @@ def test_hash_vs_equality(self):
451
451
assert hash (dtype2 ) == hash (dtype2 )
452
452
assert hash (dtype2 ) == hash (dtype3 )
453
453
454
- def test_construction (self ):
455
- with pytest .raises (ValueError ):
456
- IntervalDtype ('xx' )
454
+ @pytest .mark .parametrize ('subtype' , [
455
+ 'interval[int64]' , 'Interval[int64]' , 'int64' , np .dtype ('int64' )])
456
+ def test_construction (self , subtype ):
457
+ i = IntervalDtype (subtype )
458
+ assert i .subtype == np .dtype ('int64' )
459
+ assert is_interval_dtype (i )
457
460
458
- for s in ['interval[int64]' , 'Interval[int64]' , 'int64' ]:
459
- i = IntervalDtype (s )
460
- assert i .subtype == np .dtype ('int64' )
461
- assert is_interval_dtype (i )
461
+ @pytest .mark .parametrize ('subtype' , [None , 'interval' , 'Interval' ])
462
+ def test_construction_generic (self , subtype ):
463
+ # generic
464
+ i = IntervalDtype (subtype )
465
+ assert i .subtype is None
466
+ assert is_interval_dtype (i )
462
467
463
468
@pytest .mark .parametrize ('subtype' , [
464
469
CategoricalDtype (list ('abc' ), False ),
@@ -471,17 +476,27 @@ def test_construction_not_supported(self, subtype):
471
476
with tm .assert_raises_regex (TypeError , msg ):
472
477
IntervalDtype (subtype )
473
478
474
- def test_construction_generic (self ):
475
- # generic
476
- i = IntervalDtype ('interval' )
477
- assert i .subtype == ''
478
- assert is_interval_dtype (i )
479
- assert str (i ) == 'interval[]'
479
+ def test_construction_errors (self ):
480
+ msg = 'could not construct IntervalDtype'
481
+ with tm .assert_raises_regex (ValueError , msg ):
482
+ IntervalDtype ('xx' )
480
483
481
- i = IntervalDtype ()
482
- assert i .subtype is None
483
- assert is_interval_dtype (i )
484
- assert str (i ) == 'interval'
484
+ def test_construction_from_string (self ):
485
+ result = IntervalDtype ('interval[int64]' )
486
+ assert is_dtype_equal (self .dtype , result )
487
+ result = IntervalDtype .construct_from_string ('interval[int64]' )
488
+ assert is_dtype_equal (self .dtype , result )
489
+
490
+ @pytest .mark .parametrize ('string' , [
491
+ 'foo' , 'interval[foo]' , 'foo[int64]' , 0 , 3.14 , ('a' , 'b' ), None ])
492
+ def test_construction_from_string_errors (self , string ):
493
+ if isinstance (string , string_types ):
494
+ error , msg = ValueError , 'could not construct IntervalDtype'
495
+ else :
496
+ error , msg = TypeError , 'a string needs to be passed, got type'
497
+
498
+ with tm .assert_raises_regex (error , msg ):
499
+ IntervalDtype .construct_from_string (string )
485
500
486
501
def test_subclass (self ):
487
502
a = IntervalDtype ('interval[int64]' )
@@ -506,36 +521,45 @@ def test_is_dtype(self):
506
521
assert not IntervalDtype .is_dtype (np .int64 )
507
522
assert not IntervalDtype .is_dtype (np .float64 )
508
523
509
- def test_identity (self ):
510
- assert (IntervalDtype ('interval[int64]' ) ==
511
- IntervalDtype ('interval[int64]' ))
512
-
513
524
def test_coerce_to_dtype (self ):
514
525
assert (_coerce_to_dtype ('interval[int64]' ) ==
515
526
IntervalDtype ('interval[int64]' ))
516
527
517
- def test_construction_from_string (self ):
518
- result = IntervalDtype ('interval[int64]' )
519
- assert is_dtype_equal (self .dtype , result )
520
- result = IntervalDtype .construct_from_string ('interval[int64]' )
521
- assert is_dtype_equal (self .dtype , result )
522
- with pytest .raises (TypeError ):
523
- IntervalDtype .construct_from_string ('foo' )
524
- with pytest .raises (TypeError ):
525
- IntervalDtype .construct_from_string ('interval[foo]' )
526
- with pytest .raises (TypeError ):
527
- IntervalDtype .construct_from_string ('foo[int64]' )
528
-
529
528
def test_equality (self ):
530
529
assert is_dtype_equal (self .dtype , 'interval[int64]' )
531
530
assert is_dtype_equal (self .dtype , IntervalDtype ('int64' ))
532
- assert is_dtype_equal (self .dtype , IntervalDtype ('int64' ))
533
531
assert is_dtype_equal (IntervalDtype ('int64' ), IntervalDtype ('int64' ))
534
532
535
533
assert not is_dtype_equal (self .dtype , 'int64' )
536
534
assert not is_dtype_equal (IntervalDtype ('int64' ),
537
535
IntervalDtype ('float64' ))
538
536
537
+ @pytest .mark .parametrize ('subtype' , [
538
+ None , 'interval' , 'Interval' , 'int64' , 'uint64' , 'float64' ,
539
+ 'complex128' , 'datetime64' , 'timedelta64' , PeriodDtype ('Q' )])
540
+ def test_equality_generic (self , subtype ):
541
+ # GH 18980
542
+ dtype = IntervalDtype (subtype )
543
+ assert is_dtype_equal (dtype , 'interval' )
544
+ assert is_dtype_equal (dtype , IntervalDtype ())
545
+
546
+ @pytest .mark .parametrize ('subtype' , [
547
+ 'int64' , 'uint64' , 'float64' , 'complex128' , 'datetime64' ,
548
+ 'timedelta64' , PeriodDtype ('Q' )])
549
+ def test_name_repr (self , subtype ):
550
+ # GH 18980
551
+ dtype = IntervalDtype (subtype )
552
+ expected = 'interval[{subtype}]' .format (subtype = subtype )
553
+ assert str (dtype ) == expected
554
+ assert dtype .name == 'interval'
555
+
556
+ @pytest .mark .parametrize ('subtype' , [None , 'interval' , 'Interval' ])
557
+ def test_name_repr_generic (self , subtype ):
558
+ # GH 18980
559
+ dtype = IntervalDtype (subtype )
560
+ assert str (dtype ) == 'interval'
561
+ assert dtype .name == 'interval'
562
+
539
563
def test_basic (self ):
540
564
assert is_interval_dtype (self .dtype )
541
565
0 commit comments