8000 BUG: pivot_table not returning correct type when margin=True and aggfunc='mean' by mabelvj · Pull Request #28248 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: pivot_table not returning correct type when margin=True and aggfunc='mean' #28248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge branch 'master' into 24893-pivot_table
  • Loading branch information
mabelvj authored Nov 23, 2019
commit c957d01987bc8cce9a643f7b69ba4d1a0e8e8bc2
10 changes: 9 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,16 @@ Reshaping

- Bug in :meth:`DataFrame.apply` that caused incorrect output with empty :class:`DataFrame` (:issue:`28202`, :issue:`21959`)
- Bug in :meth:`DataFrame.stack` not handling non-unique indexes correctly when creating MultiIndex (:issue: `28301`)
-
- Bug in :meth:`pivot_table` not returning correct type ``float`` when ``margins=True`` and ``aggfunc='mean'`` (:issue:`24893`)
- Bug :func:`merge_asof` could not use :class:`datetime.timedelta` for ``tolerance`` kwarg (:issue:`28098`)
- Bug in :func:`merge`, did not append suffixes correctly with MultiIndex (:issue:`28518`)
- :func:`qcut` and :func:`cut` now handle boolean input (:issue:`20303`)
- Fix to ensure all int dtypes can be used in :func:`merge_asof` when using a tolerance value. Previously every non-int64 type would raise an erroneous ``MergeError`` (:issue:`28870`).
- Better error message in :func:`get_dummies` when `columns` isn't a list-like value (:issue:`28383`)
- Bug :meth:`Series.pct_change` where supplying an anchored frequency would throw a ValueError (:issue:`28664`)
- Bug where :meth:`DataFrame.equals` returned True incorrectly in some cases when two DataFrames had the same columns in different orders (:issue:`28839`)
- Bug in :meth:`DataFrame.replace` that caused non-numeric replacer's dtype not respected (:issue:`26632`)


Sparse
^^^^^^
Expand Down
45 changes: 45 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,51 @@ def test_margins_casted_to_float(self, observed):
)
tm.assert_frame_equal(result, expected)

def test_pivot_with_categorical(self, observed, ordered_fixture):
# gh-21370
idx = [np.nan, "low", "high", "low", np.nan]
col = [np.nan, "A", "B", np.nan, "A"]
df = pd.DataFrame(
{
"In": pd.Categorical(
idx, categories=["low", "high"], ordered=ordered_fixture
),
"Col": pd.Categorical(
col, categories=["A", "B"], ordered=ordered_fixture
),
"Val": range(1, 6),
}
)
# case with index/columns/value
result = df.pivot_table(
index="In", columns="Col", values="Val", observed=observed
)

expected_cols = pd.CategoricalIndex(
["A", "B"], ordered=ordered_fixture, name="Col"
)

expected = pd.DataFrame(
data=[[2.0, np.nan], [np.nan, 3.0]], columns=expected_cols
)
expected.index = Index(
pd.Categorical(
["low", "high"], categories=["low", "high"], ordered=ordered_fixture
),
name="In",
)

tm.assert_frame_equal(result, expected)

# case with columns/value
result = df.pivot_table(columns="Col", values="Val", observed=observed)

expected = pd.DataFrame(
data=[[3.5, 3.0]], columns=expected_cols, index=Index(["Val"])
)

tm.assert_frame_equal(result, expected)

def test_categorical_aggfunc(self, observed):
# GH 9534
df = pd.DataFrame(
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0