Skip to content

BUG: crosstab fails with lists/tuples #44088

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 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ Reshaping
- Bug in :func:`concat` would fail when the ``objs`` argument all had the same index and the ``keys`` argument contained duplicates (:issue:`43595`)
- Bug in :func:`concat` which ignored the ``sort`` parameter (:issue:`43375`)
- Fixed bug in :func:`merge` with :class:`MultiIndex` as column index for the ``on`` argument returning an error when assigning a column internally (:issue:`43734`)
- Bug in :func:`crosstab` would fail when inputs are lists or tuples (:issue:`44076`)

Sparse
^^^^^^
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pandas.core.dtypes.common import (
is_integer_dtype,
is_list_like,
is_nested_list_like,
is_scalar,
)
from pandas.core.dtypes.generic import (
Expand Down Expand Up @@ -625,8 +626,10 @@ def crosstab(
if values is not None and aggfunc is None:
raise ValueError("values cannot be used without an aggfunc.")

index = com.maybe_make_list(index)
columns = com.maybe_make_list(columns)
if not is_nested_list_like(index):
index = [index]
if not is_nested_list_like(columns):
columns = [columns]

common_idx = None
pass_objs = [x for x in index + columns if isinstance(x, (ABCSeries, ABCDataFrame))]
Expand Down
18 changes: 11 additions & 7 deletions pandas/tests/reshape/test_crosstab.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ def test_crosstab_multiple(self):
expected = expected.unstack("A").fillna(0).astype(np.int64)
tm.assert_frame_equal(result, expected)

def test_crosstab_ndarray(self):
a = np.random.randint(0, 5, size=100)
b = np.random.randint(0, 3, size=100)
c = np.random.randint(0, 10, size=100)
@pytest.mark.parametrize("box", [np.array, list, tuple])
def test_crosstab_ndarray(self, box):
# GH 44076
a = box(np.random.randint(0, 5, size=100))
b = box(np.random.randint(0, 3, size=100))
c = box(np.random.randint(0, 10, size=100))

df = DataFrame({"a": a, "b": b, "c": c})

Expand All @@ -100,9 +102,11 @@ def test_crosstab_ndarray(self):
tm.assert_frame_equal(result, expected)

# assign arbitrary names
result = crosstab(self.df["A"].values, self.df["C"].values)
assert result.index.name == "row_0"
assert result.columns.name == "col_0"
result = crosstab(a, c)
expected = crosstab(df["a"], df["c"])
expected.index.names = ["row_0"]
expected.columns.names = ["col_0"]
tm.assert_frame_equal(result, expected)

def test_crosstab_non_aligned(self):
# GH 17005
Expand Down