Skip to content

Commit ad6661b

Browse files
committed
Merge pull request #3125 from jreback/GH2903
BUG: GH2903 implemented xs for axis=1 with a level specified
2 parents ff1ca84 + 6b5b6b9 commit ad6661b

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

RELEASE.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ pandas 0.11.0
219219
- Fix NameError issue on RESO_US (GH2787_)
220220
- Allow selection in an *unordered* timeseries to work similary
221221
to an *ordered* timeseries (GH2437_).
222+
- Fix implemented ``.xs`` when called with ``axes=1`` and a level parameter (GH2903_)
222223

223-
.. _GH2758: https://github.com/pydata/pandas/issues/2758
224+
.. _GH2758: https://github.com/pydata/pandas/issues/2758
224225
.. _GH2809: https://github.com/pydata/pandas/issues/2809
225226
.. _GH2810: https://github.com/pydata/pandas/issues/2810
226227
.. _GH2837: https://github.com/pydata/pandas/issues/2837
@@ -257,6 +258,7 @@ pandas 0.11.0
257258
.. _GH2850: https://github.com/pydata/pandas/issues/2850
258259
.. _GH2898: https://github.com/pydata/pandas/issues/2898
259260
.. _GH2892: https://github.com/pydata/pandas/issues/2892
261+
.. _GH2903: https://github.com/pydata/pandas/issues/2903
260262
.. _GH2909: https://github.com/pydata/pandas/issues/2909
261263
.. _GH2922: https://github.com/pydata/pandas/issues/2922
262264
.. _GH2929: https://github.com/pydata/pandas/issues/2929

pandas/core/frame.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,11 @@ def xs(self, key, axis=0, level=None, copy=True):
22112211
if labels.levels[lev_num].inferred_type == 'integer':
22122212
indexer = self.index[loc]
22132213

2214-
result = self.ix[indexer]
2214+
# select on the correct axis
2215+
if axis == 1:
2216+
result = self.ix[:, indexer]
2217+
else:
2218+
result = self.ix[indexer]
22152219
setattr(result, result._get_axis_name(axis), new_ax)
22162220
return result
22172221

pandas/tests/test_indexing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,16 @@ def test_ix_general(self):
714714
df.sortlevel(inplace=True)
715715
df.ix[(4.0,2012)]
716716

717+
def test_xs_multiindex(self):
718+
719+
# GH2903
720+
columns = MultiIndex.from_tuples([('a', 'foo'), ('a', 'bar'), ('b', 'hello'), ('b', 'world')], names=['lvl0', 'lvl1'])
721+
df = DataFrame(np.random.randn(4, 4), columns=columns)
722+
df.sortlevel(axis=1,inplace=True)
723+
result = df.xs('a', level='lvl0', axis=1)
724+
expected = df.iloc[:,0:2].loc[:,'a']
725+
assert_frame_equal(result,expected)
726+
717727
if __name__ == '__main__':
718728
import nose
719729
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)