Skip to content

PERF: fix assert_frame_equal can be very slow #38202

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 14 commits into from
Dec 24, 2020

Conversation

ivanovmg
Copy link
Member

@ivanovmg ivanovmg commented Dec 1, 2020

Add kwarg check_index into assert_series_equal to allow elimination of multiple index checking (for each column) in assert_frame_equal.

Performance wise.

import pandas as pd
import numpy as np
from string import ascii_letters

idx = np.random.choice(np.array(list(ascii_letters)), size=10000).astype(object)
a = np.random.random_integers(0, 100, (10000, 1000))

%time pd.testing.assert_frame_equal(pd.DataFrame(a), pd.DataFrame(a.copy()))
Wall time: 319 ms

%time pd.testing.assert_index_equal(pd.Index(idx), pd.Index(idx.copy()))
Wall time: 993 µs

%time pd.testing.assert_frame_equal(pd.DataFrame(a, index=idx), pd.DataFrame(a, index=idx))
Wall time: 281 ms

@ivanovmg
Copy link
Member Author

ivanovmg commented Dec 1, 2020

Looks like green.

@jreback
Copy link
Contributor

jreback commented Dec 1, 2020

hmm can you merge master (this looks like it's running some older code)

@ivanovmg
Copy link
Member Author

ivanovmg commented Dec 1, 2020

Did that.
But presumably we have the same irrelevant failures anyway.

@jreback
Copy link
Contributor

jreback commented Dec 1, 2020

Did that.
But presumably we have the same irrelevant failures anyway.

it was some other errors.

but in spite of this change i am not sure this actually fixes things. (tests times are about the same)

@jbrockmendel
Copy link
Member

tests times are about the same

i doubt we have any/many tests that are as poorly behaved as the example from #38183

@@ -1703,6 +1710,7 @@ def assert_frame_equal(
obj=f'{obj}.iloc[:, {i}] (column name="{col}")',
rtol=rtol,
atol=atol,
check_index=False,
)
Copy link
Member

Choose a reason for hiding this comment

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

we have a lot of kwargs already. would it be viable to call assert_(ea|numpy)_array_equal on lcol._values and rcol._values?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably.
But I looked inside assert_series_equal and noticed that there are a lot of cases covered, different dtypes, etc.
So, I thought that it would be safer to parametrize.
I will try to find another way, as you suggest.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jbrockmendel, I am afraid I can only suggest the following approach for now.

Split assert_series_equal into assert_index_equal and _assert_series_values_equal.
The latter one is the same as the original assert_series_equal, but with the assert_index_equal removed from there.

Then we can use _assert_series_values_equal in the loop inside assert_frame_equal.

Copy link
Member

Choose a reason for hiding this comment

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

works for me

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm i actually liked your prior implementation (check_index) but i guess this is fine.

@ivanovmg
Copy link
Member Author

ivanovmg commented Dec 1, 2020

Did that.
But presumably we have the same irrelevant failures anyway.

it was some other errors.

but in spite of this change i am not sure this actually fixes things. (tests times are about the same)

Execution of the following command improved.

%time pd.testing.assert_frame_equal(pd.DataFrame(a, index=idx), pd.DataFrame(a, index=idx))

from 50 seconds to 280 something milliseconds (#38183).

@ivanovmg
Copy link
Member Author

ivanovmg commented Dec 1, 2020

I can think of a test ensuring that assert_frame_equals calls assert_index_equal only once.
This is not a performance test, but for now we see that the problem with the performance observed in the original issue is due to multiple calls of the latter one.
What do you think of this approach?
@jreback @jbrockmendel

In fact, assert_index_equal is called twice: first time for checking index equivalence and second - for columns.
Added this test.

@@ -1,3 +1,5 @@
from unittest.mock import patch
Copy link
Member

Choose a reason for hiding this comment

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

i think elsewhere we use pytest's monkeypatch fixture, does that not provide the call_count check?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not find this functionality there.

@@ -285,3 +287,15 @@ def test_allows_duplicate_labels():

with pytest.raises(AssertionError, match="<Flags"):
tm.assert_frame_equal(left, right)


def test_assert_frame_equal_checks_index_exactly_twice():
Copy link
Member

Choose a reason for hiding this comment

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

i dont think this necessarily needs a test, maybe an asv. as long as there is a comment in tm.assert_frame_equal about why we are doing what we're doing that should be enough

Copy link
Member Author

Choose a reason for hiding this comment

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

I am removing the test because it relies on unittest.mock, which is not accepted by the CI check.

@@ -1690,11 +1727,10 @@ def assert_frame_equal(
assert col in right
lcol = left.iloc[:, i]
rcol = right.iloc[:, i]
Copy link
Member

Choose a reason for hiding this comment

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

marginally faster to do right._ixs(i, axis=1)

rtol=1.0e-5,
atol=1.0e-8,
obj="Series",
):
if check_less_precise is not no_default:
Copy link
Member

Choose a reason for hiding this comment

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

__tracebackhide__ = True

@jreback
Copy link
Contributor

jreback commented Dec 2, 2020

Did that.
But presumably we have the same irrelevant failures anyway.

it was some other errors.
but in spite of this change i am not sure this actually fixes things. (tests times are about the same)

Execution of the following command improved.

%time pd.testing.assert_frame_equal(pd.DataFrame(a, index=idx), pd.DataFrame(a, index=idx))

from 50 seconds to 280 something milliseconds (#38183).

ok maybe something else is stil take time

@jreback
Copy link
Contributor

jreback commented Dec 2, 2020

its this build: https://travis-ci.org/github/pandas-dev/pandas/jobs/747040921 that is really really slow, used to be like 30min.

@jreback
Copy link
Contributor

jreback commented Dec 2, 2020

pre-commit checks are faling



def _assert_series_values_equal(
left,
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah let's revert this and use the check_index=False

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted to use of check_index.

Copy link
Member Author

@ivanovmg ivanovmg Dec 2, 2020

Choose a reason for hiding this comment

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

But personally I prefer the way involving splitting of assert_series_equal into two functions.
Indeed, this function does two major things: check index equivalence and check values equivalence, so it is reasonable to split it like that.
I see that there are multiple parameters passed to _assert_series_values_equal, which make this solution look not very elegant.
But there is a benefit that we do not touch public API, by not adding new parameter check_index.
Please suggest which solution you prefer (@jbrockmendel suggested that we do not introduce new kwarg).

Copy link
Member

Choose a reason for hiding this comment

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

the new kwarg isnt the worst thing in the world. lets make it keyword-only to start the process of #38222

Copy link
Contributor

Choose a reason for hiding this comment

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

ok i think this is fine (reason I don't mind check_index is that we have this elsewhere publicly). ok let's make this kwarg only fo r the new paramater, otherwise lgtm.

Copy link
Member Author

Choose a reason for hiding this comment

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

I make this kwarg keyword-only.

@jreback jreback added the Testing pandas testing functions or related to the test suite label Dec 2, 2020
@ivanovmg ivanovmg requested a review from jreback December 2, 2020 07:46
@ivanovmg ivanovmg requested a review from jbrockmendel December 2, 2020 07:46
@jreback jreback added this to the 1.2 milestone Dec 2, 2020
@simonjayhawkins
Copy link
Member

We normally have a high bar for adding parameters to the public api. Since assert_series_equal is public, should we give this more consideration.

we could just have an internal assert_series_equal for now with the extra parameter, and dispatch from the public assert_series_equal with the api unchanged.

@jreback jreback removed this from the 1.2 milestone Dec 7, 2020
@jreback
Copy link
Contributor

jreback commented Dec 7, 2020

We normally have a high bar for adding parameters to the public api. Since assert_series_equal is public, should we give this more consideration.

we could just have an internal assert_series_equal for now with the extra parameter, and dispatch from the public assert_series_equal with the api unchanged.

-1 on this (which was already suggested), this add even more complexity in an already complex world. we already have a check_index keyword so nbd here.

check_index : bool, default True
Whether to check index equivalence. If False, then compare only values.

.. versionadded:: 1.2.0
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 1.3

@@ -1702,8 +1710,12 @@ def assert_frame_equal(
else:
for i, col in enumerate(left.columns):
assert col in right
lcol = left.iloc[:, i]
rcol = right.iloc[:, i]
lcol = left._ixs(i, axis=1)
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you changing this? this is not a public function and shouldn't be used here at all

Copy link
Member Author

Choose a reason for hiding this comment

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

@jbrockmendel suggested that it would be a marginally faster option.
Looks like it actually is.

Copy link
Contributor

Choose a reason for hiding this comment

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

it doesn't matter if its faster as its not public; these are heavily used public function (yes this is an implementation), but we want to use iloc here (and i doubt this actually makes any real world difference).

@jreback
Copy link
Contributor

jreback commented Dec 23, 2020

can you show how this is faster, specifically are things decreasing in the duration? which builds are faster?

@ivanovmg
Copy link
Member Author

can you show how this is faster, specifically are things decreasing in the duration? which builds are faster?

I compared iloc vs _ixs and did not notice much difference. So, I reverted back to iloc.

@jreback jreback added this to the 1.3 milestone Dec 24, 2020
@jreback jreback merged commit df90970 into pandas-dev:master Dec 24, 2020
@jreback
Copy link
Contributor

jreback commented Dec 24, 2020

thanks @ivanovmg

jbrockmendel pushed a commit to jbrockmendel/pandas that referenced this pull request Dec 27, 2020
luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: assert_frame_equal can be very slow
4 participants