1313from pandas .core .internals import BlockManager
1414import pandas .core .common as com
1515from pandas import compat
16- from pandas .compat import map , zip
16+ from pandas .compat import map , zip , lrange
1717from pandas .core .common import (isnull , notnull , is_list_like ,
1818 _values_from_object ,
19- _infer_dtype_from_scalar , _maybe_promote )
19+ _infer_dtype_from_scalar , _maybe_promote ,
20+ ABCSeries )
2021
2122class NDFrame (PandasObject ):
2223
@@ -382,7 +383,77 @@ def swaplevel(self, i, j, axis=0):
382383 result ._data .set_axis (axis , labels .swaplevel (i , j ))
383384 return result
384385
385- def rename_axis (self , mapper , axis = 0 , copy = True ):
386+ #----------------------------------------------------------------------
387+ # Rename
388+
389+ def rename (self , * args , ** kwargs ):
390+ """
391+ Alter axes input function or
392+ functions. Function / dict values must be unique (1-to-1). Labels not
393+ contained in a dict / Series will be left as-is.
394+
395+ Parameters
396+ ----------
397+ axis keywords for this object
398+ (e.g. index for Series,
399+ index,columns for DataFrame,
400+ items,major_axis,minor_axis for Panel)
401+ : dict-like or function, optional
402+ Transformation to apply to that axis values
403+
404+ copy : boolean, default True
405+ Also copy underlying data
406+ inplace : boolean, default False
407+ Whether to return a new PandasObject. If True then value of copy is
408+ ignored.
409+
410+ Returns
411+ -------
412+ renamed : PandasObject (new object)
413+ """
414+
415+ axes , kwargs = self ._construct_axes_from_arguments (args , kwargs )
416+ copy = kwargs .get ('copy' , True )
417+ inplace = kwargs .get ('inplace' , False )
418+
419+ if (com ._count_not_none (* axes .values ()) == 0 ):
420+ raise Exception ('must pass an index to rename' )
421+
422+ # renamer function if passed a dict
423+ def _get_rename_function (mapper ):
424+ if isinstance (mapper , (dict , ABCSeries )):
425+ def f (x ):
426+ if x in mapper :
427+ return mapper [x ]
428+ else :
429+ return x
430+ else :
431+ f = mapper
432+
433+ return f
434+
435+
436+ self ._consolidate_inplace ()
437+ result = self if inplace else self .copy (deep = copy )
438+
439+ # start in the axis order to eliminate too many copies
440+ for axis in lrange (self ._AXIS_LEN ):
441+ v = axes .get (self ._AXIS_NAMES [axis ])
442+ if v is None : continue
443+ f = _get_rename_function (v )
444+
445+ baxis = self ._get_block_manager_axis (axis )
446+ result ._data = result ._data .rename (f , axis = baxis , copy = copy )
447+ result ._clear_item_cache ()
448+
449+ if inplace :
450+ self ._data = result ._data
451+ self ._clear_item_cache ()
452+
453+ else :
454+ return result ._propogate_attributes (self )
455+
456+ def rename_axis (self , mapper , axis = 0 , copy = True , inplace = False ):
386457 """
387458 Alter index and / or columns using input function or functions.
388459 Function / dict values must be unique (1-to-1). Labels not contained in
@@ -394,24 +465,16 @@ def rename_axis(self, mapper, axis=0, copy=True):
394465 axis : int, default 0
395466 copy : boolean, default True
396467 Also copy underlying data
468+ inplace : boolean, default False
397469
398470 Returns
399471 -------
400472 renamed : type of caller
401473 """
402- # should move this at some point
403- from pandas .core .series import _get_rename_function
404-
405- mapper_f = _get_rename_function (mapper )
406-
407- if axis == 0 :
408- new_data = self ._data .rename_items (mapper_f , copydata = copy )
409- else :
410- new_data = self ._data .rename_axis (mapper_f , axis = axis )
411- if copy :
412- new_data = new_data .copy ()
413-
414- return self ._constructor (new_data )
474+ axis = self ._AXIS_NAMES [axis ]
475+ d = { 'copy' : copy , 'inplace' : inplace }
476+ d [axis ] = mapper
477+ return self .rename (** d )
415478
416479 #----------------------------------------------------------------------
417480 # Comparisons
@@ -1373,7 +1436,7 @@ def copy(self, deep=True):
13731436 data = self ._data
13741437 if deep :
13751438 data = data .copy ()
1376- return self ._constructor (data )
1439+ return self ._constructor (data ). _propogate_attributes ( self )
13771440
13781441 def convert_objects (self , convert_dates = True , convert_numeric = False , copy = True ):
13791442 """
0 commit comments