Skip to content

Commit 4a6c361

Browse files
tqa236pmhatre1
authored andcommitted
STY: Enable B904 (pandas-dev#56941)
1 parent 69619d8 commit 4a6c361

File tree

25 files changed

+74
-68
lines changed

25 files changed

+74
-68
lines changed

asv_bench/benchmarks/algos/isin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def setup(self, dtype):
6161
self.series = Series(
6262
Index([f"i-{i}" for i in range(N)], dtype=object), dtype=dtype
6363
)
64-
except ImportError:
65-
raise NotImplementedError
64+
except ImportError as err:
65+
raise NotImplementedError from err
6666
self.values = list(self.series[:2])
6767

6868
else:

asv_bench/benchmarks/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class ArrowStringArray:
7676
def setup(self, multiple_chunks):
7777
try:
7878
import pyarrow as pa
79-
except ImportError:
80-
raise NotImplementedError
79+
except ImportError as err:
80+
raise NotImplementedError from err
8181
strings = np.array([str(i) for i in range(10_000)], dtype=object)
8282
if multiple_chunks:
8383
chunks = [strings[i : i + 100] for i in range(0, len(strings), 100)]

asv_bench/benchmarks/strings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def setup(self, dtype):
2121
self.s = Series(
2222
Index([f"i-{i}" for i in range(10000)], dtype=object), dtype=dtype
2323
)
24-
except ImportError:
25-
raise NotImplementedError
24+
except ImportError as err:
25+
raise NotImplementedError from err
2626

2727

2828
class Construction:

pandas/compat/_optional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def import_optional_dependency(
160160
)
161161
try:
162162
module = importlib.import_module(name)
163-
except ImportError:
163+
except ImportError as err:
164164
if errors == "raise":
165-
raise ImportError(msg)
165+
raise ImportError(msg) from err
166166
return None
167167

168168
# Handle submodules: if we have submodule, grab parent module from sys.modules

pandas/core/array_algos/datetimelike_accumulations.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ def _cum_func(
3737
np.cumsum: 0,
3838
np.minimum.accumulate: np.iinfo(np.int64).max,
3939
}[func]
40-
except KeyError:
41-
raise ValueError(f"No accumulation for {func} implemented on BaseMaskedArray")
40+
except KeyError as err:
41+
raise ValueError(
42+
f"No accumulation for {func} implemented on BaseMaskedArray"
43+
) from err
4244

4345
mask = isna(values)
4446
y = values.view("i8")

pandas/core/array_algos/masked_accumulations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ def _cum_func(
6060
np.cumsum: 0,
6161
np.minimum.accumulate: dtype_info.max,
6262
}[func]
63-
except KeyError:
63+
except KeyError as err:
6464
raise NotImplementedError(
6565
f"No accumulation for {func} implemented on BaseMaskedArray"
66-
)
66+
) from err
6767

6868
values[mask] = fill_value
6969

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
597597
except (
598598
TypeError, # downstream error msg for CategoricalIndex is misleading
599599
ValueError,
600-
):
600+
) as err:
601601
msg = f"Cannot cast {self.categories.dtype} dtype to {dtype}"
602-
raise ValueError(msg)
602+
raise ValueError(msg) from err
603603

604604
result = take_nd(
605605
new_cats, ensure_platform_int(self._codes), fill_value=fill_value

pandas/core/arrays/datetimelike.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ def _validate_comparison_value(self, other):
524524
try:
525525
# GH#18435 strings get a pass from tzawareness compat
526526
other = self._scalar_from_string(other)
527-
except (ValueError, IncompatibleFrequency):
527+
except (ValueError, IncompatibleFrequency) as err:
528528
# failed to parse as Timestamp/Timedelta/Period
529-
raise InvalidComparison(other)
529+
raise InvalidComparison(other) from err
530530

531531
if isinstance(other, self._recognized_scalars) or other is NaT:
532532
other = self._scalar_type(other)
@@ -664,11 +664,11 @@ def _validate_listlike(self, value, allow_object: bool = False):
664664
if lib.infer_dtype(value) in self._infer_matches:
665665
try:
666666
value = type(self)._from_sequence(value)
667-
except (ValueError, TypeError):
667+
except (ValueError, TypeError) as err:
668668
if allow_object:
669669
return value
670670
msg = self._validation_error_message(value, True)
671-
raise TypeError(msg)
671+
raise TypeError(msg) from err
672672

673673
# Do type inference if necessary up front (after unpacking
674674
# NumpyExtensionArray)

pandas/core/arrays/period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,10 +1188,10 @@ def dt64arr_to_periodarr(
11881188
freq = Period._maybe_convert_freq(freq)
11891189
try:
11901190
base = freq._period_dtype_code
1191-
except (AttributeError, TypeError):
1191+
except (AttributeError, TypeError) as err:
11921192
# AttributeError: _period_dtype_code might not exist
11931193
# TypeError: _period_dtype_code might intentionally raise
1194-
raise TypeError(f"{freq.name} is not supported as period frequency")
1194+
raise TypeError(f"{freq.name} is not supported as period frequency") from err
11951195
return c_dt64arr_to_periodarr(data.view("i8"), base, tz, reso=reso), freq
11961196

11971197

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,8 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
18171817
# TODO: general-case for EAs?
18181818
try:
18191819
casted = element.astype(dtype)
1820-
except (ValueError, TypeError):
1821-
raise LossySetitemError
1820+
except (ValueError, TypeError) as err:
1821+
raise LossySetitemError from err
18221822
# Check for cases of either
18231823
# a) lossy overflow/rounding or
18241824
# b) semantic changes like dt64->int64

0 commit comments

Comments
 (0)