Skip to content

Commit f520e8d

Browse files
committed
BUG: fixup Categorical.describe to work with 'fixed' count
CLN: remove __inv__, __neg__ from series and use generic version CLN: remove __wrap_array__ from generic (replace with __array_wrap__)
1 parent 6fa398e commit f520e8d

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

pandas/core/categorical.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,13 @@ def describe(self):
226226
"""
227227
# Hack?
228228
from pandas.core.frame import DataFrame
229-
grouped = DataFrame(self.labels).groupby(0)
230-
counts = grouped.count().values.squeeze()
229+
counts = DataFrame({
230+
'labels' : self.labels,
231+
'values' : self.labels }
232+
).groupby('labels').count().squeeze().values
231233
freqs = counts / float(counts.sum())
232-
return DataFrame.from_dict({
234+
return DataFrame({
233235
'counts': counts,
234236
'freqs': freqs,
235237
'levels': self.levels
236-
}).set_index('levels')
238+
}).set_index('levels')

pandas/core/generic.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,19 @@ def __neg__(self):
611611
arr = operator.inv(values)
612612
else:
613613
arr = operator.neg(values)
614-
return self._wrap_array(arr, self.axes, copy=False)
614+
return self.__array_wrap__(arr)
615615

616616
def __invert__(self):
617-
arr = operator.inv(_values_from_object(self))
618-
return self._wrap_array(arr, self.axes, copy=False)
617+
try:
618+
arr = operator.inv(_values_from_object(self))
619+
return self.__array_wrap__(arr)
620+
except:
621+
622+
# inv fails with 0 len
623+
if not np.prod(self.shape):
624+
return self
625+
626+
raise
619627

620628
def equals(self, other):
621629
"""
@@ -707,15 +715,11 @@ def __abs__(self):
707715
#----------------------------------------------------------------------
708716
# Array Interface
709717

710-
def _wrap_array(self, arr, axes, copy=False):
711-
d = self._construct_axes_dict_from(self, axes, copy=copy)
712-
return self._constructor(arr, **d).__finalize__(self)
713-
714718
def __array__(self, dtype=None):
715719
return _values_from_object(self)
716720

717-
def __array_wrap__(self, result):
718-
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
721+
def __array_wrap__(self, result, copy=False):
722+
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=copy)
719723
return self._constructor(result, **d).__finalize__(self)
720724

721725
# ideally we would define this to avoid the getattr checks, but

pandas/core/series.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,12 @@ def __array__(self, result=None):
370370
""" the array interface, return my values """
371371
return self.values
372372

373-
def __array_wrap__(self, result):
373+
def __array_wrap__(self, result, copy=False):
374374
"""
375375
Gets called prior to a ufunc (and after)
376376
"""
377377
return self._constructor(result, index=self.index,
378-
copy=False).__finalize__(self)
378+
copy=copy).__finalize__(self)
379379

380380
def __contains__(self, key):
381381
return key in self.index
@@ -959,19 +959,6 @@ def iteritems(self):
959959
if compat.PY3: # pragma: no cover
960960
items = iteritems
961961

962-
# inversion
963-
def __neg__(self):
964-
values = self.values
965-
if values.dtype == np.bool_:
966-
arr = operator.inv(values)
967-
else:
968-
arr = operator.neg(values)
969-
return self._constructor(arr, self.index).__finalize__(self)
970-
971-
def __invert__(self):
972-
arr = operator.inv(self.values)
973-
return self._constructor(arr, self.index).__finalize__(self)
974-
975962
#----------------------------------------------------------------------
976963
# unbox reductions
977964

0 commit comments

Comments
 (0)