Skip to content

Commit dfaf8c6

Browse files
jschendeljreback
authored andcommitted
CLN: replace %s syntax with .format in core.tools, algorithms.py, base.py (#17305)
1 parent 870b6a6 commit dfaf8c6

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

Diff for: pandas/core/algorithms.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@ def isin(comps, values):
393393

394394
if not is_list_like(comps):
395395
raise TypeError("only list-like objects are allowed to be passed"
396-
" to isin(), you passed a "
397-
"[{0}]".format(type(comps).__name__))
396+
" to isin(), you passed a [{comps_type}]"
397+
.format(comps_type=type(comps).__name__))
398398
if not is_list_like(values):
399399
raise TypeError("only list-like objects are allowed to be passed"
400-
" to isin(), you passed a "
401-
"[{0}]".format(type(values).__name__))
400+
" to isin(), you passed a [{values_type}]"
401+
.format(values_type=type(values).__name__))
402402

403403
if not isinstance(values, (ABCIndex, ABCSeries, np.ndarray)):
404404
values = lib.list_to_object_array(list(values))
@@ -671,7 +671,7 @@ def mode(values):
671671
try:
672672
result = np.sort(result)
673673
except TypeError as e:
674-
warn("Unable to sort modes: %s" % e)
674+
warn("Unable to sort modes: {error}".format(error=e))
675675

676676
result = _reconstruct_data(result, original.dtype, original)
677677
return Series(result)

Diff for: pandas/core/base.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -342,24 +342,25 @@ def _obj_with_exclusions(self):
342342

343343
def __getitem__(self, key):
344344
if self._selection is not None:
345-
raise Exception('Column(s) %s already selected' % self._selection)
345+
raise Exception('Column(s) {selection} already selected'
346+
.format(selection=self._selection))
346347

347348
if isinstance(key, (list, tuple, ABCSeries, ABCIndexClass,
348349
np.ndarray)):
349350
if len(self.obj.columns.intersection(key)) != len(key):
350351
bad_keys = list(set(key).difference(self.obj.columns))
351-
raise KeyError("Columns not found: %s"
352-
% str(bad_keys)[1:-1])
352+
raise KeyError("Columns not found: {missing}"
353+
.format(missing=str(bad_keys)[1:-1]))
353354
return self._gotitem(list(key), ndim=2)
354355

355356
elif not getattr(self, 'as_index', False):
356357
if key not in self.obj.columns:
357-
raise KeyError("Column not found: %s" % key)
358+
raise KeyError("Column not found: {key}".format(key=key))
358359
return self._gotitem(key, ndim=2)
359360

360361
else:
361362
if key not in self.obj:
362-
raise KeyError("Column not found: %s" % key)
363+
raise KeyError("Column not found: {key}".format(key=key))
363364
return self._gotitem(key, ndim=1)
364365

365366
def _gotitem(self, key, ndim, subset=None):
@@ -409,7 +410,7 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs):
409410
if f is not None:
410411
return f(self, *args, **kwargs)
411412

412-
raise ValueError("{} is an unknown string function".format(arg))
413+
raise ValueError("{arg} is an unknown string function".format(arg=arg))
413414

