Skip to content

Commit 859d260

Browse files
committed
BUG: segfault when set_index with MultiIndex. close #3308
1 parent 7b58f1d commit 859d260

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ pandas 0.11.0
294294
- Panel() and Panel.from_dict() now respects ordering when give OrderedDict (GH3303_)
295295
- DataFrame where with a datetimelike incorrectly selecting (GH3311_)
296296
- Ensure index casts work even in Int64Index
297+
- Fix set_index segfault when passing MultiIndex (GH3308_)
297298

298299
.. _GH3294: https://github.com/pydata/pandas/issues/3294
299300
.. _GH622: https://github.com/pydata/pandas/issues/622
@@ -402,6 +403,7 @@ pandas 0.11.0
402403
.. _GH3258: https://github.com/pydata/pandas/issues/3258
403404
.. _GH3283: https://github.com/pydata/pandas/issues/3283
404405
.. _GH2919: https://github.com/pydata/pandas/issues/2919
406+
.. _GH3308: https://github.com/pydata/pandas/issues/3308
405407
.. _GH3311: https://github.com/pydata/pandas/issues/3311
406408

407409
pandas 0.10.1

pandas/core/index.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __new__(cls, data, dtype=None, copy=False, name=None):
109109
if issubclass(data.dtype.type, np.integer):
110110
return Int64Index(data, copy=copy, dtype=dtype, name=name)
111111

112-
subarr = com._ensure_object(data)
112+
subarr = com._asarray_tuplesafe(data, dtype=object)
113113
elif np.isscalar(data):
114114
raise ValueError('Index(...) must be called with a collection '
115115
'of some kind, %s was passed' % repr(data))
@@ -1169,7 +1169,7 @@ def slice_indexer(self, start=None, end=None, step=None):
11691169
If None, defaults to the beginning
11701170
end : label, default None
11711171
If None, defaults to the end
1172-
step : int, default None
1172+
step : int, default None
11731173
11741174
Returns
11751175
-------
@@ -2779,7 +2779,7 @@ def _handle_legacy_indexes(indexes):
27792779

27802780
def _get_consensus_names(indexes):
27812781

2782-
# find the non-none names, need to tupleify to make
2782+
# find the non-none names, need to tupleify to make
27832783
# the set hashable, then reverse on return
27842784
consensus_names = set([ tuple(i.names) for i in indexes if all(n is not None for n in i.names) ])
27852785
if len(consensus_names) == 1:

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _setitem_with_indexer(self, indexer, value):
113113
het_axis = self.obj._het_axis
114114
het_idx = indexer[het_axis]
115115

116-
if isinstance(het_idx, (int, long)):
116+
if com.is_integer(het_idx):
117117
het_idx = [het_idx]
118118

119119
plane_indexer = indexer[:het_axis] + indexer[het_axis + 1:]
@@ -820,7 +820,7 @@ def _convert_key(self, key):
820820

821821
def __getitem__(self, key):
822822
if not isinstance(key, tuple):
823-
823+
824824
# we could have a convertible item here (e.g. Timestamp)
825825
if not _is_list_like(key):
826826
key = tuple([ key ])

pandas/tests/test_multilevel.py

+11
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,17 @@ def test_duplicate_groupby_issues(self):
18221822
result = s.groupby(s.index).first()
18231823
self.assertEquals(len(result), 3)
18241824

1825+
def test_multiindex_set_index(self):
1826+
# segfault in #3308
1827+
d = {'t1': [2, 2.5, 3], 't2': [4, 5, 6]}
1828+
df = DataFrame(d)
1829+
tuples = [(0, 1), (0, 2), (1, 2)]
1830+
df['tuples'] = tuples
1831+
1832+
index = MultiIndex.from_tuples(df['tuples'])
1833+
# it works!
1834+
df.set_index(index)
1835+
18251836
if __name__ == '__main__':
18261837

18271838
# unittest.main()

0 commit comments

Comments
 (0)