Skip to content

Series comparison to NumPy scalar fails #3392

Closed

Description

@philippjfr

Comparing a dask Series with a NumPy scalar or 0D array fails in certain cases. Here is a minimal reproducible example:

import dask.dataframe as dd
import pandas as pd

np.float64(5.2) >= dd.from_array(np.arange(10))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-d905075c0565> in <module>()
      1 
----> 2 np.float64(10) >= dd.from_array(np.arange(10))

~/miniconda/envs/anacondaviz/lib/python3.6/site-packages/dask/dataframe/core.py in __array_wrap__(self, array, context)
   1704     def __array_wrap__(self, array, context=None):
   1705         if isinstance(context, tuple) and len(context) > 0:
-> 1706             index = context[1][0].index
   1707 
   1708         return pd.Series(array, index=index, name=self.name)

AttributeError: 'numpy.ndarray' object has no attribute 'index'

I can reproduce this using all releases in the dask 0.17.x series.

Activity

  1. mrocklin commented on Apr 12, 2018

    @mrocklin
    Member
  2. martindurant commented on Apr 12, 2018

    @martindurant
    Member

    I also get an error for the same code, but not the same error. Running from master.

    In [20]: np.float64(5.2) >= dd.from_array(np.arange(10))
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-20-f79e026dfad4> in <module>()
    ----> 1 np.float64(5.2) >= dd.from_array(np.arange(10))
    
    TypeError: operand type(s) all returned NotImplemented from __array_ufunc__(<ufunc 'greater_equal'>, '__call__', array(5.2), Dask Series Structure:
    npartitions=1
    0    int64
    9      ...
    dtype: int64
    Dask Name: from_array, 1 tasks): 'ndarray', 'Series'
    
    In [22]: np.__version__
    Out[22]: '1.13.3'
    
    In [23]: dask.__version__
    Out[23]: '0.17.2+49.g7cd5143'
    
  3. jrbourbeau commented on Apr 23, 2018

    @jrbourbeau
    Member

    I'm getting the same error as @martindurant.

    It looks like np.float64(5.2) is getting converted to a 0-dimensional array (i.e. np.array(5.2)) when inputted into __array_ufunc__. Because of this, the isinstance check for Number fails

    dask/dask/dataframe/core.py

    Lines 347 to 349 in b64e694

    if not isinstance(x, (Number, Scalar, _Frame, Array,
    pd.DataFrame, pd.Series, pd.Index)):
    return NotImplemented

    and an error is thrown.

    I can look into adding an extra check in __array_ufunc__ for 0-dimensional np.ndarrays

  4. mrocklin commented on Apr 24, 2018

    @mrocklin
    Member
  5. jrbourbeau commented on Apr 24, 2018

    @jrbourbeau
    Member

    I can take this one up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions