From 4ffa5d5872b480e8fe3f4ae3e9b3b31fe4282995 Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Wed, 15 Nov 2017 23:44:11 +0000 Subject: [PATCH 1/4] CLN: Replacing % with .format in core/panel --- pandas/core/panel.py | 98 +++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index d2b513367fc78..ef77d738e830e 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -42,9 +42,9 @@ klass="Panel", axes_single_arg="{0, 1, 2, 'items', 'major_axis', 'minor_axis'}", optional_mapper='', optional_axis='', optional_labels='') -_shared_doc_kwargs['args_transpose'] = ("three positional arguments: each one" - "of\n%s" % - _shared_doc_kwargs['axes_single_arg']) +_shared_doc_kwargs['args_transpose'] = ( + "three positional arguments: each one of\n{ax_single}".format( + ax_single=_shared_doc_kwargs['axes_single_arg'])) def _ensure_like_indices(time, panels): @@ -311,7 +311,8 @@ def _init_matrix(self, data, axes, dtype=None, copy=False): try: values = values.astype(dtype) except Exception: - raise ValueError('failed to cast to %s' % dtype) + raise ValueError('failed to cast to ' + '{datatype}'.format(datatype=dtype)) shape = values.shape fixed_axes = [] @@ -352,18 +353,18 @@ def __unicode__(self): class_name = str(self.__class__) - shape = self.shape - dims = u('Dimensions: %s') % ' x '.join( - ["%d (%s)" % (s, a) for a, s in zip(self._AXIS_ORDERS, shape)]) + dims = u('Dimensions: {dimensions}'.format(dimensions=' x '.join( + ["{shape} ({axis})".format(shape=shape, axis=axis) for axis, shape + in zip(self._AXIS_ORDERS, self.shape)]))) def axis_pretty(a): v = getattr(self, a) if len(v) > 0: - return u('%s axis: %s to %s') % (a.capitalize(), - pprint_thing(v[0]), - pprint_thing(v[-1])) + return u('{ax} axis: {x} to {y}'.format(ax=a.capitalize(), + x=pprint_thing(v[0]), + y=pprint_thing(v[-1]))) else: - return u('%s axis: None') % a.capitalize() + return u('{ax} axis: None'.format(ax=a.capitalize())) output = '\n'.join( [class_name, dims] + [axis_pretty(a) for a in self._AXIS_ORDERS]) @@ -610,7 +611,8 @@ def __setitem__(self, key, value): elif is_scalar(value): mat = cast_scalar_to_array(shape[1:], value) else: - raise TypeError('Cannot set item of type: %s' % str(type(value))) + raise TypeError('Cannot set item of ' + 'type: {dtype!s}'.format(dtype=type(value))) mat = mat.reshape(tuple([1]) + shape[1:]) NDFrame._set_item(self, key, mat) @@ -739,9 +741,9 @@ def _combine(self, other, func, axis=0): elif is_scalar(other): return self._combine_const(other, func) else: - raise NotImplementedError("%s is not supported in combine " - "operation with %s" % - (str(type(other)), str(type(self)))) + raise NotImplementedError("{otype!s} is not supported in combine " + "operation with {selftype!s}".format( + otype=type(other), selftype=type(self))) def _combine_const(self, other, func, try_cast=True): with np.errstate(all='ignore'): @@ -1188,8 +1190,8 @@ def _construct_return_type(self, result, axes=None): return self._constructor_sliced( result, **self._extract_axes_for_slice(self, axes)) - raise ValueError('invalid _construct_return_type [self->%s] ' - '[result->%s]' % (self, result)) + raise ValueError('invalid _construct_return_type [self->{self}] ' + '[result->{result}]'.format(self=self, result=result)) def _wrap_result(self, result, axis): axis = self._get_axis_name(axis) @@ -1508,7 +1510,8 @@ def _extract_axis(self, data, axis=0, intersect=False): if have_raw_arrays: lengths = list(set(raw_lengths)) if len(lengths) > 1: - raise ValueError('ndarrays must match shape on axis %d' % axis) + raise ValueError('ndarrays must match shape on ' + 'axis {ax}'.format(ax=axis)) if have_frames: if lengths[0] != len(index): @@ -1525,20 +1528,6 @@ def _extract_axis(self, data, axis=0, intersect=False): def _add_aggregate_operations(cls, use_numexpr=True): """ add the operations to the cls; evaluate the doc strings again """ - # doc strings substitors - _agg_doc = """ -Wrapper method for %%s - -Parameters ----------- -other : %s or %s""" % (cls._constructor_sliced.__name__, cls.__name__) + """ -axis : {""" + ', '.join(cls._AXIS_ORDERS) + "}" + """ - Axis to broadcast over - -Returns -------- -""" + cls.__name__ + "\n" - def _panel_arith_method(op, name, str_rep=None, default_axis=None, fill_zeros=None, **eval_kwargs): def na_op(x, y): @@ -1566,27 +1555,44 @@ def na_op(x, y): equiv = 'panel ' + op_desc['op'] + ' other' _op_doc = """ - %%s of series and other, element-wise (binary operator `%%s`). - Equivalent to ``%%s``. +{desc} of series and other, element-wise (binary operator `{op_name}`). +Equivalent to ``{equiv}``. + +Parameters +---------- +other : {construct} or {cls_name} +axis : {{axis_order}} + Axis to broadcast over + +Returns +------- +{cls_name} + +See also +-------- +{cls_name}.{reverse}\n""" + doc = _op_doc.format( + desc=op_desc['desc'], op_name=op_name, equiv=equiv, + construct=cls._constructor_sliced.__name__, + cls_name=cls.__name__, reverse=op_desc['reverse'], + axis_order=', '.join(cls._AXIS_ORDERS)) + else: + _agg_doc = """ + Wrapper method for {wrp_method} Parameters ---------- - other : %s or %s""" % (cls._constructor_sliced.__name__, - cls.__name__) + """ - axis : {""" + ', '.join(cls._AXIS_ORDERS) + "}" + """ + other : {construct} or {cls_name} + axis : {{axis_order}} Axis to broadcast over Returns ------- - """ + cls.__name__ + """ - - See also - -------- - """ + cls.__name__ + ".%s\n" - doc = _op_doc % (op_desc['desc'], op_name, equiv, - op_desc['reverse']) - else: - doc = _agg_doc % name + {cls_name}\n""" + doc = _agg_doc.format( + construct=cls._constructor_sliced.__name__, + cls_name=cls.__name__, wrp_method=name, + axis_order=', '.join(cls._AXIS_ORDERS)) @Appender(doc) def f(self, other, axis=0): From 7c40f6a1a5eb9adae5582b7acf17fc49b5385f6f Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Wed, 15 Nov 2017 23:50:06 +0000 Subject: [PATCH 2/4] Flake8 passing --- pandas/core/panel.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index ef77d738e830e..6d35dd86474cf 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -741,9 +741,9 @@ def _combine(self, other, func, axis=0): elif is_scalar(other): return self._combine_const(other, func) else: - raise NotImplementedError("{otype!s} is not supported in combine " - "operation with {selftype!s}".format( - otype=type(other), selftype=type(self))) + raise NotImplementedError( + "{otype!s} is not supported in combine operation with " + "{selftype!s}".format(otype=type(other), selftype=type(self))) def _combine_const(self, other, func, try_cast=True): with np.errstate(all='ignore'): From 29c22b4021ebd9d3568596e65f913ea63605b6f5 Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Thu, 16 Nov 2017 00:01:08 +0000 Subject: [PATCH 3/4] Properly escaping {} literals --- pandas/core/panel.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 6d35dd86474cf..d160f077e683b 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1561,7 +1561,7 @@ def na_op(x, y): Parameters ---------- other : {construct} or {cls_name} -axis : {{axis_order}} +axis : {{{axis_order}}} Axis to broadcast over Returns @@ -1583,7 +1583,7 @@ def na_op(x, y): Parameters ---------- other : {construct} or {cls_name} - axis : {{axis_order}} + axis : {{{axis_order}}} Axis to broadcast over Returns From 25c200800578aa8933283ec3590c4c24fd3758ee Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Thu, 16 Nov 2017 00:26:13 +0000 Subject: [PATCH 4/4] Adding comment back in --- pandas/core/panel.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index d160f077e683b..327180b6a6e84 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1577,6 +1577,7 @@ def na_op(x, y): cls_name=cls.__name__, reverse=op_desc['reverse'], axis_order=', '.join(cls._AXIS_ORDERS)) else: + # doc strings substitors _agg_doc = """ Wrapper method for {wrp_method}