Skip to content

BUG: bug in xs for a Series with a multiindex (GH6258) #6259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Bug Fixes
- Bug in version string gen. for dev versions with shallow clones / install from tarball (:issue:`6127`)
- Inconsistent tz parsing Timestamp/to_datetime for current year (:issue:`5958`)
- Indexing bugs with reordered indexes (:issue:`6252`, :issue:`6254`)
- Bug in ``.xs`` with a Series multiindex (:issue:`6258`, :issue:`5684`)

pandas 0.13.1
-------------
Expand Down
20 changes: 8 additions & 12 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,21 +1262,17 @@ def xs(self, key, axis=0, level=None, copy=True, drop_level=True):
if not copy and not isinstance(loc, slice):
raise ValueError('Cannot retrieve view (copy=False)')

# level = 0
loc_is_slice = isinstance(loc, slice)
if not loc_is_slice:
indexer = [slice(None)] * self.ndim
indexer[axis] = loc
indexer = tuple(indexer)
else:
indexer = loc
# convert to a label indexer if needed
if isinstance(loc, slice):
lev_num = labels._get_level_number(level)
if labels.levels[lev_num].inferred_type == 'integer':
indexer = self.index[loc]
loc = labels[loc]

# create the tuple of the indexer
indexer = [slice(None)] * self.ndim
indexer[axis] = loc
indexer = tuple(indexer)

# select on the correct axis
if axis == 1 and loc_is_slice:
indexer = slice(None), indexer
result = self.ix[indexer]
setattr(result, result._get_axis_name(axis), new_ax)
return result
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3236,7 +3236,7 @@ def _get_level_indexer(self, key, level=0):
labels = self.labels[level]

if level > 0 or self.lexsort_depth == 0:
return labels == loc
return np.array(labels == loc,dtype=bool)
else:
# sorted, so can return slice object -> view
i = labels.searchsorted(loc, side='left')
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,24 @@ def test_series_getitem_multiindex(self):
expected = Series([2,3],index=[1,2])
assert_series_equal(result,expected)

# GH6258
s = Series([1,3,4,1,3,4],
index=MultiIndex.from_product([list('AB'),
list(date_range('20130903',periods=3))]))
result = s.xs('20130903',level=1)
expected = Series([1,1],index=list('AB'))
assert_series_equal(result,expected)

# GH5684
idx = MultiIndex.from_tuples([('a', 'one'), ('a', 'two'),
('b', 'one'), ('b', 'two')])
s = Series([1, 2, 3, 4], index=idx)
s.index.set_names(['L1', 'L2'], inplace=True)
result = s.xs('one', level='L2')
expected = Series([1, 3], index=['a', 'b'])
expected.index.set_names(['L1'], inplace=True)
assert_series_equal(result, expected)

def test_ix_general(self):

# ix general issues
Expand Down