Skip to content

Commit e1c304f

Browse files
committed
modified: pandas/tests/reshape/test_pivot.py
- Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue pandas-dev#57876.
1 parent 2a4bda7 commit e1c304f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pandas/tests/reshape/test_pivot.py

+32
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,38 @@ def test_pivot_table_index_and_column_keys_with_nan(self, dropna):
25542554

25552555
tm.assert_frame_equal(left=result, right=expected)
25562556

2557+
@pytest.mark.parametrize(
2558+
argnames=["index", "columns"],
2559+
argvalues=[("Category", "Value"), ("Value", "Category")],
2560+
ids=["values-and-columns", "values-and-index"],
2561+
)
2562+
def test_pivot_table_values_as_two_params(self, index, columns, request):
2563+
data = {"Category": ["A", "B", "A", "B"], "Value": [10, 20, 40, 50]}
2564+
df = DataFrame(data)
2565+
result = df.pivot_table(
2566+
index=index, columns=columns, values="Value", aggfunc="count"
2567+
)
2568+
nan = np.nan
2569+
cat_index = Index(data=["A", "B"], name="Category")
2570+
val_index = Index(data=[10, 20, 40, 50], name="Value")
2571+
if request.node.callspec.id == "values-and-columns":
2572+
e_data = [
2573+
[1.0, nan, 1.0, nan],
2574+
[nan, 1.0, nan, 1.0],
2575+
]
2576+
expected = DataFrame(data=e_data, index=cat_index, columns=val_index)
2577+
2578+
else:
2579+
e_data = [
2580+
[1.0, nan],
2581+
[nan, 1.0],
2582+
[1.0, nan],
2583+
[nan, 1.0],
2584+
]
2585+
expected = DataFrame(data=e_data, index=val_index, columns=cat_index)
2586+
2587+
tm.assert_frame_equal(left=result, right=expected)
2588+
25572589

25582590
class TestPivot:
25592591
def test_pivot(self):

0 commit comments

Comments
 (0)