Skip to content

Commit 8091ded

Browse files
committed
generic slice with BlockManager for panel
1 parent 482018f commit 8091ded

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

pandas/core/frame.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ def xs(self, key, copy=True):
872872
raise Exception('No cross-section for %s' % key)
873873

874874
self._consolidate_inplace()
875-
loc = self.index.get_loc(key)
876-
values = self._data.xs(loc, axis=1, copy=copy)
875+
values = self._data.xs(key, axis=1, copy=copy)
877876
return Series(values, index=self.columns)
878877

879878
#----------------------------------------------------------------------

pandas/core/internals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,10 @@ def _interleave(self, items):
414414
assert(itemmask.all())
415415
return result
416416

417-
def xs(self, i, axis=1, copy=True):
417+
def xs(self, key, axis=1, copy=True):
418418
from pandas.core.series import Series
419419

420+
i = self.axes[axis].get_loc(key)
420421
slicer = [slice(None, None) for _ in range(self.ndim)]
421422
slicer[axis] = i
422423
slicer = tuple(slicer)

pandas/core/panel.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def fillna(self, value=None, method='pad'):
588588
divide = _wide_arith_method(operator.div, 'divide')
589589
multiply = _wide_arith_method(operator.mul, 'multiply')
590590

591-
def major_xs(self, key):
591+
def major_xs(self, key, copy=True):
592592
"""
593593
Return slice of panel along major axis
594594
@@ -602,11 +602,10 @@ def major_xs(self, key):
602602
y : DataFrame
603603
index -> minor axis, columns -> items
604604
"""
605-
loc = self.major_axis.get_loc(key)
606-
mat = np.array(self.values[:, loc, :].T)
607-
return DataFrame(mat, index=self.minor_axis, columns=self.items)
605+
values = self._data.xs(key, axis=1, copy=copy).T
606+
return DataFrame(values, index=self.minor_axis, columns=self.items)
608607

609-
def minor_xs(self, key):
608+
def minor_xs(self, key, copy=False):
610609
"""
611610
Return slice of panel along minor axis
612611
@@ -620,9 +619,8 @@ def minor_xs(self, key):
620619
y : DataFrame
621620
index -> major axis, columns -> items
622621
"""
623-
loc = self.minor_axis.get_loc(key)
624-
mat = np.array(self.values[:, :, loc].T)
625-
return DataFrame(mat, index=self.major_axis, columns=self.items)
622+
values = self._data.xs(key, axis=2, copy=copy).T
623+
return DataFrame(values, index=self.major_axis, columns=self.items)
626624

627625
def getMinorXS(self, key): # pragma: no cover
628626
warnings.warn("getMinorXS has been replaced by the minor_xs function "

0 commit comments

Comments
 (0)