From 8d4dcc288de69ef8d9dec4db1e50bce3f5514b06 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 1 Jun 2023 12:53:07 -0700 Subject: [PATCH 1/9] DEPR: pd.value_counts --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/__init__.py | 20 +++++++++++++++++++- pandas/core/arrays/string_.py | 2 +- pandas/tests/api/test_api.py | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7cc2e19f477dd..a138f54bbcbe7 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -266,6 +266,7 @@ Deprecations - Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`) - Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`) - Deprecated logical operation between two non boolean :class:`Series` with different indexes always coercing the result to bool dtype. In a future version, this will maintain the return type of the inputs. (:issue:`52500`, :issue:`52538`) +- Deprecated :func:`value_counts`, use ``pd.Series(obj).value_counts()`` instead (:issue:`47862`) - Deprecated allowing ``downcast`` keyword other than ``None``, ``False``, "infer", or a dict with these as values in :meth:`Series.fillna`, :meth:`DataFrame.fillna` (:issue:`40988`) - Deprecated allowing arbitrary ``fill_value`` in :class:`SparseDtype`, in a future version the ``fill_value`` will need to be compatible with the ``dtype.subtype``, either a scalar that can be held by that subtype or ``NaN`` for integer or bool subtypes (:issue:`23124`) - Deprecated behavior of :func:`assert_series_equal` and :func:`assert_frame_equal` considering NA-like values (e.g. ``NaN`` vs ``None`` as equivalent) (:issue:`52081`) diff --git a/pandas/__init__.py b/pandas/__init__.py index d11a429987ac4..4aaa3b392aba8 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -99,7 +99,6 @@ Grouper, factorize, unique, - value_counts, NamedAgg, array, Categorical, @@ -232,6 +231,25 @@ conversion, moving window statistics, date shifting and lagging. """ + +def __getattr__(name: str): + if name == "value_counts": + import warnings + from pandas.util._exceptions import find_stack_level + from pandas.core.algorithms import value_counts + + warnings.warn( + # GH#47862 + "pandas.value_counts is deprecated and will be removed in a future " + "version. Use pd.Series(obj).value_counts() instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + return value_counts + + raise AttributeError(f"module 'pandas' has no attribute '{name}'") + + # Use __all__ to let type checkers know what is part of the public API. # Pandas is not (yet) a py.typed library: the public API is determined # based on the documentation. diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index c9dc20cf93ddd..fd17ef6e94786 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -496,7 +496,7 @@ def max(self, axis=None, skipna: bool = True, **kwargs) -> Scalar: return self._wrap_reduction_result(axis, result) def value_counts(self, dropna: bool = True) -> Series: - from pandas import value_counts + from pandas.core.algorithms import value_counts result = value_counts(self._ndarray, dropna=dropna).astype("Int64") result.index = result.index.astype(self.dtype) diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index ffed6a0935c8d..094b3cb023961 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -210,6 +210,8 @@ def test_api(self): + self.funcs_to + self.private_modules ) + checkthese.remove("value_counts") + # value_counts is deprecated, is in __all__ but not dir() self.check(namespace=pd, expected=checkthese, ignored=self.ignored) def test_api_all(self): From ccace33ab30636c29e841bf23ea67af230c0fac8 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 2 Jun 2023 09:48:34 -0700 Subject: [PATCH 2/9] update docs --- doc/source/user_guide/basics.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/doc/source/user_guide/basics.rst b/doc/source/user_guide/basics.rst index a8afb05bc5200..44e643b07e354 100644 --- a/doc/source/user_guide/basics.rst +++ b/doc/source/user_guide/basics.rst @@ -675,7 +675,7 @@ matching index: Value counts (histogramming) / mode ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The :meth:`~Series.value_counts` Series method and top-level function computes a histogram +The :meth:`~Series.value_counts` Series method computes a histogram of a 1D array of values. It can also be used as a function on regular arrays: .. ipython:: python @@ -684,7 +684,6 @@ of a 1D array of values. It can also be used as a function on regular arrays: data s = pd.Series(data) s.value_counts() - pd.value_counts(data) The :meth:`~DataFrame.value_counts` method can be used to count combinations across multiple columns. By default all columns are used but a subset can be selected using the ``subset`` argument. @@ -733,7 +732,6 @@ normally distributed data into equal-size quartiles like so: arr = np.random.randn(30) factor = pd.qcut(arr, [0, 0.25, 0.5, 0.75, 1]) factor - pd.value_counts(factor) We can also pass infinite values to define the bins: From 2cc94583b0fc2c62a83757e23e4e72029fa7f904 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 2 Jun 2023 12:12:47 -0700 Subject: [PATCH 3/9] suppress warning --- doc/source/whatsnew/v0.19.1.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.19.1.rst b/doc/source/whatsnew/v0.19.1.rst index 6ff3fb6900a99..c590a8d969abf 100644 --- a/doc/source/whatsnew/v0.19.1.rst +++ b/doc/source/whatsnew/v0.19.1.rst @@ -7,6 +7,7 @@ Version 0.19.1 (November 3, 2016) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 From 93c1f33bd160b18a9f4d7e7281317232fda1aa6b Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 3 Jun 2023 13:18:33 -0700 Subject: [PATCH 4/9] suppress warning --- doc/source/whatsnew/v0.19.2.rst | 1 + doc/source/whatsnew/v0.20.2.rst | 1 + doc/source/whatsnew/v0.20.3.rst | 1 + doc/source/whatsnew/v0.21.1.rst | 1 + doc/source/whatsnew/v0.22.0.rst | 1 + 5 files changed, 5 insertions(+) diff --git a/doc/source/whatsnew/v0.19.2.rst b/doc/source/whatsnew/v0.19.2.rst index db9d9e65c923d..17f138755c132 100644 --- a/doc/source/whatsnew/v0.19.2.rst +++ b/doc/source/whatsnew/v0.19.2.rst @@ -7,6 +7,7 @@ Version 0.19.2 (December 24, 2016) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.20.2.rst b/doc/source/whatsnew/v0.20.2.rst index 430a39d2d2e97..f9264d375db29 100644 --- a/doc/source/whatsnew/v0.20.2.rst +++ b/doc/source/whatsnew/v0.20.2.rst @@ -7,6 +7,7 @@ Version 0.20.2 (June 4, 2017) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.20.3.rst b/doc/source/whatsnew/v0.20.3.rst index ff28f6830783e..b4c142d60346f 100644 --- a/doc/source/whatsnew/v0.20.3.rst +++ b/doc/source/whatsnew/v0.20.3.rst @@ -7,6 +7,7 @@ Version 0.20.3 (July 7, 2017) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.21.1.rst b/doc/source/whatsnew/v0.21.1.rst index e217e1a75efc5..c85f6916ca33b 100644 --- a/doc/source/whatsnew/v0.21.1.rst +++ b/doc/source/whatsnew/v0.21.1.rst @@ -7,6 +7,7 @@ Version 0.21.1 (December 12, 2017) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.22.0.rst b/doc/source/whatsnew/v0.22.0.rst index c494b4f286662..4f1e81307918b 100644 --- a/doc/source/whatsnew/v0.22.0.rst +++ b/doc/source/whatsnew/v0.22.0.rst @@ -7,6 +7,7 @@ Version 0.22.0 (December 29, 2017) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 From 9f514ef9dad4b94d38bbda6cf62f8a83d5acffec Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 5 Jun 2023 10:01:04 -0700 Subject: [PATCH 5/9] suppress warning --- doc/source/whatsnew/v0.21.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.21.0.rst b/doc/source/whatsnew/v0.21.0.rst index 1bbbbdc7e5410..acb8bdd7dfacc 100644 --- a/doc/source/whatsnew/v0.21.0.rst +++ b/doc/source/whatsnew/v0.21.0.rst @@ -7,6 +7,7 @@ Version 0.21.0 (October 27, 2017) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 From 12337fba73f31e3fb4ede43b887910f62eb7e5f6 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 5 Jun 2023 11:12:38 -0700 Subject: [PATCH 6/9] suppress warning --- doc/source/whatsnew/v0.23.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.23.0.rst b/doc/source/whatsnew/v0.23.0.rst index 5aba5ec061e8b..6551d862c285c 100644 --- a/doc/source/whatsnew/v0.23.0.rst +++ b/doc/source/whatsnew/v0.23.0.rst @@ -7,6 +7,7 @@ What's new in 0.23.0 (May 15, 2018) .. ipython:: python :suppress: + :okwarning: from pandas import * # noqa F401, F403 From 954ef426fd29403b926bd58474d9c661ba1dd100 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 5 Jun 2023 12:37:47 -0700 Subject: [PATCH 7/9] pyright ignore --- pandas/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/__init__.py b/pandas/__init__.py index 4aaa3b392aba8..9273c88b18f80 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -366,6 +366,6 @@ def __getattr__(name: str): "to_timedelta", "tseries", "unique", - "value_counts", + "value_counts", # pyright: ignore [reportUnsupportedDunderAll] "wide_to_long", ] From 7c372d2ac7be06c5ccf5caf5212673c6335d9bd5 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 5 Jun 2023 15:48:24 -0700 Subject: [PATCH 8/9] pylint ignore --- pandas/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/__init__.py b/pandas/__init__.py index 9273c88b18f80..b0835fd2f02e6 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -1,3 +1,4 @@ +# pylint: disable=undefined-all-variable from __future__ import annotations __docformat__ = "restructuredtext" From 3abe63fc6cf8bc1e2eadae4b97cdf4d3ad034be5 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 7 Jun 2023 11:17:14 -0700 Subject: [PATCH 9/9] warn inside value_counts --- doc/source/whatsnew/v0.19.1.rst | 1 - doc/source/whatsnew/v0.19.2.rst | 1 - doc/source/whatsnew/v0.20.2.rst | 1 - doc/source/whatsnew/v0.20.3.rst | 1 - doc/source/whatsnew/v0.21.0.rst | 1 - doc/source/whatsnew/v0.21.1.rst | 1 - doc/source/whatsnew/v0.22.0.rst | 1 - doc/source/whatsnew/v0.23.0.rst | 1 - pandas/__init__.py | 23 ++------------ pandas/core/algorithms.py | 29 +++++++++++++++-- pandas/core/arrays/_mixins.py | 2 +- pandas/core/arrays/interval.py | 2 +- pandas/core/arrays/string_.py | 2 +- pandas/core/base.py | 2 +- pandas/tests/api/test_api.py | 2 -- pandas/tests/extension/decimal/array.py | 3 +- pandas/tests/test_algos.py | 41 +++++++++++++++++-------- 17 files changed, 63 insertions(+), 51 deletions(-) diff --git a/doc/source/whatsnew/v0.19.1.rst b/doc/source/whatsnew/v0.19.1.rst index c590a8d969abf..6ff3fb6900a99 100644 --- a/doc/source/whatsnew/v0.19.1.rst +++ b/doc/source/whatsnew/v0.19.1.rst @@ -7,7 +7,6 @@ Version 0.19.1 (November 3, 2016) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.19.2.rst b/doc/source/whatsnew/v0.19.2.rst index 17f138755c132..db9d9e65c923d 100644 --- a/doc/source/whatsnew/v0.19.2.rst +++ b/doc/source/whatsnew/v0.19.2.rst @@ -7,7 +7,6 @@ Version 0.19.2 (December 24, 2016) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.20.2.rst b/doc/source/whatsnew/v0.20.2.rst index f9264d375db29..430a39d2d2e97 100644 --- a/doc/source/whatsnew/v0.20.2.rst +++ b/doc/source/whatsnew/v0.20.2.rst @@ -7,7 +7,6 @@ Version 0.20.2 (June 4, 2017) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.20.3.rst b/doc/source/whatsnew/v0.20.3.rst index b4c142d60346f..ff28f6830783e 100644 --- a/doc/source/whatsnew/v0.20.3.rst +++ b/doc/source/whatsnew/v0.20.3.rst @@ -7,7 +7,6 @@ Version 0.20.3 (July 7, 2017) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.21.0.rst b/doc/source/whatsnew/v0.21.0.rst index acb8bdd7dfacc..1bbbbdc7e5410 100644 --- a/doc/source/whatsnew/v0.21.0.rst +++ b/doc/source/whatsnew/v0.21.0.rst @@ -7,7 +7,6 @@ Version 0.21.0 (October 27, 2017) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.21.1.rst b/doc/source/whatsnew/v0.21.1.rst index c85f6916ca33b..e217e1a75efc5 100644 --- a/doc/source/whatsnew/v0.21.1.rst +++ b/doc/source/whatsnew/v0.21.1.rst @@ -7,7 +7,6 @@ Version 0.21.1 (December 12, 2017) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.22.0.rst b/doc/source/whatsnew/v0.22.0.rst index 4f1e81307918b..c494b4f286662 100644 --- a/doc/source/whatsnew/v0.22.0.rst +++ b/doc/source/whatsnew/v0.22.0.rst @@ -7,7 +7,6 @@ Version 0.22.0 (December 29, 2017) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/doc/source/whatsnew/v0.23.0.rst b/doc/source/whatsnew/v0.23.0.rst index 6551d862c285c..5aba5ec061e8b 100644 --- a/doc/source/whatsnew/v0.23.0.rst +++ b/doc/source/whatsnew/v0.23.0.rst @@ -7,7 +7,6 @@ What's new in 0.23.0 (May 15, 2018) .. ipython:: python :suppress: - :okwarning: from pandas import * # noqa F401, F403 diff --git a/pandas/__init__.py b/pandas/__init__.py index b0835fd2f02e6..d11a429987ac4 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -1,4 +1,3 @@ -# pylint: disable=undefined-all-variable from __future__ import annotations __docformat__ = "restructuredtext" @@ -100,6 +99,7 @@ Grouper, factorize, unique, + value_counts, NamedAgg, array, Categorical, @@ -232,25 +232,6 @@ conversion, moving window statistics, date shifting and lagging. """ - -def __getattr__(name: str): - if name == "value_counts": - import warnings - from pandas.util._exceptions import find_stack_level - from pandas.core.algorithms import value_counts - - warnings.warn( - # GH#47862 - "pandas.value_counts is deprecated and will be removed in a future " - "version. Use pd.Series(obj).value_counts() instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - return value_counts - - raise AttributeError(f"module 'pandas' has no attribute '{name}'") - - # Use __all__ to let type checkers know what is part of the public API. # Pandas is not (yet) a py.typed library: the public API is determined # based on the documentation. @@ -367,6 +348,6 @@ def __getattr__(name: str): "to_timedelta", "tseries", "unique", - "value_counts", # pyright: ignore [reportUnsupportedDunderAll] + "value_counts", "wide_to_long", ] diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 3a0fa1261701c..c3574829f9b0e 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -838,6 +838,31 @@ def value_counts( ------- Series """ + warnings.warn( + # GH#53493 + "pandas.value_counts is deprecated and will be removed in a " + "future version. Use pd.Series(obj).value_counts() instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + return value_counts_internal( + values, + sort=sort, + ascending=ascending, + normalize=normalize, + bins=bins, + dropna=dropna, + ) + + +def value_counts_internal( + values, + sort: bool = True, + ascending: bool = False, + normalize: bool = False, + bins=None, + dropna: bool = True, +) -> Series: from pandas import ( Index, Series, @@ -1678,8 +1703,8 @@ def union_with_duplicates( """ from pandas import Series - l_count = value_counts(lvals, dropna=False) - r_count = value_counts(rvals, dropna=False) + l_count = value_counts_internal(lvals, dropna=False) + r_count = value_counts_internal(rvals, dropna=False) l_count, r_count = l_count.align(r_count, fill_value=0) final_count = np.maximum(l_count.values, r_count.values) final_count = Series(final_count, index=l_count.index, dtype="int", copy=False) diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index d93ecc087844c..0100c17805d76 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -48,7 +48,7 @@ from pandas.core.algorithms import ( take, unique, - value_counts, + value_counts_internal as value_counts, ) from pandas.core.array_algos.quantile import quantile_with_mask from pandas.core.array_algos.transforms import shift diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 2842d8267b7c6..7f874a07341eb 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -79,7 +79,7 @@ isin, take, unique, - value_counts, + value_counts_internal as value_counts, ) from pandas.core.arrays.base import ( ExtensionArray, diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index 483d81b7215f0..a6579879cab96 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -496,7 +496,7 @@ def max(self, axis=None, skipna: bool = True, **kwargs) -> Scalar: return self._wrap_reduction_result(axis, result) def value_counts(self, dropna: bool = True) -> Series: - from pandas.core.algorithms import value_counts + from pandas.core.algorithms import value_counts_internal as value_counts result = value_counts(self._ndarray, dropna=dropna).astype("Int64") result.index = result.index.astype(self.dtype) diff --git a/pandas/core/base.py b/pandas/core/base.py index f66abaa17d8a7..d4a808f4d7dd1 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -958,7 +958,7 @@ def value_counts( NaN 1 Name: count, dtype: int64 """ - return algorithms.value_counts( + return algorithms.value_counts_internal( self, sort=sort, ascending=ascending, diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 094b3cb023961..ffed6a0935c8d 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -210,8 +210,6 @@ def test_api(self): + self.funcs_to + self.private_modules ) - checkthese.remove("value_counts") - # value_counts is deprecated, is in __all__ but not dir() self.check(namespace=pd, expected=checkthese, ignored=self.ignored) def test_api_all(self): diff --git a/pandas/tests/extension/decimal/array.py b/pandas/tests/extension/decimal/array.py index 3e495e9ac6814..393c01488c234 100644 --- a/pandas/tests/extension/decimal/array.py +++ b/pandas/tests/extension/decimal/array.py @@ -25,6 +25,7 @@ is_scalar, ) from pandas.core import arraylike +from pandas.core.algorithms import value_counts_internal as value_counts from pandas.core.arraylike import OpsMixin from pandas.core.arrays import ( ExtensionArray, @@ -273,8 +274,6 @@ def convert_values(param): return np.asarray(res, dtype=bool) def value_counts(self, dropna: bool = True): - from pandas.core.algorithms import value_counts - return value_counts(self.to_numpy(), dropna=dropna) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 6d09488df06e2..86142d9fe4d95 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1181,7 +1181,9 @@ def test_value_counts(self): factor = cut(arr, 4) # assert isinstance(factor, n) - result = algos.value_counts(factor) + msg = "pandas.value_counts is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = algos.value_counts(factor) breaks = [-1.194, -0.535, 0.121, 0.777, 1.433] index = IntervalIndex.from_breaks(breaks).astype(CDT(ordered=True)) expected = Series([1, 1, 1, 1], index=index, name="count") @@ -1189,13 +1191,16 @@ def test_value_counts(self): def test_value_counts_bins(self): s = [1, 2, 3, 4] - result = algos.value_counts(s, bins=1) + msg = "pandas.value_counts is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = algos.value_counts(s, bins=1) expected = Series( [4], index=IntervalIndex.from_tuples([(0.996, 4.0)]), name="count" ) tm.assert_series_equal(result, expected) - result = algos.value_counts(s, bins=2, sort=False) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = algos.value_counts(s, bins=2, sort=False) expected = Series( [2, 2], index=IntervalIndex.from_tuples([(0.996, 2.5), (2.5, 4.0)]), @@ -1204,31 +1209,40 @@ def test_value_counts_bins(self): tm.assert_series_equal(result, expected) def test_value_counts_dtypes(self): - result = algos.value_counts(np.array([1, 1.0])) + msg2 = "pandas.value_counts is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg2): + result = algos.value_counts(np.array([1, 1.0])) assert len(result) == 1 - result = algos.value_counts(np.array([1, 1.0]), bins=1) + with tm.assert_produces_warning(FutureWarning, match=msg2): + result = algos.value_counts(np.array([1, 1.0]), bins=1) assert len(result) == 1 - result = algos.value_counts(Series([1, 1.0, "1"])) # object + with tm.assert_produces_warning(FutureWarning, match=msg2): + result = algos.value_counts(Series([1, 1.0, "1"])) # object assert len(result) == 2 msg = "bins argument only works with numeric data" with pytest.raises(TypeError, match=msg): - algos.value_counts(np.array(["1", 1], dtype=object), bins=1) + with tm.assert_produces_warning(FutureWarning, match=msg2): + algos.value_counts(np.array(["1", 1], dtype=object), bins=1) def test_value_counts_nat(self): td = Series([np.timedelta64(10000), NaT], dtype="timedelta64[ns]") dt = to_datetime(["NaT", "2014-01-01"]) + msg = "pandas.value_counts is deprecated" + for s in [td, dt]: - vc = algos.value_counts(s) - vc_with_na = algos.value_counts(s, dropna=False) + with tm.assert_produces_warning(FutureWarning, match=msg): + vc = algos.value_counts(s) + vc_with_na = algos.value_counts(s, dropna=False) assert len(vc) == 1 assert len(vc_with_na) == 2 exp_dt = Series({Timestamp("2014-01-01 00:00:00"): 1}, name="count") - tm.assert_series_equal(algos.value_counts(dt), exp_dt) + with tm.assert_produces_warning(FutureWarning, match=msg): + tm.assert_series_equal(algos.value_counts(dt), exp_dt) # TODO same for (timedelta) def test_value_counts_datetime_outofbounds(self): @@ -1388,13 +1402,16 @@ def test_value_counts_normalized(self, dtype): def test_value_counts_uint64(self): arr = np.array([2**63], dtype=np.uint64) expected = Series([1], index=[2**63], name="count") - result = algos.value_counts(arr) + msg = "pandas.value_counts is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = algos.value_counts(arr) tm.assert_series_equal(result, expected) arr = np.array([-1, 2**63], dtype=object) expected = Series([1, 1], index=[-1, 2**63], name="count") - result = algos.value_counts(arr) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = algos.value_counts(arr) tm.assert_series_equal(result, expected)