Skip to content

Fixes Series comparison to NumPy scalars - #3436

Merged
mrocklin merged 7 commits into
dask:masterfrom
jrbourbeau:series_comparison_numpy_scalar
May 1, 2018
Merged

mrocklin merged 7 commits into
dask:masterfrom
jrbourbeau:series_comparison_numpy_scalar

Conversation

@jrbourbeau

@jrbourbeau jrbourbeau commented Apr 24, 2018 •

Copy link
Copy Markdown
Member

Fixes #3392. This PR fixes Series comparison to NumPy scalars, which currently fail.

  • Tests added / passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

@jrbourbeau jrbourbeau changed the title [WIP] Adds check for 0-dimensional numpy array [WIP] Fixes Series comparison to NumPy scalars Apr 24, 2018
@mrocklin

Copy link
Copy Markdown
Member

Thank you for taking this on @jrbourbeau !

@philippjfr does this resolve your original issue?

@jrbourbeau

Copy link
Copy Markdown
Member Author

The original regression test I added was using the dask.array.greater_equal ufunc, which actually doesn't raise any errors. So I replaced the added test with the original code snippet given in issue #3392.

@mrocklin

Copy link
Copy Markdown
Member

It looks like there are some genuine testing errors in travis-ci. I suspect that this fix works for some versions, but not others.

@jrbourbeau

Copy link
Copy Markdown
Member Author

It looks like the NumPy 1.11.0 tests on Travis are failing because I've only modified __array_ufunc__ (which is new in NumPy version 1.13) and not __array_wrap__ (which is what's being used in the 1.11.0 tests).

@jrbourbeau

Copy link
Copy Markdown
Member Author

Looks like there was an issue during the installation process on some of the Travis tests (see https://travis-ci.org/dask/dask/jobs/371219552#L827-L832). Could someone please restart the failed builds?

@mrocklin

mrocklin commented Apr 25, 2018 via email

Copy link
Copy Markdown
Member

@mrocklin

Copy link
Copy Markdown
Member

cc @TomAugspurger for review if he has time

@TomAugspurger TomAugspurger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one extremely minor nitpick, and one question :)

Does the same wrapping need to be applied to dd.Index?

Comment thread dask/dataframe/core.py Outdated
for x in inputs + out:
if not isinstance(x, (Number, Scalar, _Frame, Array,
pd.DataFrame, pd.Series, pd.Index)):
# Want to check for 0-dimensional arrays

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: it'd be nice if the comment explained why we're doing this, instead of what we're doing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point. I can update it to something like:

# ufuncs work with 0-dimensional NumPy ndarrays
# so we don't want to raise NotImplemented

Comment thread dask/dataframe/core.py
def __array_wrap__(self, array, context=None):
if isinstance(context, tuple) and len(context) > 0:
index = context[1][0].index
if isinstance(context[1][0], np.ndarray) and context[1][0].shape == ():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting close to enough code where we might want to refactor it to a base method. In pandas this would be a _shallow_copy. You pass it the data, and the result is constructed with all the metadata and indexes.

For now though, it's probably fine.

@jrbourbeau

Copy link
Copy Markdown
Member Author

Does the same wrapping need to be applied to dd.Index?

Just to clarify, do you mean that dd.Index.__array_wrap__ should also have a check for 0-dimensional ndarrays? Currently, if a 0-dimensional ndarray is given to dd.Index.__array_wrap__ it raises

TypeError: len() of unsized object

@TomAugspurger

Copy link
Copy Markdown
Member

I was just checking and it seems like things already work fine?

In [1]: import numpy as np

In [2]: import dask.dataframe as dd

In [3]: import pandas as pd

In [4]: df = dd.from_pandas(pd.Series([1, 2, 3], index=[0, 2, 3]), 2)

In [5]: (df.index + np.array(1)).compute()
Out[5]: Int64Index([1, 3, 4], dtype='int64')

Maybe add a test to verify that things stay fine.

@martindurant

Copy link
Copy Markdown
Member

How about (np.array(1) + df.index).compute()

@TomAugspurger

TomAugspurger commented Apr 26, 2018 via email

Copy link
Copy Markdown
Member

@jrbourbeau

Copy link
Copy Markdown
Member Author

Hmmmm I'm able to reproduce Tom's example. I.e.

import numpy as np
import dask.dataframe as dd
import pandas as pd

df = dd.from_pandas(pd.Series([1, 2, 3], index=[0, 2, 3]), 2)

print((df.index + np.array(1)).compute())

Gives Int64Index([1, 3, 4], dtype='int64').

But reversing the addition order changes behavior depending on which numpy version I'm using.

  • With numpy 1.11.0, np.array(1) + df.index returns a pandas Index Int64Index([1, 3, 4], dtype='int64')

  • With numpy 1.14.2, np.array(1) + df.index returns a dask Index. After compute() is called it gives Int64Index([1, 3, 4], dtype='int64')

I take it this difference has to do with the fact that __array_ufunc__ is using elemwise while __array_wrap__ simply returns a pandas Index, i.e. pd.Index(array, name=self.name).

@mrocklin

mrocklin commented May 1, 2018

Copy link
Copy Markdown
Member

Checking in here. What is the status of this?

@TomAugspurger

Copy link
Copy Markdown
Member

Haven't looked closely, but I think everything I looked at last time was fine. I'd be happy with merging this as is, and opening a separate issue to track the (presumably less common) ops on the Index class, if there's even an issue there.

@jrbourbeau

Copy link
Copy Markdown
Member Author

Just updated comment as per Tom's suggestion

@jrbourbeau jrbourbeau changed the title [WIP] Fixes Series comparison to NumPy scalars Fixes Series comparison to NumPy scalars May 1, 2018
@mrocklin
mrocklin merged commit 7242b8d into dask:master May 1, 2018
@mrocklin

mrocklin commented May 1, 2018

Copy link
Copy Markdown
Member

Great. This is in. Thanks @jrbourbeau !

@jrbourbeau
jrbourbeau deleted the series_comparison_numpy_scalar branch May 1, 2018 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Series comparison to NumPy scalar fails

4 participants