Skip to content

Commit 4e4d6f5

Browse files
committed
FIX: interesction and union don't index names anymore. fixes #9943 partly #9862
1 parent 6908719 commit 4e4d6f5

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

doc/source/whatsnew/v0.16.1.txt

+10
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,13 @@ Bug Fixes
243243

244244

245245
- Bug in hiding ticklabels with subplots and shared axes when adding a new plot to an existing grid of axes (:issue:`9158`)
246+
247+
248+
249+
250+
251+
252+
253+
254+
255+
- ``Union`` and ``intersection`` now wont change index name. (:issue:`9943`)

pandas/core/index.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ def union(self, other):
12721272
return self._wrap_union_result(other, result)
12731273

12741274
def _wrap_union_result(self, other, result):
1275-
name = self.name if self.name == other.name else None
1275+
name = self.name#GH 9943, 9862 if self.name == other.name else None
12761276
return self.__class__(data=result, name=name)
12771277

12781278
def intersection(self, other):
@@ -1319,8 +1319,9 @@ def intersection(self, other):
13191319
indexer = indexer[indexer != -1]
13201320

13211321
taken = self.take(indexer)
1322-
if self.name != other.name:
1323-
taken.name = None
1322+
#GH 9943, 9862
1323+
#if self.name != other.name:
1324+
# taken.name = None
13241325
return taken
13251326

13261327
def difference(self, other):

pandas/tests/test_index.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
CategoricalIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex)
1818
from pandas.core.index import InvalidIndexError, NumericIndex
1919
from pandas.util.testing import (assert_almost_equal, assertRaisesRegexp,
20-
assert_copy)
20+
assert_copy, assert_frame_equal)
2121
from pandas import compat
2222
from pandas.compat import long
2323

@@ -603,6 +603,15 @@ def test_shift(self):
603603
shifted.name = 'shifted'
604604
self.assertEqual(shifted.name, shifted.shift(1, 'D').name)
605605

606+
def test_intersection_preserves_name(self):
607+
#GH 9943
608+
df = pd.DataFrame([np.nan, np.nan], columns = ['tags'], index=pd.Int64Index([4815961, 4815962], dtype='int64', name='id'))
609+
assert str(df) == ' tags\nid \n4815961 NaN\n4815962 NaN'
610+
L = [4815962]
611+
assert list(L) == list(df.index.intersection(L))
612+
assert df.ix[L].tags.index.name == df.ix[df.index.intersection(L)].tags.index.name
613+
assert_frame_equal(df.ix[L], df.ix[df.index.intersection(L)])
614+
606615
def test_intersection(self):
607616
first = self.strIndex[:20]
608617
second = self.strIndex[:10]
@@ -627,7 +636,7 @@ def test_intersection(self):
627636

628637
# if target name is different, it will be reset
629638
idx3 = Index([3, 4, 5, 6, 7], name='other')
630-
expected3 = Index([3, 4, 5], name=None)
639+
expected3 = Index([3, 4, 5], name='idx')
631640
result3 = idx1.intersection(idx3)
632641
self.assertTrue(result3.equals(expected3))
633642
self.assertEqual(result3.name, expected3.name)
@@ -677,9 +686,10 @@ def test_union(self):
677686
union = first.union(second)
678687
self.assertEqual(union.name, 'A')
679688

689+
#preserve name if different GH 9862
680690
second.name = 'B'
681691
union = first.union(second)
682-
self.assertIsNone(union.name)
692+
self.assertEquals('A', union.name)
683693

684694
def test_add(self):
685695

0 commit comments

Comments
 (0)