Skip to content

Commit c00493c

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

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 10 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,3 +2769,17 @@ 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+
def test_pivot_empty_with_datetime(self):
2774+
# GH#59126
2775+
df = DataFrame(
2776+
{
2777+
"timestamp": pd.Series([], pd.DatetimeTZDtype(tz="UTC")),
2778+
"category": pd.Series([], dtype=str),
2779+
"value": pd.Series([], dtype=str),
2780+
}
2781+
)
2782+
df_pivoted = df.pivot_table(
2783+
index="category", columns="value", values="timestamp"
2784+
)
2785+
assert df_pivoted.empty

0 commit comments

Comments
 (0)