414415
def _aggregate(self, arg, *args, **kwargs):
415416
"""
@@ -484,9 +485,9 @@ def nested_renaming_depr(level=4):
484485
is_nested_renamer = True
485486

486487
if k not in obj.columns:
487-
raise SpecificationError('cannot perform renaming '
488-
'for {0} with a nested '
489-
'dictionary'.format(k))
488+
msg = ('cannot perform renaming for {key} with a '
489+
'nested dictionary').format(key=k)
490+
raise SpecificationError(msg)
490491
nested_renaming_depr(4 + (_level or 0))
491492

492493
elif isinstance(obj, ABCSeries):

Diff for: pandas/core/tools/datetimes.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def _infer(a, b):
4646
if b and b.tzinfo:
4747
if not (tslib.get_timezone(tz) == tslib.get_timezone(b.tzinfo)):
4848
raise AssertionError('Inputs must both have the same timezone,'
49-
' {0} != {1}'.format(tz, b.tzinfo))
49+
' {timezone1} != {timezone2}'
50+
.format(timezone1=tz, timezone2=b.tzinfo))
5051
return tz
5152

5253
tz = None
@@ -491,10 +492,10 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
491492
offset = tslib.Timestamp(origin) - tslib.Timestamp(0)
492493
except tslib.OutOfBoundsDatetime:
493494
raise tslib.OutOfBoundsDatetime(
494-
"origin {} is Out of Bounds".format(origin))
495+
"origin {origin} is Out of Bounds".format(origin=origin))
495496
except ValueError:
496-
raise ValueError("origin {} cannot be converted "
497-
"to a Timestamp".format(origin))
497+
raise ValueError("origin {origin} cannot be converted "
498+
"to a Timestamp".format(origin=origin))
498499

499500
# convert the offset to the unit of the arg
500501
# this should be lossless in terms of precision
@@ -590,16 +591,16 @@ def f(value):
590591
required = ['year', 'month', 'day']
591592
req = sorted(list(set(required) - set(unit_rev.keys())))
592593
if len(req):
593-
raise ValueError("to assemble mappings requires at "
594-
"least that [year, month, day] be specified: "
595-
"[{0}] is missing".format(','.join(req)))
594+
raise ValueError("to assemble mappings requires at least that "
595+
"[year, month, day] be specified: [{required}] "
596+
"is missing".format(required=','.join(req)))
596597

597598
# keys we don't recognize
598599
excess = sorted(list(set(unit_rev.keys()) - set(_unit_map.values())))
599600
if len(excess):
600601
raise ValueError("extra keys have been passed "
601602
"to the datetime assemblage: "
602-
"[{0}]".format(','.join(excess)))
603+
"[{excess}]".format(','.join(excess=excess)))
603604

604605
def coerce(values):
605606
# we allow coercion to if errors allows
@@ -617,7 +618,7 @@ def coerce(values):
617618
values = to_datetime(values, format='%Y%m%d', errors=errors)
618619
except (TypeError, ValueError) as e:
619620
raise ValueError("cannot assemble the "
620-
"datetimes: {0}".format(e))
621+
"datetimes: {error}".format(error=e))
621622

622623
for u in ['h', 'm', 's', 'ms', 'us', 'ns']:
623624
value = unit_rev.get(u)
@@ -627,8 +628,8 @@ def coerce(values):
627628
unit=u,
628629
errors=errors)
629630
except (TypeError, ValueError) as e:
630-
raise ValueError("cannot assemble the datetimes "
631-
"[{0}]: {1}".format(value, e))
631+
raise ValueError("cannot assemble the datetimes [{value}]: "
632+
"{error}".format(value=value, error=e))
632633

633634
return values
634635

@@ -810,8 +811,10 @@ def _convert_listlike(arg, format):
810811
times.append(datetime.strptime(element, format).time())
811812
except (ValueError, TypeError):
812813
if errors == 'raise':
813-
raise ValueError("Cannot convert %s to a time with "
814-
"given format %s" % (element, format))
814+
msg = ("Cannot convert {element} to a time with given "
815+
"format {format}").format(element=element,
816+
format=format)
817+
raise ValueError(msg)
815818
elif errors == 'ignore':
816819
return arg
817820
else:
@@ -876,6 +879,7 @@ def ole2datetime(oledt):
876879
# Excel has a bug where it thinks the date 2/29/1900 exists
877880
# we just reject any date before 3/1/1900.
878881
if val < 61:
879-
raise ValueError("Value is outside of acceptable range: %s " % val)
882+
msg = "Value is outside of acceptable range: {value}".format(value=val)
883+
raise ValueError(msg)
880884

881885
return OLE_TIME_ZERO + timedelta(days=val)

Diff for: pandas/core/tools/timedeltas.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ def _validate_timedelta_unit(arg):
129129
except:
130130
if arg is None:
131131
return 'ns'
132-
raise ValueError("invalid timedelta unit {0} provided".format(arg))
132+
raise ValueError("invalid timedelta unit {arg} provided"
133+
.format(arg=arg))
133134

134135

135136
def _coerce_scalar_to_timedelta_type(r, unit='ns', box=True, errors='raise'):
@@ -161,8 +162,8 @@ def _convert_listlike(arg, unit='ns', box=True, errors='raise', name=None):
161162
if is_timedelta64_dtype(arg):
162163
value = arg.astype('timedelta64[ns]')
163164
elif is_integer_dtype(arg):
164-
value = arg.astype('timedelta64[{0}]'.format(
165-
unit)).astype('timedelta64[ns]', copy=False)
165+
value = arg.astype('timedelta64[{unit}]'.format(unit=unit)).astype(
166+
'timedelta64[ns]', copy=False)
166167
else:
167168
try:
168169
value = tslib.array_to_timedelta64(_ensure_object(arg),

0 commit comments

Comments
 (0)