Skip to content

BUG: _can_use_numexpr fails when passed large Series #27773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2019

Conversation

ccharlesgb
Copy link
Contributor

This fixes a regression introduced in #27145 where _can_use_numexpr would fail if passed a Series and not a DataFrame. I decided not to use run_arithmetic in the test_suite because there is a separate issue when running floordiv that is out of the scope of the fix. I will open a separate issue in improving the test coverage of "test_expressions.py" as it currently does not check any of the arguments you can pass to the operators, such as 'axis', 'level' and 'fill_value'.

@gfyoung gfyoung added Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version labels Aug 6, 2019
@ccharlesgb
Copy link
Contributor Author

ccharlesgb commented Aug 7, 2019

There was some CI failures (Looked like some unrelated http exceptions so I pulled the latest commits in from master to see if that fixes it).

I have added the xfail for axis=1 on floordiv and made the whatsnew entry more specific now which should now cover all comments.

@ccharlesgb ccharlesgb force-pushed the numexpr-bugfix branch 2 times, most recently from 3fb801c to 7caea6c Compare August 7, 2019 11:06
@jbrockmendel
Copy link
Member

I think the CI error is unrelated. Can you rebase and see if it resolves itself?

@ccharlesgb
Copy link
Contributor Author

I think the CI error is unrelated. Can you rebase and see if it resolves itself?

That has fixed it. @WillAyd are you comfortable with the changes?

else:
other = self.frame.iloc[:, 0]

expr._MIN_ELEMENTS = 0
Copy link
Member

Choose a reason for hiding this comment

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

What does this do?

Copy link
Contributor Author

@ccharlesgb ccharlesgb Aug 8, 2019

Choose a reason for hiding this comment

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

There is an overhead in using numexpr for computations that makes it not worth using for a low number of computations. The values of this defaults to: 10,000. In this test suite we are trying to verify that numexpr is being invoked correctly however the function _can_use_numexpr checks that the objects operated on are sufficient size:

def _can_use_numexpr(op, op_str, a, b, dtype_check):
    """ return a boolean if we WILL be using numexpr """
    if op_str is not None:
        # required min elements (otherwise we are adding overhead)
        if np.prod(a.shape) > _MIN_ELEMENTS:
            # further dtype checks to check compatibility may return True
    return False

We can therefore run the test suite exclusively on objects where the number of elements > 10,000 or set the min elements to zero to always try and use numexpr.

This is what is meant by a "large DataFrame". Anything where the size of the objects is large enough to warrant using numexpr fails because of the regression, anything that is smaller than the threshold will succeed because numexpr evaluation will not even be considered.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm is affecting global state, right? I worry about this leaking into other tests...

Can you use the monkeypatch fixture, and do this setting with (IIRC) monkeypatch.setattr? That way it'll be undone when the test exits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is but this is handled in the test classes teardown method on line 57 so the global state won't be affected permanently. I can move my test out of the class and handle this with monkeypatch or we can keep the setup/teardown framework already implemented in the TestExpressions class.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, I wasn't familiar with these tests.

@ccharlesgb ccharlesgb force-pushed the numexpr-bugfix branch 3 times, most recently from 5d85901 to 1bc2900 Compare August 8, 2019 22:19
@ccharlesgb
Copy link
Contributor Author

Hi @WillAyd is the Whatsnew entry ok now?

Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

Looks close, one question about the test.

else:
other = self.frame.iloc[:, 0]

expr._MIN_ELEMENTS = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm is affecting global state, right? I worry about this leaking into other tests...

Can you use the monkeypatch fixture, and do this setting with (IIRC) monkeypatch.setattr? That way it'll be undone when the test exits.

@TomAugspurger TomAugspurger added this to the 0.25.1 milestone Aug 12, 2019
@TomAugspurger
Copy link
Contributor

FYI, can you just merge master & do a regular push, rather than a force push? We don't always get notifications on force pushes.

@jbrockmendel
Copy link
Member

This should also close #27853 @ccharlesgb can you make sure the use case there is covered by the new tests?

@ccharlesgb
Copy link
Contributor Author

This should also close #27853 @ccharlesgb can you make sure the use case there is covered by the new tests?

@jbrockmendel yes this case is covered in the test as long as divide/div and truediv are all still aliases of each other?

@ccharlesgb
Copy link
Contributor Author

FYI, can you just merge master & do a regular push, rather than a force push? We don't always get notifications on force pushes.

@jbrockmendel Had to rebase as the CI was failing again. But I think everything is addressed now.

@TomAugspurger TomAugspurger merged commit fb62fcf into pandas-dev:master Aug 19, 2019
@TomAugspurger
Copy link
Contributor

Thanks @ccharlesgb!

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Aug 19, 2019
EunSeop pushed a commit to EunSeop/pandas that referenced this pull request Aug 20, 2019
* BUG: _can_use_numexpr did not handle Series case correctly
galuhsahid pushed a commit to galuhsahid/pandas that referenced this pull request Aug 25, 2019
* BUG: _can_use_numexpr did not handle Series case correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Operators between DataFrame and Series fail on large dataframes
5 participants