diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f476bff4df2cd..a71ade3da87de 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -411,7 +411,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, arr = np.array(data, dtype=dtype, copy=copy) except (ValueError, TypeError) as e: exc = TypeError('DataFrame constructor called with ' - 'incompatible data and dtype: %s' % e) + 'incompatible data and dtype: {e}'.format(e=e)) raise_with_traceback(exc) if arr.ndim == 0 and index is not None and columns is not None: @@ -520,8 +520,9 @@ def _get_axes(N, K, index=index, columns=columns): try: values = values.astype(dtype) except Exception as orig: - e = ValueError("failed to cast to '%s' (Exception was: %s)" - % (dtype, orig)) + e = ValueError("failed to cast to '{dtype}' (Exception " + "was: {orig})".format(dtype=dtype, + orig=orig)) raise_with_traceback(e) index, columns = _get_axes(*values.shape) @@ -873,8 +874,9 @@ def dot(self, other): lvals = self.values rvals = np.asarray(other) if lvals.shape[1] != rvals.shape[0]: - raise ValueError('Dot product shape mismatch, %s vs %s' % - (lvals.shape, rvals.shape)) + raise ValueError('Dot product shape mismatch, ' + '{l} vs {r}'.format(l=lvals.shape, + r=rvals.shape)) if isinstance(other, DataFrame): return self._constructor(np.dot(lvals, rvals), index=left.index, @@ -888,7 +890,7 @@ def dot(self, other): else: return Series(result, index=left.index) else: # pragma: no cover - raise TypeError('unsupported type: %s' % type(other)) + raise TypeError('unsupported type: {oth}'.format(oth=type(other))) def __matmul__(self, other): """ Matrix multiplication using binary `@` operator in Python>=3.5 """ @@ -1098,7 +1100,7 @@ def to_dict(self, orient='dict', into=dict): return into_c((t[0], dict(zip(self.columns, t[1:]))) for t in self.itertuples()) else: - raise ValueError("orient '%s' not understood" % orient) + raise ValueError("orient '{o}' not understood".format(o=orient)) def to_gbq(self, destination_table, project_id, chunksize=None, verbose=None, reauth=False, if_exists='fail', private_key=None, @@ -2140,7 +2142,7 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None, lines.append(self.index._summary()) if len(self.columns) == 0: - lines.append('Empty %s' % type(self).__name__) + lines.append('Empty {name}'.format(name=type(self).__name__)) fmt.buffer_put_lines(buf, lines) return @@ -2166,13 +2168,15 @@ def _verbose_repr(): space = max(len(pprint_thing(k)) for k in self.columns) + 4 counts = None - tmpl = "%s%s" + tmpl = "{count}{dtype}" if show_counts: counts = self.count() if len(cols) != len(counts): # pragma: no cover - raise AssertionError('Columns must equal counts (%d != %d)' - % (len(cols), len(counts))) - tmpl = "%s non-null %s" + raise AssertionError( + 'Columns must equal counts ' + '({cols:d} != {counts:d})'.format( + cols=len(cols), counts=len(counts))) + tmpl = "{count} non-null {dtype}" dtypes = self.dtypes for i, col in enumerate(self.columns): @@ -2183,7 +2187,8 @@ def _verbose_repr(): if show_counts: count = counts.iloc[i] - lines.append(_put_str(col, space) + tmpl % (count, dtype)) + lines.append(_put_str(col, space) + tmpl.format(count=count, + dtype=dtype)) def _non_verbose_repr(): lines.append(self.columns._summary(name='Columns')) @@ -2192,9 +2197,12 @@ def _sizeof_fmt(num, size_qualifier): # returns size in human readable format for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if num < 1024.0: - return "%3.1f%s %s" % (num, size_qualifier, x) + return ("{num:3.1f}{size_q}" + "{x}".format(num=num, size_q=size_qualifier, x=x)) num /= 1024.0 - return "%3.1f%s %s" % (num, size_qualifier, 'PB') + return "{num:3.1f}{size_q} {pb}".format(num=num, + size_q=size_qualifier, + pb='PB') if verbose: _verbose_repr() @@ -2207,8 +2215,9 @@ def _sizeof_fmt(num, size_qualifier): _verbose_repr() counts = self.get_dtype_counts() - dtypes = ['%s(%d)' % k for k in sorted(compat.iteritems(counts))] - lines.append('dtypes: %s' % ', '.join(dtypes)) + dtypes = ['{k}({kk:d})'.format(k=k[0], kk=k[1]) for k + in sorted(compat.iteritems(counts))] + lines.append('dtypes: {types}'.format(types=', '.join(dtypes))) if memory_usage is None: memory_usage = get_option('display.memory_usage') @@ -2226,8 +2235,9 @@ def _sizeof_fmt(num, size_qualifier): self.index._is_memory_usage_qualified()): size_qualifier = '+' mem_usage = self.memory_usage(index=True, deep=deep).sum() - lines.append("memory usage: %s\n" % - _sizeof_fmt(mem_usage, size_qualifier)) + lines.append("memory usage: {mem}\n".format( + mem=_sizeof_fmt(mem_usage, size_qualifier))) + fmt.buffer_put_lines(buf, lines) def memory_usage(self, index=True, deep=False): @@ -3013,8 +3023,8 @@ def select_dtypes(self, include=None, exclude=None): # can't both include AND exclude! if not include.isdisjoint(exclude): - raise ValueError('include and exclude overlap on %s' % - (include & exclude)) + raise ValueError('include and exclude overlap on {inc_ex}'.format( + inc_ex=(include & exclude))) # empty include/exclude -> defaults to True # three cases (we've already raised if both are empty) @@ -3869,7 +3879,8 @@ def set_index(self, keys, drop=True, append=False, inplace=False, if verify_integrity and not index.is_unique: duplicates = index.get_duplicates() - raise ValueError('Index has duplicate keys: %s' % duplicates) + raise ValueError('Index has duplicate keys: {dup}'.format( + dup=duplicates)) for c in to_remove: del frame[c] @@ -4241,7 +4252,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None, mask = count > 0 else: if how is not None: - raise ValueError('invalid how option: %s' % how) + raise ValueError('invalid how option: {h}'.format(h=how)) else: raise TypeError('must specify how or thresh') @@ -6750,8 +6761,8 @@ def _count_level(self, level, axis=0, numeric_only=False): agg_axis = frame._get_agg_axis(axis) if not isinstance(count_axis, MultiIndex): - raise TypeError("Can only count levels on hierarchical %s." % - self._get_axis_name(axis)) + raise TypeError("Can only count levels on hierarchical " + "{ax}.".format(ax=self._get_axis_name(axis))) if frame._is_mixed_type: # Since we have mixed types, calling notna(frame.values) might @@ -6829,9 +6840,9 @@ def f(x): elif filter_type == 'bool': data = self._get_bool_data() else: # pragma: no cover - e = NotImplementedError("Handling exception with filter_" - "type %s not implemented." % - filter_type) + e = NotImplementedError( + "Handling exception with filter_type {f} not" + "implemented.".format(f=filter_type)) raise_with_traceback(e) with np.errstate(all='ignore'): result = f(data.values) @@ -6843,8 +6854,8 @@ def f(x): elif filter_type == 'bool': data = self._get_bool_data() else: # pragma: no cover - msg = ("Generating numeric_only data with filter_type %s" - "not supported." % filter_type) + msg = ("Generating numeric_only data with filter_type {f}" + "not supported.".format(f=filter_type)) raise NotImplementedError(msg) values = data.values labels = data._get_agg_axis(axis) @@ -7119,7 +7130,8 @@ def to_timestamp(self, freq=None, how='start', axis=0, copy=True): elif axis == 1: new_data.set_axis(0, self.columns.to_timestamp(freq=freq, how=how)) else: # pragma: no cover - raise AssertionError('Axis must be 0 or 1. Got %s' % str(axis)) + raise AssertionError('Axis must be 0 or 1. Got {ax!s}'.format( + ax=axis)) return self._constructor(new_data) @@ -7150,7 +7162,8 @@ def to_period(self, freq=None, axis=0, copy=True): elif axis == 1: new_data.set_axis(0, self.columns.to_period(freq=freq)) else: # pragma: no cover - raise AssertionError('Axis must be 0 or 1. Got %s' % str(axis)) + raise AssertionError('Axis must be 0 or 1. Got {ax!s}'.format( + ax=axis)) return self._constructor(new_data) @@ -7509,8 +7522,9 @@ def _convert_object_array(content, columns, coerce_float=False, dtype=None): else: if len(columns) != len(content): # pragma: no cover # caller's responsibility to check for this... - raise AssertionError('%d columns passed, passed data had %s ' - 'columns' % (len(columns), len(content))) + raise AssertionError('{col:d} columns passed, passed data had ' + '{con} columns'.format(col=len(columns), + con=len(content))) # provide soft conversion of object dtypes def convert(arr): @@ -7585,4 +7599,4 @@ def _from_nested_dict(data): def _put_str(s, space): - return ('%s' % s)[:space].ljust(space) + return u'{s}'.format(s=s)[:space].ljust(space)