Skip to content

Commit 79a0324

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
extract ._validate from ._simple_new
1 parent e7b7054 commit 79a0324

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

pandas/core/arrays/interval.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,20 @@ def __new__(
257257
closed = closed or infer_closed
258258

259259
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,
261265
)
262266

267+
if verify_integrity:
268+
cls._validate(left, right, dtype=dtype)
269+
263270
return cls._simple_new(
264271
left,
265272
right,
266273
dtype=dtype,
267-
verify_integrity=verify_integrity,
268274
)
269275

270276
@classmethod
@@ -273,16 +279,12 @@ def _simple_new(
273279
left,
274280
right,
275281
dtype: IntervalDtype,
276-
verify_integrity: bool = True,
277282
) -> IntervalArrayT:
278283
result = IntervalMixin.__new__(cls)
279284
result._left = left
280285
result._right = right
281286
result._dtype = dtype
282287

283-
if verify_integrity:
284-
result._validate()
285-
286288
return result
287289

288290
@classmethod
@@ -527,10 +529,15 @@ def from_arrays(
527529
right = _maybe_convert_platform_interval(right)
528530

529531
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,
531537
)
538+
cls._validate(left, right, dtype=dtype)
532539

533-
return cls._simple_new(left, right, dtype=dtype, verify_integrity=True)
540+
return cls._simple_new(left, right, dtype=dtype)
534541

535542
_interval_shared_docs["from_tuples"] = textwrap.dedent(
536543
"""
@@ -615,32 +622,33 @@ def from_tuples(
615622

616623
return cls.from_arrays(left, right, closed, copy=False, dtype=dtype)
617624

618-
def _validate(self):
625+
@classmethod
626+
def _validate(cls, left, right, dtype: IntervalDtype) -> None:
619627
"""
620628
Verify that the IntervalArray is valid.
621629
622630
Checks that
623631
624-
* closed is valid
632+
* dtype is correct
625633
* left and right match lengths
626634
* left and right have the same missing values
627635
* left is always below right
628636
"""
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}"
631639
raise ValueError(msg)
632-
if len(self._left) != len(self._right):
640+
if len(left) != len(right):
633641
msg = "left and right must have the same length"
634642
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)
637645
if not (left_mask == right_mask).all():
638646
msg = (
639647
"missing values must be missing in the same "
640648
"location both left and right sides"
641649
)
642650
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():
644652
msg = "left side of interval must be <= right side"
645653
raise ValueError(msg)
646654

@@ -657,7 +665,9 @@ def _shallow_copy(self: IntervalArrayT, left, right) -> IntervalArrayT:
657665
"""
658666
dtype = IntervalDtype(left.dtype, closed=self.closed)
659667
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)
661671

662672
# ---------------------------------------------------------------------
663673
# Descriptive
@@ -1019,9 +1029,8 @@ def copy(self: IntervalArrayT) -> IntervalArrayT:
10191029
"""
10201030
left = self._left.copy()
10211031
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)
10251034

10261035
def isna(self) -> np.ndarray:
10271036
return isna(self._left)
@@ -1423,7 +1432,7 @@ def set_closed(self: IntervalArrayT, closed: IntervalClosedType) -> IntervalArra
14231432

14241433
left, right = self._left, self._right
14251434
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)
14271436

14281437
_interval_shared_docs[
14291438
"is_non_overlapping_monotonic"

0 commit comments

Comments
 (0)