Skip to content

Add base test for Series[ExtensionArray].value_counts with sort=False #23074

Open
@TomAugspurger

Description

@TomAugspurger

I had a bug in my PeriodArray.sort_values implementation w.r.t sorting.

Here's a generic test that would have caught it.

diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py
index 4e7886dd2..4550817f0 100644
--- a/pandas/tests/extension/base/methods.py
+++ b/pandas/tests/extension/base/methods.py
@@ -24,6 +24,14 @@ class BaseMethodsTests(BaseExtensionTests):
 
         self.assert_series_equal(result, expected)
 
+    def test_value_counts_no_sort(self, data_for_grouping):
+        # BB..AABC -> BCCCBA
+        data = data_for_grouping.take([0, 7, 7, 7, 0, 4])
+        # B is the second most common, but should come first with sort=False
+        result = pd.Series(data).value_counts(sort=False)
+        expected = pd.Series([1, 2, 3], data_for_grouping.take([4, 0, 7]))
+        self.assert_series_equal(result, expected)
+
     def test_count(self, data_missing):
         df = pd.DataFrame({"A": data_missing})
         result = df.count(axis='columns')

We could add that to base/methods.py. It may have to be skipped for some like Categorical, since they return a CategoricalIndex, and this would build an object index.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffExtensionArrayExtending pandas with custom dtypes or arrays.Testingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions