Skip to content

TypeError when slicing non-numeric array #4911

Closed

Description

@TomAugspurger
In [1]: import dask.array as da

In [2]: import numpy as np

In [3]: a = np.array([['a', 'b'], ['c', 'd']])

In [4]: b = da.from_array(a, 2)

In [5]: b[:, 0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-687028c8d699> in <module>
----> 1 b[:, 0]

~/sandbox/dask/dask/array/core.py in __getitem__(self, index)
   1302             # If meta still has more dimensions than actual data
   1303             if meta.ndim > len(chunks):
-> 1304                 meta = np.sum(meta, axis=tuple([i for i in range(meta.ndim - len(chunks))]))
   1305
   1306             # Ensure all dimensions are 0

~/Envs/dask-dev/lib/python3.7/site-packages/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims, initial)
   2074
   2075     return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
-> 2076                           initial=initial)
   2077
   2078

~/Envs/dask-dev/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     84                 return reduction(axis=axis, out=out, **passkwargs)
     85
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     87
     88

TypeError: cannot perform reduce with flexible type

Activity

  1. TomAugspurger commented on Jun 10, 2019

    @TomAugspurger
    MemberAuthor

    Bisected to #4543 (cc @pentschev if you have a chance to look)

  2. pentschev commented on Jun 10, 2019

    @pentschev
    Member

    When I saw the title, I immediately thought it would be there. Thanks for the report @TomAugspurger, will take a look.

  3. pentschev commented on Jun 10, 2019

    @pentschev
    Member

    Oh no, I was taking a quick look now, and you have fallen on the one case that we sort of decided to ignore, that of flexible types. We did this mainly for two reason: we didn't think anyone used those and we didn't really know how we could handle _meta for those.

    This case in particular can be easily fixed (I'll send a PR soon), but I assume you're going to do something more complex than that to your data, which may then not be easily fixable.

    Given this is the first use case we've encountered since we decided to just ignore it, would you mind elaborating on how important is the use of flexible types (for completeness, those are basically strings and bytes) for you?

    cc @jakirkham @mrocklin

  4. TomAugspurger commented on Jun 10, 2019

    @TomAugspurger
    MemberAuthor

    The context is dask_ml's OneHotEncoder, which transforms an array like

    X = da.from_array(np.array([['A'], ['B'], ['A'], ['C']]), chunks=2)
    

    to

    In [5]: result.compute().todense()
    Out[5]:
    matrix([[1., 0., 0.],
            [0., 1., 0.],
            [1., 0., 0.],
            [0., 0., 1.]])

    see https://ml.dask.org/preprocessing.html#dask-ml-s-sparse-support

    As for how much weight to put on that use case... I'm not really sure. I'd say relatively low. In my experience I'm rarely working with just text data, and so I'm using a dataframe rather than an array (which takes a different code path in dask-ml).

  5. pentschev commented on Jun 10, 2019

    @pentschev
    Member

    Great, thanks for the details @TomAugspurger, I'll send a PR with the fix, it isn't anything difficult to get working. So maybe this is one of the relatively rare cases. :)

  6. jakirkham commented on Jun 10, 2019

    @jakirkham
    Member

    @pentschev, do you know how text data was handled before _meta? I'm curious if with text data we can use whatever old code path was there. Maybe it didn't track things like '<U1' vs. '<U2', but it did track a type of np.unicode_. Apologies if this question is naive, you know more about how _meta handles these things and what code it replaced than I. 🙂

  7. pentschev commented on Jun 10, 2019

    @pentschev
    Member

    Previously, it tracked things like '<U1' via dtype, so that was all of the array's knowledge of the flexible type. Eventually all the computations would be done and the dtype updated, but now we don't have a separate dtype anymore, it's inferred from _meta and this is why we have no actual length anymore. I'm not sure whether this answers your question entirely, let me know if it doesn't.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions