Skip to content

Commit

Permalink
ENH: add abs method to objects, implement array interface on Panel, S…
Browse files Browse the repository at this point in the history
…parsePanel #740
  • Loading branch information
wesm committed Feb 4, 2012
1 parent 443dcc5 commit 3583373
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pandas 0.7.0
method to ``Series`` as well (GH #699)
- Add ``isin`` method to Index objects, works just like ``Series.isin`` (GH
#657)
- Implement array interface on Panel so that ufuncs work (re: #740)

**API Changes**

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ def _get_axis(self, axis):
name = self._get_axis_name(axis)
return getattr(self, name)

def abs(self):
"""
Return an object with absolute value taken. Only applicable to objects
that are all numeric
Returns
-------
abs: type of caller
"""
return np.abs(self)

def get(self, key, default=None):
"""
Get item from object for given key (DataFrame column, Panel slice,
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ def _init_matrix(self, data, axes, dtype=None, copy=False):
block = make_block(values, items, items)
return BlockManager([block], fixed_axes)


#----------------------------------------------------------------------
# Array interface

def __array__(self, dtype=None):
return self.values

def __array_wrap__(self, result):
return self._constructor(result, items=self.items,
major_axis=self.major_axis,
minor_axis=self.minor_axis, copy=False)

#----------------------------------------------------------------------
# Magic methods

def __repr__(self):
class_name = str(self.__class__)

Expand Down
17 changes: 17 additions & 0 deletions pandas/sparse/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ class SparsePanel(Panel):

def __init__(self, frames, items=None, major_axis=None, minor_axis=None,
default_fill_value=np.nan, default_kind='block'):
if isinstance(frames, np.ndarray):
new_frames = {}
for item, vals in zip(items, frames):
new_frames[item] = \
SparseDataFrame(vals, index=major_axis,
columns=minor_axis,
default_fill_value=default_fill_value,
default_kind=default_kind)
frames = new_frames

assert(isinstance(frames, dict))

self.default_fill_value = fill_value = default_fill_value
Expand Down Expand Up @@ -92,6 +102,13 @@ def _consolidate_inplace(self): # pragma: no cover
# do nothing when DataFrame calls this method
pass

def __array_wrap__(self, result):
return SparsePanel(result, items=self.items,
major_axis=self.major_axis,
minor_axis=self.minor_axis,
default_kind=self.default_kind,
default_fill_value=self.default_fill_value)

@classmethod
def from_dict(cls, data):
"""
Expand Down
14 changes: 14 additions & 0 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ def __getitem__(self, key):
new_index = Index(self.index.view(ndarray)[key])
return self._constructor(dataSlice, index=new_index, name=self.name)

def abs(self):
"""
Return an object with absolute value taken. Only applicable to objects
that are all numeric
Returns
-------
abs: type of caller
"""
res_sp_values = np.abs(self.sp_values)
return SparseSeries(res_sp_values, index=self.index,
sparse_index=self.sp_index,
fill_value=self.fill_value)

def get(self, label, default=None):
"""
Returns value occupying requested label, default to specified
Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ def test_get_value(self):
expected = self.panel[item][mnr][mjr]
assert_almost_equal(result, expected)

def test_abs(self):
result = self.panel.abs()
expected = np.abs(self.panel)
self.assert_panel_equal(result, expected)

df = self.panel['ItemA']
result = df.abs()
expected = np.abs(df)
assert_frame_equal(result, expected)

s = df['A']
result = s.abs()
expected = np.abs(s)
assert_series_equal(result, expected)

class CheckIndexing(object):


Expand Down Expand Up @@ -966,7 +981,7 @@ def test_group_agg(self):
self.assertRaises(Exception, group_agg, values, bounds, f2)

def test_from_frame_level1_unsorted(self):
tuples = [('MSFT', 3), ('MSFT', 2), ('AAPL', 2),
tuples = [('MSFT', 3), ('MSFT', 2), ('AAPL', 2),
('AAPL', 1), ('MSFT', 1)]
midx = MultiIndex.from_tuples(tuples)
df = DataFrame(np.random.rand(5,4), index=midx)
Expand Down

0 comments on commit 3583373

Please sign in to comment.