Skip to content

Commit

Permalink
Add keyword sort to pivot_table (pandas-dev#40954)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored and yeshsurya committed May 6, 2021
1 parent 5b8f15e commit f0202bf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Other enhancements
- :meth:`pandas.read_csv` and :meth:`pandas.read_json` expose the argument ``encoding_errors`` to control how encoding errors are handled (:issue:`39450`)
- :meth:`.GroupBy.any` and :meth:`.GroupBy.all` use Kleene logic with nullable data types (:issue:`37506`)
- :meth:`.GroupBy.any` and :meth:`.GroupBy.all` return a ``BooleanDtype`` for columns with nullable data types (:issue:`33449`)
- Add keyword ``sort`` to :func:`pivot_table` to allow non-sorting of the result (:issue:`39143`)
-

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def pivot_table(
dropna=True,
margins_name="All",
observed=False,
sort=True,
) -> DataFrame:
index = _convert_by(index)
columns = _convert_by(columns)
Expand All @@ -83,6 +84,7 @@ def pivot_table(
dropna=dropna,
margins_name=margins_name,
observed=observed,
sort=sort,
)
pieces.append(_table)
keys.append(getattr(func, "__name__", func))
Expand All @@ -101,6 +103,7 @@ def pivot_table(
dropna,
margins_name,
observed,
sort,
)
return table.__finalize__(data, method="pivot_table")

Expand All @@ -116,6 +119,7 @@ def __internal_pivot_table(
dropna: bool,
margins_name: str,
observed: bool,
sort: bool,
) -> DataFrame:
"""
Helper of :func:`pandas.pivot_table` for any non-list ``aggfunc``.
Expand Down Expand Up @@ -157,7 +161,7 @@ def __internal_pivot_table(
pass
values = list(values)

grouped = data.groupby(keys, observed=observed)
grouped = data.groupby(keys, observed=observed, sort=sort)
agged = grouped.agg(aggfunc)
if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
agged = agged.dropna(how="all")
Expand Down

0 comments on commit f0202bf

Please sign in to comment.