Skip to content

Commit 09c0305

Browse files
committed
BUG: pivot_table chokes on pd.DatetimeTZDtype if there are no rows.
This is a follow up to pandas-dev#41875
1 parent 42082a8 commit 09c0305

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ Reshaping
594594
^^^^^^^^^
595595
- Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
596596
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
597+
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
597598

598599
Sparse
599600
^^^^^^

pandas/core/reshape/reshape.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,19 @@ def get_new_values(self, values, fill_value=None):
291291
# if our mask is all True, then we can use our existing dtype
292292
if mask_all:
293293
dtype = values.dtype
294-
new_values = np.empty(result_shape, dtype=dtype)
295-
else:
296-
if isinstance(dtype, ExtensionDtype):
297-
# GH#41875
298-
# We are assuming that fill_value can be held by this dtype,
299-
# unlike the non-EA case that promotes.
300-
cls = dtype.construct_array_type()
301-
new_values = cls._empty(result_shape, dtype=dtype)
294+
if isinstance(dtype, ExtensionDtype):
295+
# GH#41875
296+
# We are assuming that fill_value can be held by this dtype,
297+
# unlike the non-EA case that promotes.
298+
cls = dtype.construct_array_type()
299+
new_values = cls._empty(result_shape, dtype=dtype)
300+
if not mask_all:
302301
new_values[:] = fill_value
303-
else:
302+
else:
303+
if not mask_all:
304304
dtype, fill_value = maybe_promote(dtype, fill_value)
305-
new_values = np.empty(result_shape, dtype=dtype)
305+
new_values = np.empty(result_shape, dtype=dtype)
306+
if not mask_all:
306307
new_values.fill(fill_value)
307308

308309
name = dtype.name

pandas/tests/reshape/test_pivot.py

+15
Original file line numberDiff line numberDiff line change
@@ -2769,3 +2769,18 @@ def test_unstack_copy(self, m):
27692769
result = df.unstack(sort=False)
27702770
result.iloc[0, 0] = -1
27712771
tm.assert_frame_equal(df, df_orig)
2772+
2773+
@pytest.mark.parametrize("tz", [None, "UTC"])
2774+
def test_pivot_empty_with_datetime(self, tz):
2775+
# GH#59126
2776+
df = DataFrame(
2777+
{
2778+
"timestamp": pd.Series(None, pd.DatetimeTZDtype(tz="UTC")),
2779+
"category": pd.Series(None, dtype=str),
2780+
"value": pd.Series(None, dtype=str),
2781+
}
2782+
)
2783+
df_pivoted = df.pivot_table(
2784+
index="category", columns="value", values="timestamp"
2785+
)
2786+
assert df_pivoted.empty

0 commit comments

Comments
 (0)