Skip to content

Commit b822535

Browse files
jschendeljreback
authored andcommitted
CLN: replace %s syntax with .format in core.computation (#17209)
1 parent 073c145 commit b822535

File tree

7 files changed

+87
-73
lines changed

7 files changed

+87
-73
lines changed

pandas/core/computation/align.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ def _align_core(terms):
9898

9999
ordm = np.log10(max(1, abs(reindexer_size - term_axis_size)))
100100
if ordm >= 1 and reindexer_size >= 10000:
101-
warnings.warn('Alignment difference on axis {0} is larger '
102-
'than an order of magnitude on term {1!r}, '
103-
'by more than {2:.4g}; performance may '
104-
'suffer'.format(axis, terms[i].name, ordm),
105-
category=PerformanceWarning,
106-
stacklevel=6)
101+
w = ('Alignment difference on axis {axis} is larger '
102+
'than an order of magnitude on term {term!r}, by '
103+
'more than {ordm:.4g}; performance may suffer'
104+
).format(axis=axis, term=terms[i].name, ordm=ordm)
105+
warnings.warn(w, category=PerformanceWarning, stacklevel=6)
107106

108107
if transpose:
109108
f = partial(ti.reindex, index=reindexer, copy=False)

pandas/core/computation/engines.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ def _check_ne_builtin_clash(expr):
3333

3434
if overlap:
3535
s = ', '.join(map(repr, overlap))
36-
raise NumExprClobberingError('Variables in expression "%s" '
37-
'overlap with builtins: (%s)' % (expr, s))
36+
raise NumExprClobberingError('Variables in expression "{expr}" '
37+
'overlap with builtins: ({s})'
38+
.format(expr=expr, s=s))
3839

3940

4041
class AbstractEngine(object):

pandas/core/computation/eval.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ def _check_engine(engine):
4040
engine = 'python'
4141

4242
if engine not in _engines:
43-
raise KeyError('Invalid engine {0!r} passed, valid engines are'
44-
' {1}'.format(engine, list(_engines.keys())))
43+
valid = list(_engines.keys())
44+
raise KeyError('Invalid engine {engine!r} passed, valid engines are'
45+
' {valid}'.format(engine=engine, valid=valid))
4546

4647
# TODO: validate this in a more general way (thinking of future engines
4748
# that won't necessarily be import-able)
@@ -69,17 +70,17 @@ def _check_parser(parser):
6970
* If an invalid parser is passed
7071
"""
7172
if parser not in _parsers:
72-
raise KeyError('Invalid parser {0!r} passed, valid parsers are'
73-
' {1}'.format(parser, _parsers.keys()))
73+
raise KeyError('Invalid parser {parser!r} passed, valid parsers are'
74+
' {valid}'.format(parser=parser, valid=_parsers.keys()))
7475

7576

7677
def _check_resolvers(resolvers):
7778
if resolvers is not None:
7879
for resolver in resolvers:
7980
if not hasattr(resolver, '__getitem__'):
8081
name = type(resolver).__name__
81-
raise TypeError('Resolver of type %r does not implement '
82-
'the __getitem__ method' % name)
82+
raise TypeError('Resolver of type {name!r} does not implement '
83+
'the __getitem__ method'.format(name=name))
8384

8485

8586
def _check_expression(expr):

pandas/core/computation/expr.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ def _filter_nodes(superclass, all_nodes=_all_nodes):
189189
# and we don't want `stmt` and friends in their so get only the class whose
190190
# names are capitalized
191191
_base_supported_nodes = (_all_node_names - _unsupported_nodes) | _hacked_nodes
192-
_msg = 'cannot both support and not support {0}'.format(_unsupported_nodes &
193-
_base_supported_nodes)
192+
_msg = 'cannot both support and not support {intersection}'.format(
193+
intersection=_unsupported_nodes & _base_supported_nodes)
194194
assert not _unsupported_nodes & _base_supported_nodes, _msg
195195

196196

@@ -200,8 +200,8 @@ def _node_not_implemented(node_name, cls):
200200
"""
201201

202202
def f(self, *args, **kwargs):
203-
raise NotImplementedError("{0!r} nodes are not "
204-
"implemented".format(node_name))
203+
raise NotImplementedError("{name!r} nodes are not "
204+
"implemented".format(name=node_name))
205205
return f
206206

207207

@@ -217,7 +217,7 @@ def disallowed(cls):
217217
cls.unsupported_nodes = ()
218218
for node in nodes:
219219
new_method = _node_not_implemented(node, cls)
220-
name = 'visit_{0}'.format(node)
220+
name = 'visit_{node}'.format(node=node)
221221
cls.unsupported_nodes += (name,)
222222
setattr(cls, name, new_method)
223223
return cls
@@ -251,13 +251,14 @@ def add_ops(op_classes):
251251
"""Decorator to add default implementation of ops."""
252252
def f(cls):
253253
for op_attr_name, op_class in compat.iteritems(op_classes):
254-
ops = getattr(cls, '{0}_ops'.format(op_attr_name))
255-
ops_map = getattr(cls, '{0}_op_nodes_map'.format(op_attr_name))
254+
ops = getattr(cls, '{name}_ops'.format(name=op_attr_name))
255+
ops_map = getattr(cls, '{name}_op_nodes_map'.format(
256+
name=op_attr_name))
256257
for op in ops:
257258
op_node = ops_map[op]
258259
if op_node is not None:
259260
made_op = _op_maker(op_class, op)
260-
setattr(cls, 'visit_{0}'.format(op_node), made_op)
261+
setattr(cls, 'visit_{node}'.format(node=op_node), made_op)
261262
return cls
262263
return f
263264

@@ -388,9 +389,10 @@ def _maybe_evaluate_binop(self, op, op_class, lhs, rhs,
388389
res = op(lhs, rhs)
389390

390391
if res.has_invalid_return_type:
391-
raise TypeError("unsupported operand type(s) for {0}:"
392-
" '{1}' and '{2}'".format(res.op, lhs.type,
393-
rhs.type))
392+
raise TypeError("unsupported operand type(s) for {op}:"
393+
" '{lhs}' and '{rhs}'".format(op=res.op,
394+
lhs=lhs.type,
395+
rhs=rhs.type))
394396

395397
if self.engine != 'pytables':
396398
if (res.op in _cmp_ops_syms and
@@ -527,7 +529,8 @@ def visit_Attribute(self, node, **kwargs):
527529
if isinstance(value, ast.Name) and value.id == attr:
528530
return resolved
529531

530-
raise ValueError("Invalid Attribute context {0}".format(ctx.__name__))
532+
raise ValueError("Invalid Attribute context {name}"
533+
.format(name=ctx.__name__))
531534

532535
def visit_Call_35(self, node, side=None, **kwargs):
533536
""" in 3.5 the starargs attribute was changed to be more flexible,
@@ -549,7 +552,8 @@ def visit_Call_35(self, node, side=None, **kwargs):
549552
raise
550553

551554
if res is None:
552-
raise ValueError("Invalid function call {0}".format(node.func.id))
555+
raise ValueError("Invalid function call {func}"
556+
.format(func=node.func.id))
553557
if hasattr(res, 'value'):
554558
res = res.value
555559

@@ -558,8 +562,8 @@ def visit_Call_35(self, node, side=None, **kwargs):
558562
new_args = [self.visit(arg) for arg in node.args]
559563

560564
if node.keywords:
561-
raise TypeError("Function \"{0}\" does not support keyword "
562-
"arguments".format(res.name))
565+
raise TypeError("Function \"{name}\" does not support keyword "
566+
"arguments".format(name=res.name))
563567

564568
return res(*new_args, **kwargs)
565569

@@ -570,7 +574,7 @@ def visit_Call_35(self, node, side=None, **kwargs):
570574
for key in node.keywords:
571575
if not isinstance(key, ast.keyword):
572576
raise ValueError("keyword error in function call "
573-
"'{0}'".format(node.func.id))
577+
"'{func}'".format(func=node.func.id))
574578

575579
if key.arg:
576580
# TODO: bug?
@@ -598,7 +602,8 @@ def visit_Call_legacy(self, node, side=None, **kwargs):
598602
raise
599603

600604
if res is None:
601-
raise ValueError("Invalid function call {0}".format(node.func.id))
605+
raise ValueError("Invalid function call {func}"
606+
.format(func=node.func.id))
602607
if hasattr(res, 'value'):
603608
res = res.value
604609

@@ -609,8 +614,8 @@ def visit_Call_legacy(self, node, side=None, **kwargs):
609614
args += self.visit(node.starargs)
610615

611616
if node.keywords or node.kwargs:
612-
raise TypeError("Function \"{0}\" does not support keyword "
613-
"arguments".format(res.name))
617+
raise TypeError("Function \"{name}\" does not support keyword "
618+
"arguments".format(name=res.name))
614619

615620
return res(*args, **kwargs)
616621

@@ -623,7 +628,7 @@ def visit_Call_legacy(self, node, side=None, **kwargs):
623628
for key in node.keywords:
624629
if not isinstance(key, ast.keyword):
625630
raise ValueError("keyword error in function call "
626-
"'{0}'".format(node.func.id))
631+
"'{func}'".format(func=node.func.id))
627632
keywords[key.arg] = self.visit(key.value).value
628633
if node.kwargs is not None:
629634
keywords.update(self.visit(node.kwargs).value)

pandas/core/computation/expressions.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, truediv=True,
103103

104104
a_value = getattr(a, "values", a)
105105
b_value = getattr(b, "values", b)
106-
result = ne.evaluate('a_value %s b_value' % op_str,
106+
result = ne.evaluate('a_value {op} b_value'.format(op=op_str),
107107
local_dict={'a_value': a_value,
108108
'b_value': b_value},
109109
casting='safe', truediv=truediv,
@@ -177,15 +177,15 @@ def _bool_arith_check(op_str, a, b, not_allowed=frozenset(('/', '//', '**')),
177177

178178
if _has_bool_dtype(a) and _has_bool_dtype(b):
179179
if op_str in unsupported:
180-
warnings.warn("evaluating in Python space because the %r operator"
181-
" is not supported by numexpr for the bool "
182-
"dtype, use %r instead" % (op_str,
183-
unsupported[op_str]))
180+
warnings.warn("evaluating in Python space because the {op!r} "
181+
"operator is not supported by numexpr for "
182+
"the bool dtype, use {alt_op!r} instead"
183+
.format(op=op_str, alt_op=unsupported[op_str]))
184184
return False
185185

186186
if op_str in not_allowed:
187-
raise NotImplementedError("operator %r not implemented for bool "
188-
"dtypes" % op_str)
187+
raise NotImplementedError("operator {op!r} not implemented for "
188+
"bool dtypes".format(op=op_str))
189189
return True
190190

191191

0 commit comments

Comments
 (0)