Repository navigation
Don't allow incompatible keywords in DataFrame.dropna - #9366
Conversation
ian-r-rose
left a comment
There was a problem hiding this comment.
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.
| return self.map_partitions(M.eval, expr, meta=meta, inplace=inplace, **kwargs) | ||
|
|
||
| if PANDAS_GT_150: | ||
| from pandas._libs.lib import no_default |
There was a problem hiding this comment.
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
| meta = self._meta.eval(expr, inplace=inplace, **kwargs) | ||
| return self.map_partitions(M.eval, expr, meta=meta, inplace=inplace, **kwargs) | ||
|
|
||
| if PANDAS_GT_150: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
whoops, yes you are right, I'll leave outside the class.
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. |
| return self.map_partitions(M.eval, expr, meta=meta, inplace=inplace, **kwargs) | ||
|
|
||
| if PANDAS_GT_150: | ||
| from pandas._libs.lib import no_default |
|
|
||
| @derived_from(pd.DataFrame) | ||
| def dropna(self, how="any", subset=None, thresh=None): | ||
| def dropna(self, how=how_default, subset=None, thresh=thresh_default): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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
)There was a problem hiding this comment.
Yeah, I'd also drop the not PANDAS_GT_150 part and just always raise that error appropriately
| assert_eq(df.dropna(), df.loc[[20, 40]]) | ||
| assert_eq(ddf.dropna(), df.dropna()) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Could you also add a test that checks an error is raised when both how and thresh are specified?
There was a problem hiding this comment.
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.
| def dropna(self, how="any", subset=None, thresh=None): | ||
| def dropna(self, how=pd_no_default, subset=None, thresh=pd_no_default): | ||
|
|
||
| if ( |
There was a problem hiding this comment.
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.
| and (how is not pd_no_default) | ||
| and (thresh is not pd_no_default) | ||
| ): | ||
| raise ValueError( |
There was a problem hiding this comment.
Pandas error is a TypeError, but when we catch the errors in here
Lines 215 to 216 in f7fd6c4
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
|
Thank you @jrbourbeau for that push This failure seems unrelated, https://github.com/dask/dask/runs/7752422857?check_suite_focus=true |
DataFrame.dropna to match upstream pandas BUG-fixDataFrame.dropna to match upstream pandas BUG-fix
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| from pandas.api.extensions import no_default as pd_no_default |
There was a problem hiding this comment.
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
DataFrame.dropna to match upstream pandas BUG-fixhow and thresh to both be specified in DataFrame.dropna
how and thresh to both be specified in DataFrame.dropnaDataFrame.dropna
jrbourbeau
left a comment
There was a problem hiding this comment.
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]]) |
There was a problem hiding this comment.
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.
|
|
||
| # Only specify `how` or `thresh` keyword if specified by the user | ||
| # so we utilize other `dropna` keyword defaults appropriately | ||
| kwargs = {"subset": subset} |
|
This looks good to me and tests related to this PR are happy. |
|
Thanks @ncclementi @ian-r-rose |
DataFrame.dropnamatch pandas upstream bug fix #9365pre-commit run --all-files