Skip to content

BUG: BooleanArray.value_counts dropna #30824

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

Merged
merged 5 commits into from
Jan 9, 2020

Conversation

TomAugspurger
Copy link
Contributor

Closes #30685

@TomAugspurger TomAugspurger added this to the 1.0 milestone Jan 8, 2020
@TomAugspurger TomAugspurger added the ExtensionArray Extending pandas with custom dtypes or arrays. label Jan 8, 2020
@jbrockmendel
Copy link
Member

LGTM pending green

@@ -856,3 +856,10 @@ def test_arrow_roundtrip():
result = table.to_pandas()
assert isinstance(result["a"].dtype, pd.BooleanDtype)
tm.assert_frame_equal(result, df)


def test_value_counts_na():
Copy link
Contributor

Choose a reason for hiding this comment

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

is the result an object dtyped Series when dropna=True? (add a test as well)

Copy link
Member

Choose a reason for hiding this comment

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

In neither cases (dropna True of False) is the result an object dtype series, it is always integer (it are just counts).

That said, should the result here rather be a nullable integer type? Not that there are nulls here, but in the light of "trying to return nullable types as much as possible from operations involving nullable types".

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, yeah i think we should just move this to return a nullable integer (as this is new api). will promote consistency in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, OK, will update these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And so API breaking change for IntegerARrray.value_counts to return a nullalble int dtype too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you expand on why you find it weird?

It's true that the result of a value_counts will always have no NAs, but returning a nullable int type prevents a reintroduction of NAs in subsequent operations from converting to float.

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 expand on why you find it weird?

The motivation is to maintain consistency of "operations with nullable types return nullable types". But making value_counts().values return IntNA breaks the consistency of "values_counts().values is always np.int64". So it's a wash on "maintaining consistency".

Ideally we'd retain the dtype in the value_counts().index, and it seems like we're saying here "well we cant do that, so let's shoehorn the dtype into the values"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it seems like we're saying here "well we cant do that, so let's shoehorn the dtype into the values"

No, I don't think we're saying that. I think we're saying we find a nullable integer dtype to be more useful.

Copy link
Member

Choose a reason for hiding this comment

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

Not a hill I want to die on.

Copy link
Contributor

Choose a reason for hiding this comment

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

commented below

@TomAugspurger
Copy link
Contributor Author

OK, updated {String,Integer,Boolean}Array.value_counts to return a nullable int64 dtype.

I moved BooleanArray.value_counts and IntegerArray.value_counts to the base class.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Minor comment, looks good for the rest

.. ipython:: python

>>> pd.Series([2, 1, 1, None], dtype="Int64").value_counts().dtype
Int64Dtype()
Copy link
Member

Choose a reason for hiding this comment

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

Don't show the output (or >>> prompt), or otherwise also make it a code-block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, thanks.

@jreback
Copy link
Contributor

jreback commented Jan 9, 2020

let me clarify my comments

i think the index should be nullable ints as that preserves the intent here
but agree the values should be int64 - this is a count after all

@TomAugspurger
Copy link
Contributor Author

TomAugspurger commented Jan 9, 2020

Thanks for clarifying.

The index can't be nullable yet. @jorisvandenbossche thoughts on the values dtype?

@@ -411,6 +411,24 @@ Use :meth:`arrays.IntegerArray.to_numpy` with an explicit ``na_value`` instead.

a.to_numpy(dtype="float", na_value=np.nan)

**value_counts returns a nullable integer dtype**

Copy link
Contributor

Choose a reason for hiding this comment

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

actually, even though is a breaking change, it puts nullable in more general usage. so actually i think this is a good change.

@jorisvandenbossche
Copy link
Member

Ideally we'd retain the dtype in the value_counts().index, and it seems like we're saying here "well we cant do that, so let's shoehorn the dtype into the values"

IMO, ideally we retain the nullable dtype in both the index and the values. But indeed, for index we can't do that yet, so for now it's only the values.

It seems weird that the values would be anything other than np.int64

We are still returning an int dtype. But in the "nullable dtype universe"-subsystem, the "int64" dtype is the nullable int64 dtype.

@TomAugspurger
Copy link
Contributor Author

So I think that's some +1s and a -0 (#30824 (comment)). Merging in an hour or so unless @jbrockmendel objects.

@jbrockmendel
Copy link
Member

No objection here.

@TomAugspurger TomAugspurger merged commit 8bdd7b1 into pandas-dev:master Jan 9, 2020
@TomAugspurger
Copy link
Contributor Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BooleanArray, StringArray value_counts na_value
4 participants