Skip to content

Commit f9fcfae

Browse files
jschendelalanbato
authored andcommitted
CLN: replace %s syntax with .format in core.dtypes and core.sparse (pandas-dev#17270)
1 parent d9542d4 commit f9fcfae

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

pandas/core/dtypes/cast.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ def maybe_cast_item(obj, item, dtype):
516516
if dtype in (np.object_, np.bool_):
517517
obj[item] = chunk.astype(np.object_)
518518
elif not issubclass(dtype, (np.integer, np.bool_)): # pragma: no cover
519-
raise ValueError("Unexpected dtype encountered: %s" % dtype)
519+
raise ValueError("Unexpected dtype encountered: {dtype}"
520+
.format(dtype=dtype))
520521

521522

522523
def invalidate_string_dtypes(dtype_set):
@@ -620,8 +621,9 @@ def astype_nansafe(arr, dtype, copy=True):
620621
elif dtype == np.int64:
621622
return arr.view(dtype)
622623
elif dtype != _NS_DTYPE:
623-
raise TypeError("cannot astype a datetimelike from [%s] to [%s]" %
624-
(arr.dtype, dtype))
624+
raise TypeError("cannot astype a datetimelike from [{from_dtype}] "
625+
"to [{to_dtype}]".format(from_dtype=arr.dtype,
626+
to_dtype=dtype))
625627
return arr.astype(_NS_DTYPE)
626628
elif is_timedelta64_dtype(arr):
627629
if dtype == np.int64:
@@ -640,8 +642,9 @@ def astype_nansafe(arr, dtype, copy=True):
640642
result[mask] = np.nan
641643
return result
642644

643-
raise TypeError("cannot astype a timedelta from [%s] to [%s]" %
644-
(arr.dtype, dtype))
645+
raise TypeError("cannot astype a timedelta from [{from_dtype}] "
646+
"to [{to_dtype}]".format(from_dtype=arr.dtype,
647+
to_dtype=dtype))
645648

646649
return arr.astype(_TD_DTYPE)
647650
elif (np.issubdtype(arr.dtype, np.floating) and
@@ -926,7 +929,7 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
926929
dtype = _NS_DTYPE
927930
else:
928931
raise TypeError("cannot convert datetimelike to "
929-
"dtype [%s]" % dtype)
932+
"dtype [{dtype}]".format(dtype=dtype))
930933
elif is_datetime64tz:
931934

932935
# our NaT doesn't support tz's
@@ -943,7 +946,7 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
943946
dtype = _TD_DTYPE
944947
else:
945948
raise TypeError("cannot convert timedeltalike to "
946-
"dtype [%s]" % dtype)
949+
"dtype [{dtype}]".format(dtype=dtype))
947950

948951
if is_scalar(value):
949952
if value == iNaT or isna(value):
@@ -982,7 +985,8 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
982985
return tslib.ints_to_pydatetime(ints)
983986

984987
# we have a non-castable dtype that was passed
985-
raise TypeError('Cannot cast datetime64 to %s' % dtype)
988+
raise TypeError('Cannot cast datetime64 to {dtype}'
989+
.format(dtype=dtype))
986990

987991
else:
988992

pandas/core/dtypes/common.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1854,10 +1854,10 @@ def _validate_date_like_dtype(dtype):
18541854
try:
18551855
typ = np.datetime_data(dtype)[0]
18561856
except ValueError as e:
1857-
raise TypeError('%s' % e)
1857+
raise TypeError('{error}'.format(error=e))
18581858
if typ != 'generic' and typ != 'ns':
1859-
raise ValueError('%r is too specific of a frequency, try passing %r' %
1860-
(dtype.name, dtype.type.__name__))
1859+
msg = '{name!r} is too specific of a frequency, try passing {type!r}'
1860+
raise ValueError(msg.format(name=dtype.name, type=dtype.type.__name__))
18611861

18621862

