Skip to content

Commit

Permalink
move rename functionality out of internals (pandas-dev#21924)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and victor committed Sep 30, 2018
1 parent 5b981fe commit 7a1bfc4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
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 @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
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

0 comments on commit 7a1bfc4

Please sign in to comment.