Skip to content

Don't allow incompatible keywords in DataFrame.dropna - #9366

Merged
jrbourbeau merged 9 commits into
dask:mainfrom
ncclementi:fix-upstream-dropna
Aug 11, 2022
Merged

jrbourbeau merged 9 commits into
dask:mainfrom
ncclementi:fix-upstream-dropna

Conversation

@ncclementi

Copy link
Copy Markdown
Member

@ian-r-rose ian-r-rose left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @ncclementi.

One of these days, I think we should reconsider some of the policies around all of these backwards incompatibility checks, and probably phase some of them out. But this approach seems fine for now.

Comment thread dask/dataframe/core.py Outdated
return self.map_partitions(M.eval, expr, meta=meta, inplace=inplace, **kwargs)

if PANDAS_GT_150:
from pandas._libs.lib import no_default

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: rather than importing from a private module, we can import from pandas.api.extensions. It's not a huge deal, and pandas doesn't provide very strong guarantees around this, it seems, but see some discussion in pandas-dev/pandas#40397

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.

+1

Comment thread dask/dataframe/core.py Outdated
meta = self._meta.eval(expr, inplace=inplace, **kwargs)
return self.map_partitions(M.eval, expr, meta=meta, inplace=inplace, **kwargs)

if PANDAS_GT_150:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This clause is kind of odd, in that it attaches how_default and thresh_default to _Frame as class variables, which I don't think is what you intended. Probably cleaner to keep this logic at the module scope.

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.

whoops, yes you are right, I'll leave outside the class.

@jsignell

jsignell commented Aug 9, 2022

Copy link
Copy Markdown
Member

One of these days, I think we should reconsider some of the policies around all of these backwards incompatibility checks, and probably phase some of them out.

This has been niggling at me for a while. It would be good to create a policy for how Dask should handle this kind of API change.

Comment thread dask/dataframe/core.py Outdated
return self.map_partitions(M.eval, expr, meta=meta, inplace=inplace, **kwargs)

if PANDAS_GT_150:
from pandas._libs.lib import no_default

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.

+1

Comment thread dask/dataframe/core.py Outdated

@derived_from(pd.DataFrame)
def dropna(self, how="any", subset=None, thresh=None):
def dropna(self, how=how_default, subset=None, thresh=thresh_default):

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.

Currently we're trying to match pandas behavior here exactly, which is normally a guiding philosophy. However, in this particular case, since the change in pandas was to fix a bug where incompatible keywords could be specified and unexpected results returned pandas-dev/pandas#46575, I think it would make sense for us to always raise an error when how and thresh are both specified (i.e. always match the new pandas behavior). While this does lead to slightly different behavior for older versions of pandas, Dask users would still benefit from not having an unexpected result returned

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.

@jrbourbeau Should we then set both to no_default and handle manually the case pandas < 1.5. following what pandas does, something like

    @derived_from(pd.DataFrame)
    def dropna(self, how=how_default, subset=None, thresh=thresh_default):
        if not PANDAS_GT_150 and how is not no_default and thresh is not no_default:
            raise ValueError(
                "You cannot set both the how and thresh arguments at the same time."
            )

         if how is no_default:
            how = "any"  
          
        return self.map_partitions(
            M.dropna, how=how, subset=subset, thresh=thresh, enforce_metadata=False
        )

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.

Yeah, I'd also drop the not PANDAS_GT_150 part and just always raise that error appropriately

Comment thread dask/dataframe/tests/test_dataframe.py Outdated
Comment on lines +765 to +766
assert_eq(df.dropna(), df.loc[[20, 40]])
assert_eq(ddf.dropna(), df.dropna())

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.

Hrm, why did we drop thresh= here? It looks like it wasn't being used in conjunction with how= in this case (though I could be missing something)

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.

Could you also add a test that checks an error is raised when both how and thresh are specified?

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.

