Skip to content

Commit

Permalink
Fixed an issue where it was not possible to subclass Panel because
Browse files Browse the repository at this point in the history
various methods were calling the Panel constructor instead of the
_constructor method. Also from_dict was returning Panel objects
instead of __class__ objects.
  • Loading branch information
Josh authored and Josh committed Mar 9, 2012
1 parent 5353a7c commit 5c8347b
Showing 1 changed file with 13 additions and 36 deletions.
49 changes: 13 additions & 36 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
_try_sort, _default_index, _infer_dtype)
from pandas.core.index import (Factor, Index, MultiIndex, _ensure_index,
_get_combined_index, NULL_INDEX)
from pandas.core.indexing import _NDFrameIndexer, _maybe_droplevels
from pandas.core.indexing import _NDFrameIndexer
from pandas.core.internals import BlockManager, make_block, form_blocks
from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame
Expand Down Expand Up @@ -320,30 +320,7 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None):
data, index, columns = _homogenize_dict(data, intersect=intersect,
dtype=dtype)
items = Index(sorted(data.keys()))
return Panel(data, items, index, columns)

def __getitem__(self, key):
if isinstance(self.items, MultiIndex):
return self._getitem_multilevel(key)
return super(Panel, self).__getitem__(key)

def _getitem_multilevel(self, key):
loc = self.items.get_loc(key)
if isinstance(loc, (slice, np.ndarray)):
new_index = self.items[loc]
result_index = _maybe_droplevels(new_index, key)
if self._is_mixed_type:
result = self.reindex(items=new_index)
result.index = result_index
else:
new_values = self.values[loc, :, :]
result = Panel(new_values,
items=self.items[loc],
major_axis=self.major_axis,
minor_axis=self.minor_axis)
return result
else:
return self._get_item_cache(key)
return __class__(data, items, index, columns)

def _init_matrix(self, data, axes, dtype=None, copy=False):
values = _prep_ndarray(data, copy=copy)
Expand Down Expand Up @@ -438,14 +415,14 @@ def _get_plane_axes(self, axis):
columns = self.minor_axis

return index, columns

@property
def _constructor(self):
return Panel
return self.__class__

# Fancy indexing
_ix = None

@property
def ix(self):
if self._ix is None:
Expand Down Expand Up @@ -723,7 +700,7 @@ def _combine(self, other, func, axis=0):
return self._combine_frame(other, func, axis=axis)
elif np.isscalar(other):
new_values = func(self.values, other)
return Panel(new_values, self.items, self.major_axis,
return self._constructor(new_values, self.items, self.major_axis,
self.minor_axis)

def __neg__(self):
Expand All @@ -744,7 +721,7 @@ def _combine_frame(self, other, func, axis=0):
new_values = func(self.values.swapaxes(0, 2), other.values)
new_values = new_values.swapaxes(0, 2)

return Panel(new_values, self.items, self.major_axis,
return self._constructor(new_values, self.items, self.major_axis,
self.minor_axis)

def _combine_panel(self, other, func):
Expand All @@ -758,7 +735,7 @@ def _combine_panel(self, other, func):

result_values = func(this.values, other.values)

return Panel(result_values, items, major, minor)
return self._constructor(result_values, items, major, minor)

def fillna(self, value=None, method='pad'):
"""
Expand Down Expand Up @@ -790,10 +767,10 @@ def fillna(self, value=None, method='pad'):
for col, s in self.iterkv():
result[col] = s.fillna(method=method, value=value)

return Panel.from_dict(result)
return self._constructor.from_dict(result)
else:
new_data = self._data.fillna(value)
return Panel(new_data)
return self._constructor(new_data)

add = _panel_arith_method(operator.add, 'add')
subtract = sub = _panel_arith_method(operator.sub, 'subtract')
Expand Down Expand Up @@ -882,7 +859,7 @@ def groupby(self, function, axis='major'):
"""
from pandas.core.groupby import PanelGroupBy
axis = self._get_axis_number(axis)
return PanelGroupBy(self, function, axis=axis)
return self._constructorGroupBy(self, function, axis=axis)

def swapaxes(self, axis1='major', axis2='minor'):
"""
Expand All @@ -904,7 +881,7 @@ def swapaxes(self, axis1='major', axis2='minor'):
for k in range(3))
new_values = self.values.swapaxes(i, j).copy()

return Panel(new_values, *new_axes)
return self._constructor(new_values, *new_axes)

def to_frame(self, filter_observations=True):
"""
Expand Down Expand Up @@ -1105,7 +1082,7 @@ def shift(self, lags, axis='major'):
else:
raise ValueError('Invalid axis')

return Panel(values, items=items, major_axis=major_axis,
return self._constructor(values, items=items, major_axis=major_axis,
minor_axis=minor_axis)

def truncate(self, before=None, after=None, axis='major'):
Expand Down

0 comments on commit 5c8347b

Please sign in to comment.