From c24bf0289c53cef03ec13f26d57754f97f5c07f7 Mon Sep 17 00:00:00 2001 From: Pietro Belotti Date: Wed, 5 Sep 2018 15:45:42 +0100 Subject: [PATCH 1/2] avoid ValueError when overriding eq Overloading "eq" in NumPy for use by other modules (e.g. creating equations) requires replacing a NPY_OBJECT,NPY_OBJECT,NPY_BYTE operation with one that returns a NPY_OBJECT. This triggers a ValueError in _maybe_get_bool_indexer where a NumPy array of bytes is cdef'd but then assigned an ndarray of objects. Added an ndarray of objects to be assigned the same. --- pandas/_libs/index.pyx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 9c906a00bd4fe..6254f8e5f177a 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -154,15 +154,26 @@ cdef class IndexEngine: cdef _maybe_get_bool_indexer(self, object val): cdef: ndarray[uint8_t, ndim=1, cast=True] indexer + ndarray[object, ndim=1, cast=True] indexerObj ndarray[intp_t, ndim=1] found int count - indexer = self._get_index_values() == val - found = np.where(indexer)[0] - count = len(found) + try: + indexer = self._get_index_values() == val + found = np.where(indexer)[0] + count = len(found) + + if count > 1: + return indexer + + except ValueError: + indexerObj = self._get_index_values() == val + found = np.where(indexerObj)[0] + count = len(found) + + if count > 1: + return indexerObj - if count > 1: - return indexer if count == 1: return int(found[0]) From 8eb427cf37d513ad67eaf0a42e0aff71c8040478 Mon Sep 17 00:00:00 2001 From: Pietro Belotti Date: Fri, 14 Sep 2018 18:46:52 +0100 Subject: [PATCH 2/2] replace try/except with casting indexer to a np.array; fixes problem --- pandas/_libs/index.pyx | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 6254f8e5f177a..e3e48327d14b1 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -154,25 +154,15 @@ cdef class IndexEngine: cdef _maybe_get_bool_indexer(self, object val): cdef: ndarray[uint8_t, ndim=1, cast=True] indexer - ndarray[object, ndim=1, cast=True] indexerObj ndarray[intp_t, ndim=1] found int count - try: - indexer = self._get_index_values() == val - found = np.where(indexer)[0] - count = len(found) - - if count > 1: - return indexer - - except ValueError: - indexerObj = self._get_index_values() == val - found = np.where(indexerObj)[0] - count = len(found) + indexer = np.array(self._get_index_values() == val, dtype = bool, copy = False) + found = np.where(indexer)[0] + count = len(found) - if count > 1: - return indexerObj + if count > 1: + return indexer if count == 1: return int(found[0])