@@ -257,14 +257,20 @@ def __new__(
257
257
closed = closed or infer_closed
258
258
259
259
left , right , dtype = cls ._ensure_simple_new_inputs (
260
- left , right , closed = closed , copy = copy , dtype = dtype
260
+ left ,
261
+ right ,
262
+ closed = closed ,
263
+ copy = copy ,
264
+ dtype = dtype ,
261
265
)
262
266
267
+ if verify_integrity :
268
+ cls ._validate (left , right , dtype = dtype )
269
+
263
270
return cls ._simple_new (
264
271
left ,
265
272
right ,
266
273
dtype = dtype ,
267
- verify_integrity = verify_integrity ,
268
274
)
269
275
270
276
@classmethod
@@ -273,16 +279,12 @@ def _simple_new(
273
279
left ,
274
280
right ,
275
281
dtype : IntervalDtype ,
276
- verify_integrity : bool = True ,
277
282
) -> IntervalArrayT :
278
283
result = IntervalMixin .__new__ (cls )
279
284
result ._left = left
280
285
result ._right = right
281
286
result ._dtype = dtype
282
287
283
- if verify_integrity :
284
- result ._validate ()
285
-
286
288
return result
287
289
288
290
@classmethod
@@ -527,10 +529,15 @@ def from_arrays(
527
529
right = _maybe_convert_platform_interval (right )
528
530
529
531
left , right , dtype = cls ._ensure_simple_new_inputs (
530
- left , right , closed = closed , copy = copy , dtype = dtype
532
+ left ,
533
+ right ,
534
+ closed = closed ,
535
+ copy = copy ,
536
+ dtype = dtype ,
531
537
)
538
+ cls ._validate (left , right , dtype = dtype )
532
539
533
- return cls ._simple_new (left , right , dtype = dtype , verify_integrity = True )
540
+ return cls ._simple_new (left , right , dtype = dtype )
534
541
535
542
_interval_shared_docs ["from_tuples" ] = textwrap .dedent (
536
543
"""
@@ -615,32 +622,33 @@ def from_tuples(
615
622
616
623
return cls .from_arrays (left , right , closed , copy = False , dtype = dtype )
617
624
618
- def _validate (self ):
625
+ @classmethod
626
+ def _validate (cls , left , right , dtype : IntervalDtype ) -> None :
619
627
"""
620
628
Verify that the IntervalArray is valid.
621
629
622
630
Checks that
623
631
624
- * closed is valid
632
+ * dtype is correct
625
633
* left and right match lengths
626
634
* left and right have the same missing values
627
635
* left is always below right
628
636
"""
629
- if self . closed not in VALID_CLOSED :
630
- msg = f"invalid option for 'closed' : { self . closed } "
637
+ if not isinstance ( dtype , IntervalDtype ) :
638
+ msg = f"invalid dtype : { dtype } "
631
639
raise ValueError (msg )
632
- if len (self . _left ) != len (self . _right ):
640
+ if len (left ) != len (right ):
633
641
msg = "left and right must have the same length"
634
642
raise ValueError (msg )
635
- left_mask = notna (self . _left )
636
- right_mask = notna (self . _right )
643
+ left_mask = notna (left )
644
+ right_mask = notna (right )
637
645
if not (left_mask == right_mask ).all ():
638
646
msg = (
639
647
"missing values must be missing in the same "
640
648
"location both left and right sides"
641
649
)
642
650
raise ValueError (msg )
643
- if not (self . _left [left_mask ] <= self . _right [left_mask ]).all ():
651
+ if not (left [left_mask ] <= right [left_mask ]).all ():
644
652
msg = "left side of interval must be <= right side"
645
653
raise ValueError (msg )
646
654
@@ -657,7 +665,9 @@ def _shallow_copy(self: IntervalArrayT, left, right) -> IntervalArrayT:
657
665
"""
658
666
dtype = IntervalDtype (left .dtype , closed = self .closed )
659
667
left , right , dtype = self ._ensure_simple_new_inputs (left , right , dtype = dtype )
660
- return self ._simple_new (left , right , dtype = dtype , verify_integrity = False )
668
+ self ._validate (left , right , dtype = dtype )
669
+
670
+ return self ._simple_new (left , right , dtype = dtype )
661
671
662
672
# ---------------------------------------------------------------------
663
673
# Descriptive
@@ -1019,9 +1029,8 @@ def copy(self: IntervalArrayT) -> IntervalArrayT:
1019
1029
"""
1020
1030
left = self ._left .copy ()
1021
1031
right = self ._right .copy ()
1022
- closed = self .closed
1023
- # TODO: Could skip verify_integrity here.
1024
- return type (self ).from_arrays (left , right , closed = closed )
1032
+ dtype = self .dtype
1033
+ return self ._simple_new (left , right , dtype = dtype )
1025
1034
1026
1035
def isna (self ) -> np .ndarray :
1027
1036
return isna (self ._left )
@@ -1423,7 +1432,7 @@ def set_closed(self: IntervalArrayT, closed: IntervalClosedType) -> IntervalArra
1423
1432
1424
1433
left , right = self ._left , self ._right
1425
1434
dtype = IntervalDtype (left .dtype , closed = closed )
1426
- return self ._simple_new (left , right , dtype = dtype , verify_integrity = False )
1435
+ return self ._simple_new (left , right , dtype = dtype )
1427
1436
1428
1437
_interval_shared_docs [
1429
1438
"is_non_overlapping_monotonic"
0 commit comments