Skip to content

Commit 055562c

Browse files
committed
Docs
1 parent 1290366 commit 055562c

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,67 @@ In cases with mixed-resolution inputs, the highest resolution is used:
201201
In [2]: pd.to_datetime([pd.Timestamp("2024-03-22 11:43:01"), "2024-03-22 11:43:01.002"]).dtype
202202
Out[2]: dtype('<M8[ns]')
203203
204+
.. _whatsnew_300.api_breaking.value_counts_sorting:
205+
206+
Changed behavior in ``value_counts`` when ``sort=False``
207+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
208+
209+
In previous versions of pandas, :meth:`DataFrame.value_counts` with ``sort=False`` would sort the result by column label (as was documented). This was nonintuitive and inconsistent with :meth:`Series.value_counts` which would maintain the order of the input. Now :meth:`DataFrame.value_counts` will maintain the order of the input.
210+
211+
.. ipython:: python
212+
213+
df = pd.DataFrame(
214+
{
215+
"a": [2, 2, 2, 2, 1, 1, 1, 1],
216+
"b": [2, 1, 3, 1, 2, 3, 1, 1],
217+
}
218+
)
219+
df
220+
221+
*Old behavior*
222+
223+
.. code-block:: ipython
224+
225+
In [3]: df.value_counts(sort=False)
226+
Out[3]:
227+
a b
228+
1 1 2
229+
2 1
230+
3 1
231+
2 1 2
232+
2 1
233+
3 1
234+
Name: count, dtype: int64
235+
236+
*New behavior*
237+
238+
.. ipython:: python
239+
240+
df.value_counts(sort=False)
241+
242+
This change also applies to :meth:`.DataFrameGroupBy.value_counts`. Here, there are two options for sorting: one ``sort`` passed to :meth:`DataFrame.groupby` and one passed directly to :meth:`.DataFrameGroupBy.value_counts`. The former will determine whether to sort the groups, the latter whether to sort the counts. All non-grouping columns will maintain the order of the input *within groups*.
243+
244+
*Old behavior*
245+
246+
.. code-block:: ipython
247+
248+
In [5]: df.groupby("a", sort=True).value_counts(sort=False)
249+
Out[5]:
250+
a b
251+
1 1 2
252+
2 1
253+
3 1
254+
2 1 2
255+
2 1
256+
3 1
257+
dtype: int64
258+
259+
*New behavior*
260+
261+
.. ipython:: python
262+
263+
df.groupby("a", sort=True).value_counts(sort=False)
264+
204265
.. _whatsnew_300.api_breaking.deps:
205266

206267
Increased minimum version for Python

pandas/core/frame.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7264,7 +7264,11 @@ def value_counts(
72647264
normalize : bool, default False
72657265
Return proportions rather than frequencies.
72667266
sort : bool, default True
7267-
Sort by frequencies when True. Sort by DataFrame column values when False.
7267+
Sort by frequencies when True. Preserve the order of the data when False.
7268+
7269+
.. versionchanged:: 3.0.0
7270+
7271+
Prior to 3.0.0, ``sort=False`` would sort by the columns values.
72687272
ascending : bool, default False
72697273
Sort in ascending order.
72707274
dropna : bool, default True

pandas/core/groupby/generic.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,13 @@ def value_counts(
23032303
normalize : bool, default False
23042304
Return proportions rather than frequencies.
23052305
sort : bool, default True
2306-
Sort by frequencies.
2306+
Sort by frequencies when True. When False, non-grouping columns will appear
2307+
in the order they occur in within groups.
2308+
2309+
.. versionchanged:: 3.0.0
2310+
2311+
In prior versions, ``sort=False`` would sort the non-grouping columns
2312+
by label.
23072313
ascending : bool, default False
23082314
Sort in ascending order.
23092315
dropna : bool, default True

0 commit comments

Comments
 (0)