Skip to content

move rename functionality out of internals #21924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 8, 2018
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 10 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -3406,8 +3403,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):
"""
Expand Down Expand Up @@ -3463,8 +3462,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
Expand Down Expand Up @@ -3980,6 +3981,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)
Expand Down
15 changes: 2 additions & 13 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down