Skip to content

Commit f0202bf

Browse files
phoflyeshsurya
authored andcommitted
Add keyword sort to pivot_table (pandas-dev#40954)
1 parent 5b8f15e commit f0202bf

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ Other enhancements
221221
- :meth:`pandas.read_csv` and :meth:`pandas.read_json` expose the argument ``encoding_errors`` to control how encoding errors are handled (:issue:`39450`)
222222
- :meth:`.GroupBy.any` and :meth:`.GroupBy.all` use Kleene logic with nullable data types (:issue:`37506`)
223223
- :meth:`.GroupBy.any` and :meth:`.GroupBy.all` return a ``BooleanDtype`` for columns with nullable data types (:issue:`33449`)
224+
- Add keyword ``sort`` to :func:`pivot_table` to allow non-sorting of the result (:issue:`39143`)
224225
-
225226

226227
.. ---------------------------------------------------------------------------

pandas/core/reshape/pivot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def pivot_table(
6464
dropna=True,
6565
margins_name="All",
6666
observed=False,
67+
sort=True,
6768
) -> DataFrame:
6869
index = _convert_by(index)
6970
columns = _convert_by(columns)
@@ -83,6 +84,7 @@ def pivot_table(
8384
dropna=dropna,
8485
margins_name=margins_name,
8586
observed=observed,
87+
sort=sort,
8688
)
8789
pieces.append(_table)
8890
keys.append(getattr(func, "__name__", func))
@@ -101,6 +103,7 @@ def pivot_table(
101103
dropna,
102104
margins_name,
103105
observed,
106+
sort,
104107
)
105108
return table.__finalize__(data, method="pivot_table")
106109

@@ -116,6 +119,7 @@ def __internal_pivot_table(
116119
dropna: bool,
117120
margins_name: str,
118121
observed: bool,
122+
sort: bool,
119123
) -> DataFrame:
120124
"""
121125
Helper of :func:`pandas.pivot_table` for any non-list ``aggfunc``.
@@ -157,7 +161,7 @@ def __internal_pivot_table(
157161
pass
158162
values = list(values)
159163

160-
grouped = data.groupby(keys, observed=observed)
164+
grouped = data.groupby(keys, observed=observed, sort=sort)
161165
agged = grouped.agg(aggfunc)
162166
if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
163167
agged = agged.dropna(how="all")

0 commit comments

Comments
 (0)