@@ -1578,34 +1578,31 @@ def _get_method(self, method):
15781578 }
15791579 return aliases .get (method , method )
15801580
1581- def reindex (self , target , method = None , level = None , limit = None ,
1582- copy_if_needed = False ):
1581+ def reindex (self , target , method = None , level = None , limit = None ):
15831582 """
1584- For Index, simply returns the new index and the results of
1585- get_indexer. Provided here to enable an interface that is amenable for
1586- subclasses of Index whose internals are different (like MultiIndex)
1583+ Create index with target's values (move/add/delete values as necessary)
15871584
15881585 Returns
15891586 -------
1590- (new_index, indexer, mask) : tuple
1587+ new_index : pd.Index
1588+ Resulting index
1589+ indexer : np.ndarray or None
1590+ Indices of output values in original index
1591+
15911592 """
1593+ # GH6552: preserve names when reindexing to non-named target
1594+ # (i.e. neither Index nor Series).
1595+ preserve_names = not hasattr (target , 'name' )
1596+
15921597 target = _ensure_index (target )
15931598 if level is not None :
15941599 if method is not None :
15951600 raise TypeError ('Fill method not supported if level passed' )
15961601 _ , indexer , _ = self ._join_level (target , level , how = 'right' ,
15971602 return_indexers = True )
15981603 else :
1599-
16001604 if self .equals (target ):
16011605 indexer = None
1602-
1603- # to avoid aliasing an existing index
1604- if (copy_if_needed and target .name != self .name and
1605- self .name is not None ):
1606- if target .name is None :
1607- target = self .copy ()
1608-
16091606 else :
16101607 if self .is_unique :
16111608 indexer = self .get_indexer (target , method = method ,
@@ -1616,6 +1613,10 @@ def reindex(self, target, method=None, level=None, limit=None,
16161613 "with a method or limit" )
16171614 indexer , missing = self .get_indexer_non_unique (target )
16181615
1616+ if preserve_names and target .nlevels == 1 and target .name != self .name :
1617+ target = target .copy ()
1618+ target .name = self .name
1619+
16191620 return target , indexer
16201621
16211622 def join (self , other , how = 'left' , level = None , return_indexers = False ):
@@ -3686,17 +3687,21 @@ def get_indexer(self, target, method=None, limit=None):
36863687
36873688 return com ._ensure_platform_int (indexer )
36883689
3689- def reindex (self , target , method = None , level = None , limit = None ,
3690- copy_if_needed = False ):
3690+ def reindex (self , target , method = None , level = None , limit = None ):
36913691 """
3692- Performs any necessary conversion on the input index and calls
3693- get_indexer. This method is here so MultiIndex and an Index of
3694- like-labeled tuples can play nice together
3692+ Create index with target's values (move/add/delete values as necessary)
36953693
36963694 Returns
36973695 -------
3698- (new_index, indexer, mask) : (MultiIndex, ndarray, ndarray)
3696+ new_index : pd.MultiIndex
3697+ Resulting index
3698+ indexer : np.ndarray or None
3699+ Indices of output values in original index
3700+
36993701 """
3702+ # GH6552: preserve names when reindexing to non-named target
3703+ # (i.e. neither Index nor Series).
3704+ preserve_names = not hasattr (target , 'names' )
37003705
37013706 if level is not None :
37023707 if method is not None :
@@ -3724,6 +3729,11 @@ def reindex(self, target, method=None, level=None, limit=None,
37243729 # hopefully?
37253730 target = MultiIndex .from_tuples (target )
37263731
3732+ if (preserve_names and target .nlevels == self .nlevels and
3733+ target .names != self .names ):
3734+ target = target .copy (deep = False )
3735+ target .names = self .names
3736+
37273737 return target , indexer
37283738
37293739 @cache_readonly
0 commit comments