18631863
_string_dtypes = frozenset(map(_get_dtype_from_object, (binary_type,
@@ -1924,6 +1924,6 @@ def pandas_dtype(dtype):
19241924
if dtype in [object, np.object_, 'object', 'O']:
19251925
return npdtype
19261926
elif npdtype.kind == 'O':
1927-
raise TypeError('dtype {0} not understood'.format(dtype))
1927+
raise TypeError('dtype {dtype} not understood'.format(dtype=dtype))
19281928

19291929
return npdtype

pandas/core/sparse/array.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def _arith_method(op, name, str_rep=None, default_axis=None, fill_zeros=None,
5252
def wrapper(self, other):
5353
if isinstance(other, np.ndarray):
5454
if len(self) != len(other):
55-
raise AssertionError("length mismatch: %d vs. %d" %
56-
(len(self), len(other)))
55+
raise AssertionError("length mismatch: {self} vs. {other}"
56+
.format(self=len(self), other=len(other)))
5757
if not isinstance(other, ABCSparseArray):
5858
dtype = getattr(other, 'dtype', None)
5959
other = SparseArray(other, fill_value=self.fill_value,
@@ -66,7 +66,8 @@ def wrapper(self, other):
6666

6767
return _wrap_result(name, result, self.sp_index, fill)
6868
else: # pragma: no cover
69-
raise TypeError('operation with %s not supported' % type(other))
69+
raise TypeError('operation with {other} not supported'
70+
.format(other=type(other)))
7071

7172
if name.startswith("__"):
7273
name = name[2:-2]
@@ -218,9 +219,9 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
218219
else:
219220
values = _sanitize_values(data)
220221
if len(values) != sparse_index.npoints:
221-
raise AssertionError("Non array-like type {0} must have"
222-
" the same length as the"
223-
" index".format(type(values)))
222+
raise AssertionError("Non array-like type {type} must "
223+
"have the same length as the index"
224+
.format(type=type(values)))
224225
# Create array, do *not* copy data by default
225226
if copy:
226227
subarr = np.array(values, dtype=dtype, copy=True)
@@ -330,9 +331,10 @@ def __len__(self):
330331
return 0
331332

332333
def __unicode__(self):
333-
return '%s\nFill: %s\n%s' % (printing.pprint_thing(self),
334-
printing.pprint_thing(self.fill_value),
335-
printing.pprint_thing(self.sp_index))
334+
return '{self}\nFill: {fill}\n{index}'.format(
335+
self=printing.pprint_thing(self),
336+
fill=printing.pprint_thing(self.fill_value),
337+
index=printing.pprint_thing(self.sp_index))
336338

337339
def disable(self, other):
338340
raise NotImplementedError('inplace binary ops not supported')
@@ -377,8 +379,8 @@ def fill_value(self, value):
377379
if is_dtype_equal(self.dtype, new_dtype):
378380
self._fill_value = fill_value
379381
else:
380-
msg = 'unable to set fill_value {0} to {1} dtype'
381-
raise ValueError(msg.format(value, self.dtype))
382+
msg = 'unable to set fill_value {fill} to {dtype} dtype'
383+
raise ValueError(msg.format(fill=value, dtype=self.dtype))
382384

383385
def get_values(self, fill=None):
384386
""" return a dense representation """
@@ -466,7 +468,8 @@ def take(self, indices, axis=0, allow_fill=True,
466468
nv.validate_take(tuple(), kwargs)
467469

468470
if axis:
469-
raise ValueError("axis must be 0, input was {0}".format(axis))
471+
raise ValueError("axis must be 0, input was {axis}"
472+
.format(axis=axis))
470473

471474
if is_integer(indices):
472475
# return scalar
@@ -482,12 +485,12 @@ def take(self, indices, axis=0, allow_fill=True,
482485
'all indices must be >= -1')
483486
raise ValueError(msg)
484487
elif (n <= indices).any():
485-
msg = 'index is out of bounds for size {0}'
486-
raise IndexError(msg.format(n))
488+
msg = 'index is out of bounds for size {size}'.format(size=n)
489+
raise IndexError(msg)
487490
else:
488491
if ((indices < -n) | (n <= indices)).any():
489-
msg = 'index is out of bounds for size {0}'
490-
raise IndexError(msg.format(n))
492+
msg = 'index is out of bounds for size {size}'.format(size=n)
493+
raise IndexError(msg)
491494

492495
indices = indices.astype(np.int32)
493496
if not (allow_fill and fill_value is not None):
@@ -543,8 +546,8 @@ def astype(self, dtype=None, copy=True):
543546
else:
544547
fill_value = dtype.type(self.fill_value)
545548
except ValueError:
546-
msg = 'unable to coerce current fill_value {0} to {1} dtype'
547-
raise ValueError(msg.format(self.fill_value, dtype))
549+
msg = 'unable to coerce current fill_value {fill} to {dtype} dtype'
550+
raise ValueError(msg.format(fill=self.fill_value, dtype=dtype))
548551
return self._simple_new(sp_values, self.sp_index,
549552
fill_value=fill_value)
550553

pandas/core/sparse/frame.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ def _prep_index(self, data, index, columns):
214214
columns = _default_index(K)
215215

216216
if len(columns) != K:
217-
raise ValueError('Column length mismatch: %d vs. %d' %
218-
(len(columns), K))
217+
raise ValueError('Column length mismatch: {columns} vs. {K}'
218+
.format(columns=len(columns), K=K))
219219
if len(index) != N:
220-
raise ValueError('Index length mismatch: %d vs. %d' %
221-
(len(index), N))
220+
raise ValueError('Index length mismatch: {index} vs. {N}'
221+
.format(index=len(index), N=N))
222222
return index, columns
223223

224224
def to_coo(self):
@@ -725,17 +725,17 @@ def _maybe_rename_join(self, other, lsuffix, rsuffix):
725725
to_rename = self.columns.intersection(other.columns)
726726
if len(to_rename) > 0:
727727
if not lsuffix and not rsuffix:
728-
raise ValueError('columns overlap but no suffix specified: %s'
729-
% to_rename)
728+
raise ValueError('columns overlap but no suffix specified: '
729+
'{to_rename}'.format(to_rename=to_rename))
730730

731731
def lrenamer(x):
732732
if x in to_rename:
733-
return '%s%s' % (x, lsuffix)
733+
return '{x}{lsuffix}'.format(x=x, lsuffix=lsuffix)
734734
return x
735735

736736
def rrenamer(x):
737737
if x in to_rename:
738-
return '%s%s' % (x, rsuffix)
738+
return '{x}{rsuffix}'.format(x=x, rsuffix=rsuffix)
739739
return x
740740

741741
this = self.rename(columns=lrenamer)

pandas/core/sparse/list.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ def __init__(self, data=None, fill_value=np.nan):
3535

3636
def __unicode__(self):
3737
contents = '\n'.join(repr(c) for c in self._chunks)
38-
return '%s\n%s' % (object.__repr__(self), pprint_thing(contents))
38+
return '{self}\n{contents}'.format(self=object.__repr__(self),
39+
contents=pprint_thing(contents))
3940

4041
def __len__(self):
4142
return sum(len(c) for c in self._chunks)
4243

4344
def __getitem__(self, i):
4445
if i < 0:
4546
if i + len(self) < 0: # pragma: no cover
46-
raise ValueError('%d out of range' % i)
47+
raise ValueError('{index} out of range'.format(index=i))
4748
i += len(self)
4849

4950
passed = 0

pandas/core/sparse/series.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def wrapper(self, other):
6565
index=self.index,
6666
name=self.name)
6767
else: # pragma: no cover
68-
raise TypeError('operation with %s not supported' % type(other))
68+
raise TypeError('operation with {other} not supported'
69+
.format(other=type(other)))
6970

7071
wrapper.__name__ = name
7172
if name.startswith("__"):
@@ -295,7 +296,8 @@ def shape(self):
295296
def __unicode__(self):
296297
# currently, unicode is same as repr...fixes infinite loop
297298
series_rep = Series.__unicode__(self)
298-
rep = '%s\n%s' % (series_rep, repr(self.sp_index))
299+
rep = '{series}\n{index!r}'.format(series=series_rep,
300+
index=self.sp_index)
299301
return rep
300302

301303
def __array_wrap__(self, result, context=None):

0 commit comments

Comments
 (0)