Skip to content

Commit a4b9da7

Browse files
authored
BUG: crosstab fails with lists/tuples (#44088)
1 parent 58de58f commit a4b9da7

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ Reshaping
573573
- Bug in :func:`concat` would fail when the ``objs`` argument all had the same index and the ``keys`` argument contained duplicates (:issue:`43595`)
574574
- Bug in :func:`concat` which ignored the ``sort`` parameter (:issue:`43375`)
575575
- 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`)
576+
- Bug in :func:`crosstab` would fail when inputs are lists or tuples (:issue:`44076`)
576577

577578
Sparse
578579
^^^^^^

pandas/core/reshape/pivot.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pandas.core.dtypes.common import (
2626
is_integer_dtype,
2727
is_list_like,
28+
is_nested_list_like,
2829
is_scalar,
2930
)
3031
from pandas.core.dtypes.generic import (
@@ -625,8 +626,10 @@ def crosstab(
625626
if values is not None and aggfunc is None:
626627
raise ValueError("values cannot be used without an aggfunc.")
627628

628-
index = com.maybe_make_list(index)
629-
columns = com.maybe_make_list(columns)
629+
if not is_nested_list_like(index):
630+
index = [index]
631+
if not is_nested_list_like(columns):
632+
columns = [columns]
630633

631634
common_idx = None
632635
pass_objs = [x for x in index + columns if isinstance(x, (ABCSeries, ABCDataFrame))]

pandas/tests/reshape/test_crosstab.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ def test_crosstab_multiple(self):
8484
expected = expected.unstack("A").fillna(0).astype(np.int64)
8585
tm.assert_frame_equal(result, expected)
8686

87-
def test_crosstab_ndarray(self):
88-
a = np.random.randint(0, 5, size=100)
89-
b = np.random.randint(0, 3, size=100)
90-
c = np.random.randint(0, 10, size=100)
87+
@pytest.mark.parametrize("box", [np.array, list, tuple])
88+
def test_crosstab_ndarray(self, box):
89+
# GH 44076
90+
a = box(np.random.randint(0, 5, size=100))
91+
b = box(np.random.randint(0, 3, size=100))
92+
c = box(np.random.randint(0, 10, size=100))
9193

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

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

102104
# assign arbitrary names
103-
result = crosstab(self.df["A"].values, self.df["C"].values)
104-
assert result.index.name == "row_0"
105-
assert result.columns.name == "col_0"
105+
result = crosstab(a, c)
106+
expected = crosstab(df["a"], df["c"])
107+
expected.index.names = ["row_0"]
108+
expected.columns.names = ["col_0"]
109+
tm.assert_frame_equal(result, expected)
106110

107111
def test_crosstab_non_aligned(self):
108112
# GH 17005

0 commit comments

Comments
 (0)