I think I was trying something and I forgot, and now I see that if they are there, we have an AssertionError for
assert_eq(df.dropna(thresh=None), df.loc[[20, 40]]) I have to take a deeper look. I'll push the code so it's visible.

Comment thread dask/dataframe/core.py Outdated
def dropna(self, how="any", subset=None, thresh=None):
def dropna(self, how=pd_no_default, subset=None, thresh=pd_no_default):

if (

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.

This is causing a problem in the dropna test when we do assert_eq(ddf.dropna(), df.dropna()) I think it's because it's trying to use pandas defaults.

I'm not quite sure how to make things work on the tests to be consistent.

Comment thread dask/dataframe/core.py Outdated
and (how is not pd_no_default)
and (thresh is not pd_no_default)
):
raise ValueError(

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.

Pandas error is a TypeError, but when we catch the errors in here

msg = msg.format(f" in `{funcname}`" if funcname else "", repr(e), tb)
raise ValueError(msg) from e
we use a ValueError so I'm using the same to be consistent.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Interesting, I wonder why they raise a TypeError, ValueError seems more semantically correct to me as well.

I might prefer to be more consistent with pandas, so that user code could theoretically catch the same exception whether using dask or pandas, but I am not sure it's a huge deal either way

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.

Yeah, I had the same thought and preference as you. I'll go ahead and make this a TypeError. Like you said, probably not a big deal either way, but could lead to unexpected behavior if coming from pandas

@ncclementi

Copy link
Copy Markdown
Member Author

Thank you @jrbourbeau for that push

This failure seems unrelated, https://github.com/dask/dask/runs/7752422857?check_suite_focus=true
and the remaining tests on the upstream CI are the known failures.

@jrbourbeau jrbourbeau changed the title Fix DataFrame.dropna to match upstream pandas BUG-fix [WIP] Fix DataFrame.dropna to match upstream pandas BUG-fix Aug 9, 2022
Comment thread dask/dataframe/core.py Outdated

import numpy as np
import pandas as pd
from pandas.api.extensions import no_default as pd_no_default

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.

As is the changes here will pass the no_default from pandas down to the .dropna(...) method applied to each DataFrame partition. For Dask DataFrames that are backed by pandas DataFrames this should work totally fine, but my guess is this will break for cudf. cc @galipremsagar @quasiben for thoughts.

xref #8645

@jrbourbeau jrbourbeau changed the title [WIP] Fix DataFrame.dropna to match upstream pandas BUG-fix Don't allow how and thresh to both be specified in DataFrame.dropna Aug 10, 2022
@jrbourbeau jrbourbeau changed the title Don't allow how and thresh to both be specified in DataFrame.dropna Don't allow incompatible keywords in DataFrame.dropna Aug 10, 2022

@jrbourbeau jrbourbeau 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.

Alright, I think this is ready for another (possibly final) review pass. I opted to only forward only one of how or thresh in the underlying map_partitions call if one of them is specified (we raise an error early if both are specified). This will allow us to be more agnostic to the pandas version being used and also not break when operating on cudf DataFrames (I think).

We should still push the conversation forward on #8645, but I don't think a conclusion there should block this PR.

)

# threshold
assert_eq(df.dropna(thresh=None), df.loc[[20, 40]])

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.

I removed a few asserts which were testing pandas but not dask (note these are using df instead of ddf). Not sure why these were added originally, but seem out of scope for our test suite.

Comment thread dask/dataframe/core.py

# Only specify `how` or `thresh` keyword if specified by the user
# so we utilize other `dropna` keyword defaults appropriately
kwargs = {"subset": subset}

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.

😎

@ncclementi

Copy link
Copy Markdown
Member Author

This looks good to me and tests related to this PR are happy.

@jrbourbeau

Copy link
Copy Markdown
Member

Thanks @ncclementi @ian-r-rose

@jrbourbeau
jrbourbeau merged commit edb8a10 into dask:main Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix DataFrame.dropna match pandas upstream bug fix

4 participants