From 668e94f46ddd8f5ec73f3a0b8c5cd5751da98643 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 19 Oct 2019 09:21:59 -0700 Subject: [PATCH 1/3] BUG: fix AttributeError raised in libreduction --- pandas/_libs/reduction.pyx | 16 ++++++++++------ pandas/core/groupby/generic.py | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index 0eac0e94f0beb..5c57d35655bc2 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -203,7 +203,8 @@ cdef class SeriesBinGrouper: self.f = f values = series.values - if not values.flags.c_contiguous: + if util.is_array(values) and not values.flags.c_contiguous: + # e.g. Categorical has no `flags` attribute values = values.copy('C') self.arr = values self.typ = series._constructor @@ -230,7 +231,8 @@ cdef class SeriesBinGrouper: values = dummy.values if values.dtype != self.arr.dtype: raise ValueError('Dummy array must be same dtype') - if not values.flags.contiguous: + if util.is_array(values) and not values.flags.contiguous: + # e.g. Categorical has no `flags` attribute values = values.copy() index = dummy.index.values if not index.flags.contiguous: @@ -356,7 +358,8 @@ cdef class SeriesGrouper: if (dummy.dtype != self.arr.dtype and values.dtype != self.arr.dtype): raise ValueError('Dummy array must be same dtype') - if not values.flags.contiguous: + if util.is_array(values) and not values.flags.contiguous: + # e.g. Categorical has no `flags` attribute values = values.copy() index = dummy.index.values if not index.flags.contiguous: @@ -467,12 +470,13 @@ cdef class Slider: char *orig_data def __init__(self, object values, object buf): - assert(values.ndim == 1) + assert (values.ndim == 1) - if not values.flags.contiguous: + if util.is_array(values) and not values.flags.contiguous: + # e.g. Categorical has no `flags` attribute values = values.copy() - assert(values.dtype == buf.dtype) + assert (values.dtype == buf.dtype) self.values = values self.buf = buf self.stride = values.strides[0] diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 8191c3519a36a..ac7347300cc9c 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -261,7 +261,7 @@ def aggregate(self, func=None, *args, **kwargs): try: return self._python_agg_general(func, *args, **kwargs) - except AssertionError: + except (AssertionError, AttributeError): raise except Exception: result = self._aggregate_named(func, *args, **kwargs) From d24cddbaf9a494f0cdbbeb5f3cb5132ed96c683b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 19 Oct 2019 11:43:35 -0700 Subject: [PATCH 2/3] missed one --- pandas/_libs/reduction.pyx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index 5c57d35655bc2..7ed131e1c7608 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -334,7 +334,8 @@ cdef class SeriesGrouper: self.f = f values = series.values - if not values.flags.c_contiguous: + if util.is_array(values) and not values.flags.c_contiguous: + # e.g. Categorical has no `flags` attribute values = values.copy('C') self.arr = values self.typ = series._constructor From 4f1705d5515a65aa93981378e8a4da1ed3490177 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 19 Oct 2019 13:57:19 -0700 Subject: [PATCH 3/3] revert re-raising AttributeError --- pandas/core/groupby/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index b63350a4e6c19..a78857423e7e0 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -261,7 +261,7 @@ def aggregate(self, func=None, *args, **kwargs): try: return self._python_agg_general(func, *args, **kwargs) - except (AssertionError, AttributeError): + except AssertionError: raise except Exception: result = self._aggregate_named(func, *args, **kwargs)