Skip to content

Commit e8a528d

Browse files
committed
BUG: (GH4016) fix panel slicing issue that was returning an object that should not
have be a reduction in ndim
1 parent 3841ae6 commit e8a528d

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pandas 0.13
129129
(:issue:`4486`)
130130
- Fixed an issue where cumsum and cumprod didn't work with bool dtypes
131131
(:issue:`4170`, :issue:`4440`)
132+
- Fixed Panel slicing issued in ``xs`` that was returning an incorrect dimmed object
133+
(:issue:`4016`)
132134

133135
pandas 0.12
134136
===========

pandas/core/panel.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ def xs(self, key, axis=1, copy=True):
10481048
self._consolidate_inplace()
10491049
axis_number = self._get_axis_number(axis)
10501050
new_data = self._data.xs(key, axis=axis_number, copy=copy)
1051-
return self._constructor_sliced(new_data)
1051+
return self._construct_return_type(new_data)
10521052

10531053
_xs = xs
10541054

@@ -1263,24 +1263,33 @@ def _reduce(self, op, axis=0, skipna=True):
12631263
if result.ndim == 2 and axis_name != self._info_axis:
12641264
result = result.T
12651265

1266-
return self._constructor_sliced(result,
1266+
return self._construct_return_type(result, axes)
1267+
1268+
def _construct_return_type(self, result, axes=None, **kwargs):
1269+
""" return the type for the ndim of the result """
1270+
ndim = result.ndim
1271+
if self.ndim == ndim:
1272+
""" return the construction dictionary for these axes """
1273+
if axes is None:
1274+
return self._constructor(result)
1275+
return self._constructor(result, **self._construct_axes_dict())
1276+
1277+
elif self.ndim == ndim + 1:
1278+
if axes is None:
1279+
return self._constructor_sliced(result)
1280+
return self._constructor_sliced(result,
12671281
**self._extract_axes_for_slice(self, axes))
12681282

1283+
raise PandasError("invalid _construct_return_type [self->%s] [result->%s]" %
1284+
(self.ndim, result.ndim))
1285+
12691286
def _wrap_result(self, result, axis):
12701287
axis = self._get_axis_name(axis)
12711288
axes = self._get_plane_axes(axis)
12721289
if result.ndim == 2 and axis != self._info_axis:
12731290
result = result.T
12741291

1275-
# do we have reduced dimensionalility?
1276-
if self.ndim == result.ndim:
1277-
return self._constructor(result, **self._construct_axes_dict())
1278-
elif self.ndim == result.ndim + 1:
1279-
return self._constructor_sliced(result,
1280-
**self._extract_axes_for_slice(self, axes))
1281-
1282-
raise PandasError("invalid _wrap_result [self->%s] [result->%s]" %
1283-
(self.ndim, result.ndim))
1292+
return self._construct_return_type(result, axes)
12841293

12851294
def count(self, axis='major'):
12861295
"""

pandas/tests/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,16 @@ def test_iloc_panel_issue(self):
873873
self.assert_(p.iloc[1, :3, 1].shape == (3,))
874874
self.assert_(p.iloc[:3, 1, 1].shape == (3,))
875875

876+
def test_panel_getitem(self):
877+
# GH4016, date selection returns a frame when a partial string selection
878+
ind = date_range(start="2000", freq="D", periods=1000)
879+
df = DataFrame(np.random.randn(len(ind), 5), index=ind, columns=list('ABCDE'))
880+
panel = Panel({'frame_'+c:df for c in list('ABC')})
881+
882+
test2 = panel.ix[:, "2002":"2002-12-31"]
883+
test1 = panel.ix[:, "2002"]
884+
tm.assert_panel_equal(test1,test2)
885+
876886
def test_multi_assign(self):
877887

878888
# GH 3626, an assignement of a sub-df to a df

0 commit comments

Comments
 (0)