Skip to content
This repository
Browse code

BUG: Int64Index.take and MultiIndex.take don't lost names, GH #262

  • Loading branch information...
commit 9ddfc57c383ae4ce36b81fa14a227a1e0037c552 1 parent 06130c6
Wes McKinney wesm authored
2  RELEASE.rst
Source Rendered
@@ -158,6 +158,8 @@ feedback on the library.
158 158 - `DataFrame.iteritems` and `DataFrame._series` not assigning name attribute
159 159 - Panel.__repr__ raised exception on length-0 major/minor axes
160 160 - `DataFrame.join` on key with empty DataFrame produced incorrect columns
  161 + - `Int64Index.take` and `MultiIndex.take` lost name field, fix downstream
  162 + issue GH #262
161 163 - `read_csv` / `read_table` fixes
162 164 - Be less aggressive about converting float->int in cases of floating point
163 165 representations of integers like 1.0, 2.0, etc.
5 pandas/core/index.py
@@ -759,7 +759,7 @@ def take(self, *args, **kwargs):
759 759 Analogous to ndarray.take
760 760 """
761 761 taken = self.values.take(*args, **kwargs)
762   - return Int64Index(taken)
  762 + return Int64Index(taken, name=self.name)
763 763
764 764 class DateIndex(Index):
765 765 pass
@@ -1064,7 +1064,8 @@ def take(self, *args, **kwargs):
1064 1064 Analogous to ndarray.take
1065 1065 """
1066 1066 new_labels = [lab.take(*args, **kwargs) for lab in self.labels]
1067   - return MultiIndex(levels=self.levels, labels=new_labels)
  1067 + return MultiIndex(levels=self.levels, labels=new_labels,
  1068 + names=self.names)
1068 1069
1069 1070 def append(self, other):
1070 1071 """
9 pandas/tests/test_index.py
@@ -556,6 +556,11 @@ def test_prevent_casting(self):
556 556 result = self.index.astype('O')
557 557 self.assert_(result.dtype == np.object_)
558 558
  559 + def test_take_preserve_name(self):
  560 + index = Int64Index([1,2,3,4], name='foo')
  561 + taken = index.take([3,0,1])
  562 + self.assertEqual(index.name, taken.name)
  563 +
559 564 class TestMultiIndex(unittest.TestCase):
560 565
561 566 def setUp(self):
@@ -967,6 +972,10 @@ def test_insert(self):
967 972 # key wrong length
968 973 self.assertRaises(Exception, self.index.insert, 0, ('foo2',))
969 974
  975 + def test_take_preserve_name(self):
  976 + taken = self.index.take([3,0,1])
  977 + self.assertEqual(taken.names, self.index.names)
  978 +
970 979 class TestFactor(unittest.TestCase):
971 980
972 981 def setUp(self):
4 pandas/tests/test_multilevel.py
@@ -75,6 +75,10 @@ def test_reindex_preserve_levels(self):
75 75 chunk = ymdT.ix[:, new_index]
76 76 self.assert_(chunk.columns is new_index)
77 77
  78 + def test_sort_index_preserve_levels(self):
  79 + result = self.frame.sort_index()
  80 + self.assertEquals(result.index.names, self.frame.index.names)
  81 +
78 82 def test_repr_to_string(self):
79 83 repr(self.frame)
80 84 repr(self.ymd)

0 comments on commit 9ddfc57

Please sign in to comment.
Something went wrong with that request. Please try again.