Skip to content

Adding support of non-numeric data to describe() (#4401) - #4791

Merged
TomAugspurger merged 7 commits into
dask:masterfrom
almaleksia:describe_non_numeric
May 17, 2019
Merged

TomAugspurger merged 7 commits into
dask:masterfrom
almaleksia:describe_non_numeric

Conversation

@almaleksia

Copy link
Copy Markdown
Contributor

#4401

  • Tests added / passed
  • Passes flake8 dask

This PR adds handling of non-numeric dtypes in describe().

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

Thanks for the PR @almaleksia, I left a few comments, but overall this looks pretty good to me (provided tests pass).

Comment thread dask/dataframe/core.py Resolved
Comment thread dask/dataframe/core.py Resolved
Comment thread dask/dataframe/tests/test_dataframe.py Resolved
Comment thread dask/dataframe/tests/test_dataframe.py Resolved
Comment thread dask/dataframe/tests/test_dataframe.py Resolved
Comment thread dask/dataframe/tests/test_dataframe.py Resolved
@almaleksia
almaleksia force-pushed the describe_non_numeric branch from 2a36e4f to f1da451 Compare May 13, 2019 17:26

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

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.

Comment thread dask/dataframe/core.py Resolved
Comment thread dask/dataframe/methods.py Resolved
Comment thread dask/dataframe/tests/test_dataframe.py Outdated
for col in ['a', 'c', 'e', 'g']:
assert_eq(
df[col].describe(include=include, exclude=exclude),
ddf[col].describe(include=include, exclude=exclude).compute())

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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?

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.

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?

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.

We'll see what comes of pandas-dev/pandas#26397. It's not clear to me what's most useful to users.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Comment thread dask/dataframe/tests/test_dataframe.py Outdated
# 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

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'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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Small comment on the test for timedelta data. LGTM otherwise.

Comment thread dask/dataframe/tests/test_dataframe.py Outdated
# 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:

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.

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 :)

Comment thread dask/dataframe/tests/test_dataframe.py Outdated
if 'f' in desc_ddf._meta:
with pytest.raises(AssertionError) as info:
assert_eq(desc_ddf, desc_df)
msg = "DataFrame.iloc[:, 2] are different"

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@TomAugspurger yes it is wrong indent :( I'll just drop the column then as everyone agreed on it.

@TomAugspurger

Copy link
Copy Markdown
Member

Thanks @almaleksia!

@TomAugspurger
TomAugspurger merged commit 2b1bf66 into dask:master May 17, 2019
@mrocklin

Copy link
Copy Markdown
Member

Thanks for the fix @almaleksia !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants