From 6b5b6b9e4f80cb3b2bd8bcd78c1856913e8d5f60 Mon Sep 17 00:00:00 2001 From: jreback Date: Thu, 21 Mar 2013 12:25:02 -0400 Subject: [PATCH] BUG: GH2903, implemented xs for axis=1 with a level specified --- RELEASE.rst | 4 +++- pandas/core/frame.py | 6 +++++- pandas/tests/test_indexing.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/RELEASE.rst b/RELEASE.rst index 51ed383648929..8fe7a33ea0c87 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -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 @@ -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 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b47b77fdaeb6c..afb698221c48b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 207eb96a9804e..75aa208e0c6b2 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -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'],