@@ -448,12 +448,12 @@ def from_tuples(cls, data, closed="right", copy=False, dtype=None):
448
448
try :
449
449
# need list of length 2 tuples, e.g. [(0, 1), (1, 2), ...]
450
450
lhs , rhs = d
451
- except ValueError :
451
+ except ValueError as err :
452
452
msg = f"{ name } .from_tuples requires tuples of length 2, got { d } "
453
- raise ValueError (msg )
454
- except TypeError :
453
+ raise ValueError (msg ) from err
454
+ except TypeError as err :
455
455
msg = f"{ name } .from_tuples received an invalid item, { d } "
456
- raise TypeError (msg )
456
+ raise TypeError (msg ) from err
457
457
left .append (lhs )
458
458
right .append (rhs )
459
459
@@ -538,10 +538,10 @@ def __setitem__(self, key, value):
538
538
try :
539
539
array = IntervalArray (value )
540
540
value_left , value_right = array .left , array .right
541
- except TypeError :
541
+ except TypeError as err :
542
542
# wrong type: not interval or NA
543
543
msg = f"'value' should be an interval type, got { type (value )} instead."
544
- raise TypeError (msg )
544
+ raise TypeError (msg ) from err
545
545
546
546
key = check_array_indexer (self , key )
547
547
# Need to ensure that left and right are updated atomically, so we're
@@ -688,20 +688,20 @@ def astype(self, dtype, copy=True):
688
688
try :
689
689
new_left = self .left .astype (dtype .subtype )
690
690
new_right = self .right .astype (dtype .subtype )
691
- except TypeError :
691
+ except TypeError as err :
692
692
msg = (
693
693
f"Cannot convert { self .dtype } to { dtype } ; subtypes are incompatible"
694
694
)
695
- raise TypeError (msg )
695
+ raise TypeError (msg ) from err
696
696
return self ._shallow_copy (new_left , new_right )
697
697
elif is_categorical_dtype (dtype ):
698
698
return Categorical (np .asarray (self ))
699
699
# TODO: This try/except will be repeated.
700
700
try :
701
701
return np .asarray (self ).astype (dtype , copy = copy )
702
- except (TypeError , ValueError ):
702
+ except (TypeError , ValueError ) as err :
703
703
msg = f"Cannot cast { type (self ).__name__ } to dtype { dtype } "
704
- raise TypeError (msg )
704
+ raise TypeError (msg ) from err
705
705
706
706
@classmethod
707
707
def _concat_same_type (cls , to_concat ):
@@ -1020,13 +1020,13 @@ def length(self):
1020
1020
"""
1021
1021
try :
1022
1022
return self .right - self .left
1023
- except TypeError :
1023
+ except TypeError as err :
1024
1024
# length not defined for some types, e.g. string
1025
1025
msg = (
1026
1026
"IntervalArray contains Intervals without defined length, "
1027
1027
"e.g. Intervals with string endpoints"
1028
1028
)
1029
- raise TypeError (msg )
1029
+ raise TypeError (msg ) from err
1030
1030
1031
1031
@property
1032
1032
def mid (self ):
@@ -1100,11 +1100,11 @@ def __arrow_array__(self, type=None):
1100
1100
1101
1101
try :
1102
1102
subtype = pyarrow .from_numpy_dtype (self .dtype .subtype )
1103
- except TypeError :
1103
+ except TypeError as err :
1104
1104
raise TypeError (
1105
1105
f"Conversion to arrow with subtype '{ self .dtype .subtype } ' "
1106
1106
"is not supported"
1107
- )
1107
+ ) from err
1108
1108
interval_type = ArrowIntervalType (subtype , self .closed )
1109
1109
storage_array = pyarrow .StructArray .from_arrays (
1110
1110
[
0 commit comments