Skip to content

Commit 23e592f

Browse files
authored
BUG: chokes on pd.DatetimeTZDtype if there are no rows. (#59123)
BUG: `pivot_table` chokes on pd.DatetimeTZDtype if there are no rows. This is a follow up to #41875
1 parent a89f208 commit 23e592f

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

doc/source/whatsnew/v3.0.0.rst

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

600601
Sparse
601602
^^^^^^

pandas/core/reshape/reshape.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,19 @@ def get_new_values(self, values, fill_value=None):
288288

289289
dtype = values.dtype
290290

291-
# if our mask is all True, then we can use our existing dtype
292-
if mask_all:
293-
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)
291+
if isinstance(dtype, ExtensionDtype):
292+
# GH#41875
293+
# We are assuming that fill_value can be held by this dtype,
294+
# unlike the non-EA case that promotes.
295+
cls = dtype.construct_array_type()
296+
new_values = cls._empty(result_shape, dtype=dtype)
297+
if not mask_all:
302298
new_values[:] = fill_value
303-
else:
299+
else:
300+
if not mask_all:
304301
dtype, fill_value = maybe_promote(dtype, fill_value)
305-
new_values = np.empty(result_shape, dtype=dtype)
302+
new_values = np.empty(result_shape, dtype=dtype)
303+
if not mask_all:
306304
new_values.fill(fill_value)
307305

308306
name = dtype.name

pandas/tests/reshape/test_pivot.py

+14
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": Series([], dtype=pd.DatetimeTZDtype(tz="UTC")),
2778+
"category": Series([], dtype=str),
2779+
"value": 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)