From 57bbabc9090b5781a0524e3d62e14bbd20c72430 Mon Sep 17 00:00:00 2001 From: Nithurshen Date: Thu, 6 Nov 2025 07:05:36 +0530 Subject: [PATCH 1/2] TST: Add regression test for pivot on empty frame with period dtype --- pandas/tests/reshape/test_pivot.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 1bb4e35e041d0..17f1e41dd9b3a 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -2942,3 +2942,20 @@ def test_pivot_with_pyarrow_categorical(self): tm.assert_frame_equal( df, df_expected, check_dtype=False, check_column_type=False ) + + @pytest.mark.parametrize("freq", ["D", "M", "Q", "Y"]) + def test_pivot_empty_dataframe_period_dtype(self, freq): + # GH#62705 + + dtype = pd.PeriodDtype(freq=freq) + df = pd.DataFrame({"index": [], "columns": [], "values": []}) + df = df.astype({"values": dtype}) + result = df.pivot(index="index", columns="columns", values="values") + + expected_index = pd.Index([], name="index", dtype="float64") + expected_columns = pd.Index([], name="columns", dtype="float64") + expected = pd.DataFrame( + index=expected_index, columns=expected_columns, dtype=dtype + ) + + tm.assert_frame_equal(result, expected) From 9f69350eda3732570e43cd712b0d13571fe8645d Mon Sep 17 00:00:00 2001 From: Nithurshen Date: Thu, 6 Nov 2025 07:11:14 +0530 Subject: [PATCH 2/2] FIxing pre-commit check --- pandas/tests/reshape/test_pivot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 17f1e41dd9b3a..22a73ae1b5ddb 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -2948,13 +2948,13 @@ def test_pivot_empty_dataframe_period_dtype(self, freq): # GH#62705 dtype = pd.PeriodDtype(freq=freq) - df = pd.DataFrame({"index": [], "columns": [], "values": []}) + df = DataFrame({"index": [], "columns": [], "values": []}) df = df.astype({"values": dtype}) result = df.pivot(index="index", columns="columns", values="values") - expected_index = pd.Index([], name="index", dtype="float64") - expected_columns = pd.Index([], name="columns", dtype="float64") - expected = pd.DataFrame( + expected_index = Index([], name="index", dtype="float64") + expected_columns = Index([], name="columns", dtype="float64") + expected = DataFrame( index=expected_index, columns=expected_columns, dtype=dtype )