Skip to content

API: consistent default dropna for value_counts #39511

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
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions asv_bench/benchmarks/io/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class ToCSVIndexes(BaseIO):
def _create_df(rows, cols):
index_cols = {
"index1": np.random.randint(0, rows, rows),
"index2": np.full(rows, 1, dtype=np.int),
"index3": np.full(rows, 1, dtype=np.int),
"index2": np.full(rows, 1, dtype=int),
"index3": np.full(rows, 1, dtype=int),
}
data_cols = {
f"col{i}": np.random.uniform(0, 100000.0, rows) for i in range(cols)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ def delete(self: NDArrayBackedExtensionArrayT, loc) -> NDArrayBackedExtensionArr
# These are not part of the EA API, but we implement them because
# pandas assumes they're there.

def value_counts(self, dropna: bool = False):
def value_counts(self, dropna: bool = True):
"""
Return a Series containing counts of unique values.

Parameters
----------
dropna : bool, default False
dropna : bool, default True
Don't include counts of NA values.

Returns
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ def notna(self):

notnull = notna

def value_counts(self, dropna=True):
def value_counts(self, dropna: bool = True):
"""
Return a Series containing counts of each category.

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ def _validate_setitem_value(self, value):
raise ValueError("Cannot set float NaN to integer-backed IntervalArray")
return value_left, value_right

def value_counts(self, dropna=True):
def value_counts(self, dropna: bool = True):
"""
Returns a Series containing counts of each interval.

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,13 @@ def factorize(self, na_sentinel=-1):
uniques = SparseArray(uniques, dtype=self.dtype)
return codes, uniques

def value_counts(self, dropna=True):
def value_counts(self, dropna: bool = True):
"""
Returns a Series containing counts of unique values.

Parameters
----------
dropna : boolean, default True
dropna : bool, default True
Don't include counts of NaN, even if NaN is in sp_values.

Returns
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def max(self, axis=None, skipna: bool = True, **kwargs) -> Scalar:
)
return self._wrap_reduction_result(axis, result)

def value_counts(self, dropna=False):
def value_counts(self, dropna: bool = True):
from pandas import value_counts

return value_counts(self._ndarray, dropna=dropna).astype("Int64")
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,12 @@ def describe(self, **kwargs):
return result.unstack()

def value_counts(
self, normalize=False, sort=True, ascending=False, bins=None, dropna=True
self,
normalize=False,
sort=True,
ascending=False,
bins=None,
dropna: bool = True,
):

from pandas.core.reshape.merge import get_join_indexers
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_value_counts_preserves_tz(self):
assert result.index.equals(dti)

arr[-2] = pd.NaT
result = arr.value_counts()
result = arr.value_counts(dropna=False)
expected = pd.Series([4, 2, 1], index=[dti[0], dti[1], pd.NaT])
tm.assert_series_equal(result, expected)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import operator

import numpy as np
Expand All @@ -15,6 +16,14 @@
class BaseMethodsTests(BaseExtensionTests):
"""Various Series and DataFrame methods."""

def test_value_counts_default_dropna(self, data):
# make sure we have consistent default dropna kwarg
if not hasattr(data, "value_counts"):
pytest.skip("value_counts is not implemented")
sig = inspect.signature(data.value_counts)
kwarg = sig.parameters["dropna"]
assert kwarg.default is True

@pytest.mark.parametrize("dropna", [True, False])
def test_value_counts(self, all_data, dropna):
all_data = all_data[:10]
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def convert_values(param):

return np.asarray(res, dtype=bool)

def value_counts(self, dropna: bool = False):
def value_counts(self, dropna: bool = True):
from pandas.core.algorithms import value_counts

return value_counts(self.to_numpy(), dropna=dropna)
Expand Down