Skip to content

Commit

Permalink
BUG: fixup Categorical.describe to work with 'fixed' count
Browse files Browse the repository at this point in the history
CLN: remove __inv__, __neg__ from series and use generic version
CLN: remove __wrap_array__ from generic (replace with __array_wrap__)
  • Loading branch information
jreback committed Apr 29, 2014
1 parent 6fa398e commit f520e8d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
10 changes: 6 additions & 4 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ def describe(self):
"""
# Hack?
from pandas.core.frame import DataFrame
grouped = DataFrame(self.labels).groupby(0)
counts = grouped.count().values.squeeze()
counts = DataFrame({
'labels' : self.labels,
'values' : self.labels }
).groupby('labels').count().squeeze().values
freqs = counts / float(counts.sum())
return DataFrame.from_dict({
return DataFrame({
'counts': counts,
'freqs': freqs,
'levels': self.levels
}).set_index('levels')
}).set_index('levels')
22 changes: 13 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,19 @@ def __neg__(self):
arr = operator.inv(values)
else:
arr = operator.neg(values)
return self._wrap_array(arr, self.axes, copy=False)
return self.__array_wrap__(arr)

def __invert__(self):
arr = operator.inv(_values_from_object(self))
return self._wrap_array(arr, self.axes, copy=False)
try:
arr = operator.inv(_values_from_object(self))
return self.__array_wrap__(arr)
except:

# inv fails with 0 len
if not np.prod(self.shape):
return self

raise

def equals(self, other):
"""
Expand Down Expand Up @@ -707,15 +715,11 @@ def __abs__(self):
#----------------------------------------------------------------------
# Array Interface

def _wrap_array(self, arr, axes, copy=False):
d = self._construct_axes_dict_from(self, axes, copy=copy)
return self._constructor(arr, **d).__finalize__(self)

def __array__(self, dtype=None):
return _values_from_object(self)

def __array_wrap__(self, result):
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
def __array_wrap__(self, result, copy=False):
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=copy)
return self._constructor(result, **d).__finalize__(self)

# ideally we would define this to avoid the getattr checks, but
Expand Down
17 changes: 2 additions & 15 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ def __array__(self, result=None):
""" the array interface, return my values """
return self.values

def __array_wrap__(self, result):
def __array_wrap__(self, result, copy=False):
"""
Gets called prior to a ufunc (and after)
"""
return self._constructor(result, index=self.index,
copy=False).__finalize__(self)
copy=copy).__finalize__(self)

def __contains__(self, key):
return key in self.index
Expand Down Expand Up @@ -959,19 +959,6 @@ def iteritems(self):
if compat.PY3: # pragma: no cover
items = iteritems

# inversion
def __neg__(self):
values = self.values
if values.dtype == np.bool_:
arr = operator.inv(values)
else:
arr = operator.neg(values)
return self._constructor(arr, self.index).__finalize__(self)

def __invert__(self):
arr = operator.inv(self.values)
return self._constructor(arr, self.index).__finalize__(self)

#----------------------------------------------------------------------
# unbox reductions

Expand Down

0 comments on commit f520e8d

Please sign in to comment.