Skip to content

Commit e4b9500

Browse files
committed
Replace assert statements with argument validation
1 parent 3f67dc3 commit e4b9500

File tree

12 files changed

+35
-18
lines changed

12 files changed

+35
-18
lines changed

pandas/core/apply.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ def normalize_dictlike_arg(
513513
that a nested renamer is not passed. Also normalizes to all lists
514514
when values consists of a mix of list and non-lists.
515515
"""
516-
assert how in ("apply", "agg", "transform")
516+
if how not in ("apply", "agg", "transform"):
517+
raise ValueError('Value for how argument must be one of : apply, agg, transform')
517518

518519
# Can't use func.values(); wouldn't work for a Series
519520
if (

pandas/core/arrays/_ranges.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def _generate_range_overflow_safe(
102102
OutOfBoundsDatetime
103103
"""
104104
# GH#14187 raise instead of incorrectly wrapping around
105-
assert side in ["start", "end"]
105+
if side not in ["start", "end"]:
106+
raise ValueError('Value for side argument must be one of: start, end')
106107

107108
i64max = np.uint64(np.iinfo(np.int64).max)
108109
msg = f"Cannot generate range with {side}={endpoint} and periods={periods}"
@@ -148,8 +149,9 @@ def _generate_range_overflow_safe_signed(
148149
A special case for _generate_range_overflow_safe where `periods * stride`
149150
can be calculated without overflowing int64 bounds.
150151
"""
151-
assert side in ["start", "end"]
152-
if side == "end":
152+
if side not in ['start', 'end']:
153+
raise ValueError('Value for side argument must be one of: start, end')
154+
if side == 'end':
153155
stride *= -1
154156

155157
with np.errstate(over="raise"):

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,8 @@ def objects_to_datetime64ns(
21552155
------
21562156
ValueError : if data cannot be converted to datetimes
21572157
"""
2158-
assert errors in ["raise", "ignore", "coerce"]
2158+
if errors not in ["raise", "ignore", "coerce"]:
2159+
raise ValueError('Value for errors argument must be one of: raise, coerce, ignore')
21592160

21602161
# if str-dtype, convert
21612162
data = np.array(data, copy=False, dtype=np.object_)

pandas/core/groupby/ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,8 @@ def _cython_operation(
967967
"""
968968
Returns the values of a cython operation.
969969
"""
970-
assert kind in ["transform", "aggregate"]
970+
if kind not in ["transform", "aggregate"]:
971+
raise ValueError('Value for kind argument must be one of: transform, aggregate')
971972

972973
cy_op = WrappedCythonOp(kind=kind, how=how)
973974

pandas/core/indexes/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,8 @@ def _convert_slice_indexer(self, key: slice, kind: str_t):
36403640
key : label of the slice bound
36413641
kind : {'loc', 'getitem'}
36423642
"""
3643-
assert kind in ["loc", "getitem"], kind
3643+
if kind not in ["loc", "getitem"]:
3644+
raise ValueError('Value for kind argument must be one of: loc, getitem')
36443645

36453646
# potentially cast the bounds to integers
36463647
start, stop, step = key.start, key.stop, key.step
@@ -5687,7 +5688,8 @@ def _validate_indexer(self, form: str_t, key, kind: str_t):
56875688
If we are positional indexer, validate that we have appropriate
56885689
typed bounds must be an integer.
56895690
"""
5690-
assert kind in ["getitem", "iloc"]
5691+
if kind not in ["getitem", "iloc"]:
5692+
raise ValueError('Value for kind argument must be one of: getitem, iloc')
56915693

56925694
if key is not None and not is_integer(key):
56935695
raise self._invalid_indexer(form, key)
@@ -5712,7 +5714,8 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
57125714
-----
57135715
Value of `side` parameter should be validated in caller.
57145716
"""
5715-
assert kind in ["loc", "getitem", None, no_default]
5717+
if kind not in ["loc", "getitem", None, no_default]:
5718+
raise ValueError('Value for kind argument must be one of: loc, getitem or None')
57165719
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
57175720

57185721
# We are a plain index here (sub-class override this method if they
@@ -5756,7 +5759,8 @@ def get_slice_bound(self, label, side: str_t, kind=None) -> int:
57565759
int
57575760
Index of label.
57585761
"""
5759-
assert kind in ["loc", "getitem", None]
5762+
if kind not in ["loc", "getitem", None]:
5763+
raise ValueError('Value for kind argument must be one of: loc, getitem or None')
57605764

57615765
if side not in ("left", "right"):
57625766
raise ValueError(

pandas/core/indexes/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
730730
-----
731731
Value of `side` parameter should be validated in caller.
732732
"""
733-
assert kind in ["loc", "getitem", None, lib.no_default]
733+
if kind not in ["loc", "getitem", None, lib.no_default]:
734+
raise ValueError('Value for kind argument must be one of: loc, getitem or None')
734735
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
735736

736737
if isinstance(label, str):

pandas/core/indexes/numeric.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ def _should_fallback_to_positional(self) -> bool:
235235
@doc(Index._convert_slice_indexer)
236236
def _convert_slice_indexer(self, key: slice, kind: str):
237237
if is_float_dtype(self.dtype):
238-
assert kind in ["loc", "getitem"]
238+
if kind not in ["loc", "getitem"]:
239+
raise ValueError('Value for kind argument must be one of: loc, getitem')
239240

240241
# We always treat __getitem__ slicing as label-based
241242
# translate to locations
@@ -245,7 +246,8 @@ def _convert_slice_indexer(self, key: slice, kind: str):
245246

246247
@doc(Index._maybe_cast_slice_bound)
247248
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
248-
assert kind in ["loc", "getitem", None, lib.no_default]
249+
if kind not in ["loc", "getitem", None, lib.no_default]:
250+
raise ValueError('Value for kind argument must be one of: loc, getitem or None')
249251
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
250252

251253
# we will try to coerce to integers

pandas/core/indexes/period.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
485485
Value of `side` parameter should be validated in caller.
486486
487487
"""
488-
assert kind in ["loc", "getitem", None, lib.no_default]
488+
if kind not in ["loc", "getitem", None, lib.no_default]:
489+
raise ValueError('Value for kind argument must be one of: loc, getitem or None')
489490
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
490491

491492
if isinstance(label, datetime):

pandas/core/indexes/timedeltas.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
199199
-------
200200
label : object
201201
"""
202-
assert kind in ["loc", "getitem", None, lib.no_default]
202+
if kind not in ["loc", "getitem", None, lib.no_default]:
203+
raise ValueError('Value for kind argument must be one of: loc, getitem or None')
203204
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
204205

205206
if isinstance(label, str):

pandas/core/internals/blocks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,8 @@ def where(self, other, cond, errors="raise") -> list[Block]:
12041204
assert cond.ndim == self.ndim
12051205
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
12061206

1207-
assert errors in ["raise", "ignore"]
1207+
if errors not in ["raise", "ignore"]:
1208+
raise ValueError('Value for errors argument must be one of: raise, ignore')
12081209
transpose = self.ndim == 2
12091210

12101211
values = self.values

pandas/core/missing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def find_valid_index(values, *, how: str) -> int | None:
178178
-------
179179
int or None
180180
"""
181-
assert how in ["first", "last"]
181+
if how not in ["first", "last"]:
182+
raise ValueError(f'Value for how argument must be one of : first, last')
182183

183184
if len(values) == 0: # early stop
184185
return None

pandas/io/excel/_util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def get_default_engine(ext, mode="reader"):
6060
"xls": "xlwt",
6161
"ods": "odf",
6262
}
63-
assert mode in ["reader", "writer"]
63+
if mode not in ["reader", "writer"]:
64+
raise ValueError('File mode must be either "reader" or "writer".')
6465
if mode == "writer":
6566
# Prefer xlsxwriter over openpyxl if installed
6667
xlsxwriter = import_optional_dependency("xlsxwriter", errors="warn")

0 commit comments

Comments
 (0)