Skip to content

Commit

Permalink
Merge pull request #3125 from jreback/GH2903
Browse files Browse the repository at this point in the history
BUG: GH2903 implemented xs for axis=1 with a level specified
  • Loading branch information
jreback committed Mar 22, 2013
2 parents ff1ca84 + 6b5b6b9 commit ad6661b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ pandas 0.11.0
- Fix NameError issue on RESO_US (GH2787_)
- Allow selection in an *unordered* timeseries to work similary
to an *ordered* timeseries (GH2437_).
- Fix implemented ``.xs`` when called with ``axes=1`` and a level parameter (GH2903_)

.. _GH2758: https://github.com/pydata/pandas/issues/2758
.. _GH2758: https://github.com/pydata/pandas/issues/2758
.. _GH2809: https://github.com/pydata/pandas/issues/2809
.. _GH2810: https://github.com/pydata/pandas/issues/2810
.. _GH2837: https://github.com/pydata/pandas/issues/2837
Expand Down Expand Up @@ -257,6 +258,7 @@ pandas 0.11.0
.. _GH2850: https://github.com/pydata/pandas/issues/2850
.. _GH2898: https://github.com/pydata/pandas/issues/2898
.. _GH2892: https://github.com/pydata/pandas/issues/2892
.. _GH2903: https://github.com/pydata/pandas/issues/2903
.. _GH2909: https://github.com/pydata/pandas/issues/2909
.. _GH2922: https://github.com/pydata/pandas/issues/2922
.. _GH2929: https://github.com/pydata/pandas/issues/2929
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,11 @@ def xs(self, key, axis=0, level=None, copy=True):
if labels.levels[lev_num].inferred_type == 'integer':
indexer = self.index[loc]

result = self.ix[indexer]
# select on the correct axis
if axis == 1:
result = self.ix[:, indexer]
else:
result = self.ix[indexer]
setattr(result, result._get_axis_name(axis), new_ax)
return result

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ def test_ix_general(self):
df.sortlevel(inplace=True)
df.ix[(4.0,2012)]

def test_xs_multiindex(self):

# GH2903
columns = MultiIndex.from_tuples([('a', 'foo'), ('a', 'bar'), ('b', 'hello'), ('b', 'world')], names=['lvl0', 'lvl1'])
df = DataFrame(np.random.randn(4, 4), columns=columns)
df.sortlevel(axis=1,inplace=True)
result = df.xs('a', level='lvl0', axis=1)
expected = df.iloc[:,0:2].loc[:,'a']
assert_frame_equal(result,expected)

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down

0 comments on commit ad6661b

Please sign in to comment.