@@ -1161,8 +1161,8 @@ def f(typ: int) -> Union[int, str]:
1161
1161
return typ
1162
1162
try :
1163
1163
return self .TYPE_MAP_XML [typ ]
1164
- except KeyError :
1165
- raise ValueError (f"cannot convert stata types [{ typ } ]" )
1164
+ except KeyError as err :
1165
+ raise ValueError (f"cannot convert stata types [{ typ } ]" ) from err
1166
1166
1167
1167
typlist = [f (x ) for x in raw_typlist ]
1168
1168
@@ -1171,8 +1171,8 @@ def g(typ: int) -> Union[str, np.dtype]:
1171
1171
return str (typ )
1172
1172
try :
1173
1173
return self .DTYPE_MAP_XML [typ ]
1174
- except KeyError :
1175
- raise ValueError (f"cannot convert stata dtype [{ typ } ]" )
1174
+ except KeyError as err :
1175
+ raise ValueError (f"cannot convert stata dtype [{ typ } ]" ) from err
1176
1176
1177
1177
dtyplist = [g (x ) for x in raw_typlist ]
1178
1178
@@ -1296,14 +1296,14 @@ def _read_old_header(self, first_char: bytes) -> None:
1296
1296
1297
1297
try :
1298
1298
self .typlist = [self .TYPE_MAP [typ ] for typ in typlist ]
1299
- except ValueError :
1299
+ except ValueError as err :
1300
1300
invalid_types = "," .join (str (x ) for x in typlist )
1301
- raise ValueError (f"cannot convert stata types [{ invalid_types } ]" )
1301
+ raise ValueError (f"cannot convert stata types [{ invalid_types } ]" ) from err
1302
1302
try :
1303
1303
self .dtyplist = [self .DTYPE_MAP [typ ] for typ in typlist ]
1304
- except ValueError :
1304
+ except ValueError as err :
1305
1305
invalid_dtypes = "," .join (str (x ) for x in typlist )
1306
- raise ValueError (f"cannot convert stata dtypes [{ invalid_dtypes } ]" )
1306
+ raise ValueError (f"cannot convert stata dtypes [{ invalid_dtypes } ]" ) from err
1307
1307
1308
1308
if self .format_version > 108 :
1309
1309
self .varlist = [
@@ -1761,7 +1761,7 @@ def _do_convert_categoricals(
1761
1761
categories .append (category ) # Partially labeled
1762
1762
try :
1763
1763
cat_data .categories = categories
1764
- except ValueError :
1764
+ except ValueError as err :
1765
1765
vc = Series (categories ).value_counts ()
1766
1766
repeated_cats = list (vc .index [vc > 1 ])
1767
1767
repeats = "-" * 80 + "\n " + "\n " .join (repeated_cats )
@@ -1777,7 +1777,7 @@ def _do_convert_categoricals(
1777
1777
The repeated labels are:
1778
1778
{ repeats }
1779
1779
"""
1780
- raise ValueError (msg )
1780
+ raise ValueError (msg ) from err
1781
1781
# TODO: is the next line needed above in the data(...) method?
1782
1782
cat_series = Series (cat_data , index = data .index )
1783
1783
cat_converted_data .append ((col , cat_series ))
@@ -3143,11 +3143,11 @@ def _write_variable_labels(self) -> None:
3143
3143
raise ValueError ("Variable labels must be 80 characters or fewer" )
3144
3144
try :
3145
3145
encoded = label .encode (self ._encoding )
3146
- except UnicodeEncodeError :
3146
+ except UnicodeEncodeError as err :
3147
3147
raise ValueError (
3148
3148
"Variable labels must contain only characters that "
3149
3149
f"can be encoded in { self ._encoding } "
3150
- )
3150
+ ) from err
3151
3151
3152
3152
bio .write (_pad_bytes_new (encoded , vl_len + 1 ))
3153
3153
else :
0 commit comments