Skip to content
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

BUG: GroupBy.quantile fails with pd.NA #43150

Merged
merged 9 commits into from
Sep 4, 2021

Conversation

debnathshoham
Copy link
Member

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

small comment, ping on green

pandas/core/groupby/groupby.py Outdated Show resolved Hide resolved
@jreback jreback added this to the 1.3.3 milestone Aug 21, 2021
@jreback jreback added Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Aug 21, 2021
@debnathshoham
Copy link
Member Author

@jreback @jbrockmendel Green

@debnathshoham
Copy link
Member Author

Hi @jreback , @jbrockmendel - could you please take a look if this looks fine


def test_groupby_quantile_allNA_column():
# GH#42849
df = DataFrame({"x": [1, 1], "y": [pd.NA] * 2}, dtype="Float64")
Copy link
Member

Choose a reason for hiding this comment

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

any reason for this to be Float64 instead of any_foo_dtype?

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure what any_foo_dtype means.
In case it means why the explicit dtype? then without explicitly defining with nullable dtypes, the columns becomes object, and quantile fails (maybe I am wrong, but I believe this is the expected behaviour). Below on master.

In [3]: import pandas as pd

In [4]: pd.__version__
Out[4]: '1.4.0.dev0+540.ga826be1f61'

In [5]: DataFrame({"x": [1, 1], "y": [pd.NA] * 2}).dtypes
Out[5]: 
x     int64
y    object
dtype: object

In [6]: DataFrame({"x": [1, 1], "y": [pd.NA] * 2}, dtype=float).dtypes
<ipython-input-6-4731f064b6c6>:1: FutureWarning: Could not cast to float64, falling back to object. This behavior is deprecated. In a future version, when a dtype is passed to 'DataFrame', either all columns will be cast to that dtype, or a TypeError will be raised.
  DataFrame({"x": [1, 1], "y": [pd.NA] * 2}, dtype=float).dtypes
Out[6]: 
x    float64
y     object
dtype: object

def test_groupby_quantile_NA_float(any_float_dtype):
# GH#42849
df = DataFrame({"x": [1, 1], "y": [0.2, np.nan]}, dtype=any_float_dtype)
result = df.groupby("x")["y"].quantile(0.5)
Copy link
Member

Choose a reason for hiding this comment

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

can you do a case with a listlike qs e.g. [0.5, 0.75]

Copy link
Member Author

Choose a reason for hiding this comment

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

added below

@debnathshoham
Copy link
Member Author

Hi @jbrockmendel could you pls take a quick look if this is fine now?

tm.assert_series_equal(expected, result)

result = df.groupby("x").quantile(0.5)
expected = DataFrame({"y": 3.5}, index=Index([1], name="x"))
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: clearer to just do expected = expected["y"] next time

@jbrockmendel
Copy link
Member

LGTM

wouldnt surprise me if some of the tests could be condensed/parametrized, OK for follow-up

@jreback
Copy link
Contributor

jreback commented Sep 4, 2021

thanks @debnathshoham (followup to parameterize tests here would be great, that can be on master)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: GroupBy's quantile incompatible with pd.NA
3 participants