Skip to content

Commit 0522dcc

Browse files
AaronCritchleyjreback
authored andcommitted
CLN: replace %s syntax with .format in core.indexing and core.internals (#18331)
1 parent fe4c34b commit 0522dcc

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1842,8 +1842,8 @@ def _convert_to_indexer(self, obj, axis=None, is_setter=False):
18421842
elif self._has_valid_type(obj, axis):
18431843
return obj
18441844

1845-
raise ValueError("Can only index by location with a [%s]" %
1846-
self._valid_types)
1845+
raise ValueError("Can only index by location with "
1846+
"a [{types}]".format(types=self._valid_types))
18471847

18481848

18491849
class _ScalarAccessIndexer(_NDFrameIndexer):

pandas/core/internals.py

+62-47
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ def __init__(self, values, placement, ndim=None, fastpath=False):
115115
self.values = values
116116

117117
if ndim and len(self.mgr_locs) != len(self.values):
118-
raise ValueError('Wrong number of items passed %d, placement '
119-
'implies %d' % (len(self.values),
120-
len(self.mgr_locs)))
118+
raise ValueError(
119+
'Wrong number of items passed {val}, placement implies '
120+
'{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))
121121

122122
@property
123123
def _consolidate_key(self):
@@ -236,13 +236,15 @@ def __unicode__(self):
236236
name = pprint_thing(self.__class__.__name__)
237237
if self._is_single_block:
238238

239-
result = '%s: %s dtype: %s' % (name, len(self), self.dtype)
239+
result = '{name}: {len} dtype: {dtype}'.format(
240+
name=name, len=len(self), dtype=self.dtype)
240241

241242
else:
242243

243244
shape = ' x '.join([pprint_thing(s) for s in self.shape])
244-
result = '%s: %s, %s, dtype: %s' % (name, pprint_thing(
245-
self.mgr_locs.indexer), shape, self.dtype)
245+
result = '{name}: {index}, {shape}, dtype: {dtype}'.format(
246+
name=name, index=pprint_thing(self.mgr_locs.indexer),
247+
shape=shape, dtype=self.dtype)
246248

247249
return result
248250

@@ -310,7 +312,7 @@ def dtype(self):
310312

311313
@property
312314
def ftype(self):
313-
return "%s:%s" % (self.dtype, self._ftype)
315+
return "{dtype}:{ftype}".format(dtype=self.dtype, ftype=self._ftype)
314316

315317
def merge(self, other):
316318
return _merge_blocks([self, other])
@@ -330,7 +332,8 @@ def reindex_axis(self, indexer, method=None, axis=1, fill_value=None,
330332
Reindex using pre-computed indexer information
331333
"""
332334
if axis < 1:
333-
raise AssertionError('axis must be at least 1, got %d' % axis)
335+
raise AssertionError(
336+
'axis must be at least 1, got {axis}'.format(axis=axis))
334337
if fill_value is None:
335338
fill_value = self.fill_value
336339

@@ -634,11 +637,13 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
634637

635638
if newb.is_numeric and self.is_numeric:
636639
if newb.shape != self.shape:
637-
raise TypeError("cannot set astype for copy = [%s] for dtype "
638-
"(%s [%s]) with smaller itemsize that current "
639-
"(%s [%s])" % (copy, self.dtype.name,
640-
self.itemsize, newb.dtype.name,
641-
newb.itemsize))
640+
raise TypeError(
641+
"cannot set astype for copy = [{copy}] for dtype "
642+
"({dtype} [{itemsize}]) with smaller itemsize than "
643+
"current ({newb_dtype} [{newb_size}])".format(
644+
copy=copy, dtype=self.dtype.name,
645+
itemsize=self.itemsize, newb_dtype=newb.dtype.name,
646+
newb_size=newb.itemsize))
642647
return newb
643648

644649
def convert(self, copy=True, **kwargs):
@@ -1306,9 +1311,10 @@ def eval(self, func, other, errors='raise', try_cast=False, mgr=None):
13061311
is_transposed = True
13071312
else:
13081313
# this is a broadcast error heree
1309-
raise ValueError("cannot broadcast shape [%s] with block "
1310-
"values [%s]" % (values.T.shape,
1311-
other.shape))
1314+
raise ValueError(
1315+
"cannot broadcast shape [{t_shape}] with "
1316+
"block values [{oth_shape}]".format(
1317+
t_shape=values.T.shape, oth_shape=other.shape))
13121318

13131319
transf = (lambda x: x.T) if is_transposed else (lambda x: x)
13141320

@@ -1363,8 +1369,9 @@ def handle_error():
13631369

13641370
if errors == 'raise':
13651371
# The 'detail' variable is defined in outer scope.
1366-
raise TypeError('Could not operate %s with block values %s' %
1367-
(repr(other), str(detail))) # noqa
1372+
raise TypeError(
1373+
'Could not operate {other!r} with block values '
1374+
'{detail!s}'.format(other=other, detail=detail)) # noqa
13681375
else:
13691376
# return the values
13701377
result = np.empty(values.shape, dtype='O')
@@ -1391,11 +1398,12 @@ def handle_error():
13911398
# differentiate between an invalid ndarray-ndarray comparison
13921399
# and an invalid type comparison
13931400
if isinstance(values, np.ndarray) and is_list_like(other):
1394-
raise ValueError('Invalid broadcasting comparison [%s] '
1395-
'with block values' % repr(other))
1401+
raise ValueError(
1402+
'Invalid broadcasting comparison [{other!r}] with '
1403+
'block values'.format(other=other))
13961404

1397-
raise TypeError('Could not compare [%s] with block values' %
1398-
repr(other))
1405+
raise TypeError('Could not compare [{other!r}] '
1406+
'with block values'.format(other=other))
13991407

14001408
# transpose if needed
14011409
result = transf(result)
@@ -1466,8 +1474,9 @@ def func(cond, values, other):
14661474
cond, values, other))
14671475
except Exception as detail:
14681476
if errors == 'raise':
1469-
raise TypeError('Could not operate [%s] with block values '
1470-
'[%s]' % (repr(other), str(detail)))
1477+
raise TypeError(
1478+
'Could not operate [{other!r}] with block values '
1479+
'[{detail!s}]'.format(other=other, detail=detail))
14711480
else:
14721481
# return the values
14731482
result = np.empty(values.shape, dtype='float64')
@@ -2894,7 +2903,8 @@ def reindex_axis(self, indexer, method=None, axis=1, fill_value=None,
28942903
Reindex using pre-computed indexer information
28952904
"""
28962905
if axis < 1:
2897-
raise AssertionError('axis must be at least 1, got %d' % axis)
2906+
raise AssertionError(
2907+
'axis must be at least 1, got {axis}'.format(axis=axis))
28982908

28992909
# taking on the 0th axis always here
29002910
if fill_value is None:
@@ -3020,9 +3030,10 @@ def __init__(self, blocks, axes, do_integrity_check=True, fastpath=True):
30203030
"items")
30213031
else:
30223032
if self.ndim != block.ndim:
3023-
raise AssertionError('Number of Block dimensions (%d) '
3024-
'must equal number of axes (%d)' %
3025-
(block.ndim, self.ndim))
3033+
raise AssertionError(
3034+
'Number of Block dimensions ({block}) must equal '
3035+
'number of axes ({self})'.format(block=block.ndim,
3036+
self=self.ndim))
30263037

30273038
if do_integrity_check:
30283039
self._verify_integrity()
@@ -3064,9 +3075,9 @@ def set_axis(self, axis, new_labels):
30643075
new_len = len(new_labels)
30653076

30663077
if new_len != old_len:
3067-
raise ValueError('Length mismatch: Expected axis has %d elements, '
3068-
'new values have %d elements' %
3069-
(old_len, new_len))
3078+
raise ValueError(
3079+
'Length mismatch: Expected axis has {old} elements, new '
3080+
'values have {new} elements'.format(old=old_len, new=new_len))
30703081

30713082
self.axes[axis] = new_labels
30723083

@@ -3223,12 +3234,12 @@ def __unicode__(self):
32233234
output = pprint_thing(self.__class__.__name__)
32243235
for i, ax in enumerate(self.axes):
32253236
if i == 0:
3226-
output += u('\nItems: %s') % ax
3237+
output += u('\nItems: {ax}'.format(ax=ax))
32273238
else:
3228-
output += u('\nAxis %d: %s') % (i, ax)
3239+
output += u('\nAxis {i}: {ax}'.format(i=i, ax=ax))
32293240

32303241
for block in self.blocks:
3231-
output += u('\n%s') % pprint_thing(block)
3242+
output += u('\n{block}'.format(block=pprint_thing(block)))
32323243
return output
32333244

32343245
def _verify_integrity(self):
@@ -3732,8 +3743,8 @@ def to_dict(self, copy=True):
37323743

37333744
def xs(self, key, axis=1, copy=True, takeable=False):
37343745
if axis < 1:
3735-
raise AssertionError('Can only take xs across axis >= 1, got %d' %
3736-
axis)
3746+
raise AssertionError(
3747+
'Can only take xs across axis >= 1, got {ax}'.format(ax=axis))
37373748

37383749
# take by position
37393750
if takeable:
@@ -4284,8 +4295,9 @@ def _is_indexed_like(self, other):
42844295
Check all axes except items
42854296
"""
42864297
if self.ndim != other.ndim:
4287-
raise AssertionError('Number of dimensions must agree '
4288-
'got %d and %d' % (self.ndim, other.ndim))
4298+
raise AssertionError(
4299+
'Number of dimensions must agree got {ndim} and '
4300+
'{oth_ndim}'.format(ndim=self.ndim, oth_ndim=other.ndim))
42894301
for ax, oax in zip(self.axes[1:], other.axes[1:]):
42904302
if not ax.equals(oax):
42914303
return False
@@ -4934,12 +4946,14 @@ def _maybe_compare(a, b, op):
49344946
type_names = [type(a).__name__, type(b).__name__]
49354947

49364948
if is_a_array:
4937-
type_names[0] = 'ndarray(dtype=%s)' % a.dtype
4949+
type_names[0] = 'ndarray(dtype={dtype})'.format(dtype=a.dtype)
49384950

49394951
if is_b_array:
4940-
type_names[1] = 'ndarray(dtype=%s)' % b.dtype
4952+
type_names[1] = 'ndarray(dtype={dtype})'.format(dtype=b.dtype)
49414953

4942-
raise TypeError("Cannot compare types %r and %r" % tuple(type_names))
4954+
raise TypeError(
4955+
"Cannot compare types {a!r} and {b!r}".format(a=type_names[0],
4956+
b=type_names[1]))
49434957
return result
49444958

49454959

@@ -5017,17 +5031,17 @@ def items_overlap_with_suffix(left, lsuffix, right, rsuffix):
50175031
return left, right
50185032
else:
50195033
if not lsuffix and not rsuffix:
5020-
raise ValueError('columns overlap but no suffix specified: %s' %
5021-
to_rename)
5034+
raise ValueError('columns overlap but no suffix specified: '
5035+
'{rename}'.format(rename=to_rename))
50225036

50235037
def lrenamer(x):
50245038
if x in to_rename:
5025-
return '%s%s' % (x, lsuffix)
5039+
return '{x}{lsuffix}'.format(x=x, lsuffix=lsuffix)
50265040
return x
50275041

50285042
def rrenamer(x):
50295043
if x in to_rename:
5030-
return '%s%s' % (x, rsuffix)
5044+
return '{x}{rsuffix}'.format(x=x, rsuffix=rsuffix)
50315045
return x
50325046

50335047
return (_transform_index(left, lrenamer),
@@ -5519,8 +5533,9 @@ def __init__(self, block, shape, indexers=None):
55195533
self.shape = shape
55205534

55215535
def __repr__(self):
5522-
return '%s(%r, %s)' % (self.__class__.__name__, self.block,
5523-
self.indexers)
5536+
return '{name}({block!r}, {indexers})'.format(
5537+
name=self.__class__.__name__, block=self.block,
5538+
indexers=self.indexers)
55245539

55255540
@cache_readonly
55265541
def needs_filling(self):

0 commit comments

Comments
 (0)