From 8078500e2e72fd4f87e4635a46bdce1324daa802 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 7 Sep 2018 19:46:53 -0700 Subject: [PATCH] move rename functionality out of internals (#21924) --- .coveragerc | 1 + pandas/core/generic.py | 18 ++++++++++-------- pandas/core/internals/managers.py | 15 ++------------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/.coveragerc b/.coveragerc index f5c8b701a79a8..13baa100b84b7 100644 --- a/.coveragerc +++ b/.coveragerc @@ -17,6 +17,7 @@ exclude_lines = # Don't complain if tests don't hit defensive assertion code: raise AssertionError raise NotImplementedError + AbstractMethodError # Don't complain if non-runnable code isn't run: if 0: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index dd5552151f61b..71dac0ea2e98a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -289,10 +289,7 @@ def set_axis(a, i): for i, a in cls._AXIS_NAMES.items(): set_axis(a, i) - # addtl parms - if isinstance(ns, dict): - for k, v in ns.items(): - setattr(cls, k, v) + assert not isinstance(ns, dict) def _construct_axes_dict(self, axes=None, **kwargs): """Return an axes dictionary for myself.""" @@ -3483,8 +3480,10 @@ def add_prefix(self, prefix): 2 3 5 3 4 6 """ - new_data = self._data.add_prefix(prefix) - return self._constructor(new_data).__finalize__(self) + f = functools.partial('{prefix}{}'.format, prefix=prefix) + + mapper = {self._info_axis_name: f} + return self.rename(**mapper) def add_suffix(self, suffix): """ @@ -3540,8 +3539,10 @@ def add_suffix(self, suffix): 2 3 5 3 4 6 """ - new_data = self._data.add_suffix(suffix) - return self._constructor(new_data).__finalize__(self) + f = functools.partial('{}{suffix}'.format, suffix=suffix) + + mapper = {self._info_axis_name: f} + return self.rename(**mapper) _shared_docs['sort_values'] = """ Sort by the values along either axis @@ -4057,6 +4058,7 @@ def _reindex_with_indexers(self, reindexers, fill_value=None, copy=False, return self._constructor(new_data).__finalize__(self) + # TODO: unused; remove? def _reindex_axis(self, new_index, fill_method, axis, copy): new_data = self._data.reindex_axis(new_index, axis=axis, method=fill_method, copy=copy) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index e64ba44bb8a92..63738594799f5 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -176,20 +176,11 @@ def rename_axis(self, mapper, axis, copy=True, level=None): axis : int copy : boolean, default True level : int, default None - """ obj = self.copy(deep=copy) obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level)) return obj - def add_prefix(self, prefix): - f = partial('{prefix}{}'.format, prefix=prefix) - return self.rename_axis(f, axis=0) - - def add_suffix(self, suffix): - f = partial('{}{suffix}'.format, suffix=suffix) - return self.rename_axis(f, axis=0) - @property def _is_single_block(self): if self.ndim == 1: @@ -222,12 +213,10 @@ def _rebuild_blknos_and_blklocs(self): self._blknos = new_blknos self._blklocs = new_blklocs - # make items read only for now - def _get_items(self): + @property + def items(self): return self.axes[0] - items = property(fget=_get_items) - def _get_counts(self, f): """ return a dict of the counts of the function in BlockManager """ self._consolidate_inplace()