Repository navigation
Adding support of non-numeric data to describe() (#4401) - #4791
Conversation
jcrist
left a comment
There was a problem hiding this comment.
Thanks for the PR @almaleksia, I left a few comments, but overall this looks pretty good to me (provided tests pass).
2a36e4f to
f1da451
Compare
jcrist
left a comment
There was a problem hiding this comment.
Thanks @almaleksia, I gave this a deeper review. There's a few issues with how the tests are written (we generally don't want to compute before calling assert_eq), and I left a few other comments, but this is pretty close to done.
| for col in ['a', 'c', 'e', 'g']: | ||
| assert_eq( | ||
| df[col].describe(include=include, exclude=exclude), | ||
| ddf[col].describe(include=include, exclude=exclude).compute()) |
There was a problem hiding this comment.
Normally we pass an uncomputed dask object to assert_eq. This assertion not only checks that its computed version matches the desired result (e.g. the pandas dataframe), but also checks that the various bits of metadata on the dask object are correct. You likely want to drop the .compute() call here and elsewhere in your tests.
There was a problem hiding this comment.
@jcrist thank you for noticing this.
I have a question regarding meta for empty dataframe. Pandas describe() output has different dtypes when frame is empty:
>df.describe(include='all').dtypes
a float64
b object
dtype: object
>df_none = pd.DataFrame({'a':[None,None], 'b':[None,None]})
>df_none.describe(include='all').dtypes
a int64
b int64
dtype: object
meta is being assigned at the moment when it's unknown whether dataframe is empty, therefore pandas output and dask dataframe meta might not match. Could you suggest the right way to handle such case?
There was a problem hiding this comment.
This looks like a bug in pandas. Ideally the output shape would depend only on the dtypes, not the values. I'll verify and open a pandas issue.
I haven't looked at the implementation, but can you use self._meta_nonempty to compute the metadata rather than self._meta?
There was a problem hiding this comment.
We'll see what comes of pandas-dev/pandas#26397. It's not clear to me what's most useful to users.
There was a problem hiding this comment.
can you use self._meta_nonempty to compute the metadata rather than self._meta
@TomAugspurger thank you for suggestion, sure I should've used _meta_nonempty.
I think empty describe has different dtypes because it doesn't know dtypes of columns and cannot calculate any stats except count and unique which are of integer type.
> df_none.describe(include='all')
a b
count 0 0
unique 0 0
> df.describe(include='all')
a b
count 2.00000 2
unique NaN 2
top NaN True
freq NaN 1
mean 1.75000 NaN
std 1.06066 NaN
min 1.00000 NaN
25% 1.37500 NaN
50% 1.75000 NaN
75% 2.12500 NaN
max 2.50000 NaN
| # causing std to be nan, ignoring this cell | ||
| if 'f' in desc_ddf_computed: | ||
| desc_df['f']['std'] = None | ||
| desc_ddf_computed['f']['std'] = None |
There was a problem hiding this comment.
I'd just drop this column, so you can avoid computing before passing to assert_eq. Something like:
if 'f' in desc_df.columns:
desc_df = desc_df.drop('f', axis=1)
desc_ddf = desc_ddf.drop('f', axis=1)
assert_eq(desc_df, desc_ddf)There was a problem hiding this comment.
@jcrist thank you for the comments! My concern is, if we drop whole column the output for timedelta will not be tested at all. What if I workaround it with this:
if 'f' in desc_ddf._meta:
with pytest.raises(AssertionError) as info:
assert_eq(desc_ddf, desc_df)
msg = "DataFrame.iloc[:, 2] are different"
assert msg in str(info.value)It's very ugly for sure but at least the remaining input is validated. What do you think?
TomAugspurger
left a comment
There was a problem hiding this comment.
Small comment on the test for timedelta data. LGTM otherwise.
| # TODO: for timedelta columns there's overflow in var function, see #4233 | ||
| # causing std to be nan, workaround this allowing AssertionError on std sell | ||
| if 'f' in desc_ddf._meta: | ||
| with pytest.raises(AssertionError) as info: |
There was a problem hiding this comment.
My only nitpick here is that fixing the overflow will cause this test to fail, which may cause some confusion in the future, but you've documented it nicely :)
| if 'f' in desc_ddf._meta: | ||
| with pytest.raises(AssertionError) as info: | ||
| assert_eq(desc_ddf, desc_df) | ||
| msg = "DataFrame.iloc[:, 2] are different" |
There was a problem hiding this comment.
I don't think this code is executed, since the with block should exit after the AssertionError in assert_eq (unless I'm misunderstanding).
May need to move the assert outside, though personally I wouldn't worry about the error message. I also think that @jcrist's suggestion of dropping is fine too.
There was a problem hiding this comment.
@TomAugspurger yes it is wrong indent :( I'll just drop the column then as everyone agreed on it.
|
Thanks @almaleksia! |
|
Thanks for the fix @almaleksia ! |
#4401
flake8 daskThis PR adds handling of non-numeric dtypes in describe().