@@ -448,12 +448,12 @@ def from_tuples(cls, data, closed="right", copy=False, dtype=None):
448448 try :
449449 # need list of length 2 tuples, e.g. [(0, 1), (1, 2), ...]
450450 lhs , rhs = d
451- except ValueError :
451+ except ValueError as err :
452452 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 :
455455 msg = f"{ name } .from_tuples received an invalid item, { d } "
456- raise TypeError (msg )
456+ raise TypeError (msg ) from err
457457 left .append (lhs )
458458 right .append (rhs )
459459
@@ -538,10 +538,10 @@ def __setitem__(self, key, value):
538538 try :
539539 array = IntervalArray (value )
540540 value_left , value_right = array .left , array .right
541- except TypeError :
541+ except TypeError as err :
542542 # wrong type: not interval or NA
543543 msg = f"'value' should be an interval type, got { type (value )} instead."
544- raise TypeError (msg )
544+ raise TypeError (msg ) from err
545545
546546 key = check_array_indexer (self , key )
547547 # Need to ensure that left and right are updated atomically, so we're
@@ -688,20 +688,20 @@ def astype(self, dtype, copy=True):
688688 try :
689689 new_left = self .left .astype (dtype .subtype )
690690 new_right = self .right .astype (dtype .subtype )
691- except TypeError :
691+ except TypeError as err :
692692 msg = (
693693 f"Cannot convert { self .dtype } to { dtype } ; subtypes are incompatible"
694694 )
695- raise TypeError (msg )
695+ raise TypeError (msg ) from err
696696 return self ._shallow_copy (new_left , new_right )
697697 elif is_categorical_dtype (dtype ):
698698 return Categorical (np .asarray (self ))
699699 # TODO: This try/except will be repeated.
700700 try :
701701 return np .asarray (self ).astype (dtype , copy = copy )
702- except (TypeError , ValueError ):
702+ except (TypeError , ValueError ) as err :
703703 msg = f"Cannot cast { type (self ).__name__ } to dtype { dtype } "
704- raise TypeError (msg )
704+ raise TypeError (msg ) from err
705705
706706 @classmethod
707707 def _concat_same_type (cls , to_concat ):
@@ -1020,13 +1020,13 @@ def length(self):
10201020 """
10211021 try :
10221022 return self .right - self .left
1023- except TypeError :
1023+ except TypeError as err :
10241024 # length not defined for some types, e.g. string
10251025 msg = (
10261026 "IntervalArray contains Intervals without defined length, "
10271027 "e.g. Intervals with string endpoints"
10281028 )
1029- raise TypeError (msg )
1029+ raise TypeError (msg ) from err
10301030
10311031 @property
10321032 def mid (self ):
@@ -1100,11 +1100,11 @@ def __arrow_array__(self, type=None):
11001100
11011101 try :
11021102 subtype = pyarrow .from_numpy_dtype (self .dtype .subtype )
1103- except TypeError :
1103+ except TypeError as err :
11041104 raise TypeError (
11051105 f"Conversion to arrow with subtype '{ self .dtype .subtype } ' "
11061106 "is not supported"
1107- )
1107+ ) from err
11081108 interval_type = ArrowIntervalType (subtype , self .closed )
11091109 storage_array = pyarrow .StructArray .from_arrays (
11101110 [
0 commit comments