Skip to content

Commit 189d04c

Browse files
committed
BUG: raise exception when view not possible of multiindex'd dataframe. close #2117
1 parent 7d80bee commit 189d04c

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pandas 0.9.1
6868
- Work around length-0 boolean indexing NumPy bug (#2096)
6969
- Fix partial integer indexing bug in DataFrame.xs (#2107)
7070
- Fix variety of cut/qcut string-bin formatting bugs (#1978, #1979)
71+
- Raise Exception when xs view not possible of MultiIndex'd DataFrame (#2117)
7172
7273
pandas 0.9.0
7374
============

pandas/core/frame.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,9 @@ def xs(self, key, axis=0, level=None, copy=True):
20182018
if level is not None:
20192019
loc, new_ax = labels.get_loc_level(key, level=level)
20202020

2021+
if not copy and not isinstance(loc, slice):
2022+
raise ValueError('Cannot retrieve view (copy=False)')
2023+
20212024
# level = 0
20222025
if not isinstance(loc, slice):
20232026
indexer = [slice(None, None)] * 2

pandas/tests/test_multilevel.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,14 @@ def test_indexing_over_hashtable_size_cutoff(self):
15551555

15561556
lib._SIZE_CUTOFF = old_cutoff
15571557

1558+
def test_xs_mixed_no_copy(self):
1559+
index = MultiIndex.from_arrays([['a','a', 'b', 'b'], [1,2,1,2]],
1560+
names=['first', 'second'])
1561+
data = DataFrame(np.random.rand(len(index)), index=index,
1562+
columns=['A'])
1563+
1564+
self.assertRaises(Exception, data.xs, 2, level=1, copy=False)
1565+
15581566
if __name__ == '__main__':
15591567

15601568
# unittest.main()

0 commit comments

Comments
 (0)