Skip to content

Commit deb8549

Browse files
committed
BUG: fix GH #98
1 parent 163806f commit deb8549

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

pandas/core/internals.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from operator import attrgetter
21
import itertools
32

43
from numpy import nan
54
import numpy as np
65

7-
from pandas.core.index import Index, NULL_INDEX
8-
from pandas.core.common import _ensure_index, _try_sort
6+
from pandas.core.index import Index
7+
from pandas.core.common import _ensure_index
98
import pandas.core.common as common
109
import pandas._tseries as _tseries
1110

@@ -375,7 +374,6 @@ def get_series_dict(self):
375374
def from_blocks(cls, blocks, index):
376375
# also checks for overlap
377376
items = _union_block_items(blocks)
378-
ndim = blocks[0].ndim
379377
return BlockManager(blocks, [items, index])
380378

381379
def __contains__(self, item):
@@ -429,8 +427,6 @@ def _interleave(self, items):
429427
return result
430428

431429
def xs(self, key, axis=1, copy=True):
432-
from pandas.core.series import Series
433-
434430
assert(axis >= 1)
435431

436432
i = self.axes[axis].get_loc(key)
@@ -441,15 +437,15 @@ def xs(self, key, axis=1, copy=True):
441437
new_axes = list(self.axes)
442438
new_axes.pop(axis)
443439

440+
new_blocks = []
444441
if len(self.blocks) > 1:
445442
if not copy:
446443
raise Exception('cannot get view of mixed-type or '
447444
'non-consolidated DataFrame')
448-
new_blocks = []
449445
for blk in self.blocks:
450446
newb = make_block(blk.values[slicer], blk.items, blk.ref_items)
451447
new_blocks.append(newb)
452-
else:
448+
elif len(self.blocks) == 1:
453449
vals = self.blocks[0].values[slicer]
454450
if copy:
455451
vals = vals.copy()

pandas/tests/test_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,7 @@ def test_xs(self):
15481548
self.assertRaises(Exception, self.tsframe.xs,
15491549
self.tsframe.index[0] - datetools.bday)
15501550

1551+
def test_xs_corner(self):
15511552
# pathological mixed-type reordering case
15521553
df = DataFrame(index=[0])
15531554
df['A'] = 1.
@@ -1559,6 +1560,12 @@ def test_xs(self):
15591560
xs = df.xs(0)
15601561
assert_almost_equal(xs, [1., 'foo', 2., 'bar', 3.])
15611562

1563+
# no columns but index
1564+
df = DataFrame(index=['a', 'b', 'c'])
1565+
result = df.xs('a')
1566+
expected = Series([])
1567+
assert_series_equal(result, expected)
1568+
15621569
def test_pivot(self):
15631570
data = {
15641571
'index' : ['A', 'B', 'C', 'C', 'B', 'A'],

0 commit comments

Comments
 (0)