Skip to content

Commit

Permalink
BUG: chokes on pd.DatetimeTZDtype if there are no rows. (#59123)
Browse files Browse the repository at this point in the history
BUG: `pivot_table` chokes on pd.DatetimeTZDtype if there are no rows.

This is a follow up to #41875
  • Loading branch information
pcorpet authored Jun 29, 2024
1 parent a89f208 commit 23e592f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ Reshaping
^^^^^^^^^
- Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)

Sparse
^^^^^^
Expand Down
24 changes: 11 additions & 13 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,19 @@ def get_new_values(self, values, fill_value=None):

dtype = values.dtype

# if our mask is all True, then we can use our existing dtype
if mask_all:
dtype = values.dtype
new_values = np.empty(result_shape, dtype=dtype)
else:
if isinstance(dtype, ExtensionDtype):
# GH#41875
# We are assuming that fill_value can be held by this dtype,
# unlike the non-EA case that promotes.
cls = dtype.construct_array_type()
new_values = cls._empty(result_shape, dtype=dtype)
if isinstance(dtype, ExtensionDtype):
# GH#41875
# We are assuming that fill_value can be held by this dtype,
# unlike the non-EA case that promotes.
cls = dtype.construct_array_type()
new_values = cls._empty(result_shape, dtype=dtype)
if not mask_all:
new_values[:] = fill_value
else:
else:
if not mask_all:
dtype, fill_value = maybe_promote(dtype, fill_value)
new_values = np.empty(result_shape, dtype=dtype)
new_values = np.empty(result_shape, dtype=dtype)
if not mask_all:
new_values.fill(fill_value)

name = dtype.name
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2769,3 +2769,17 @@ def test_unstack_copy(self, m):
result = df.unstack(sort=False)
result.iloc[0, 0] = -1
tm.assert_frame_equal(df, df_orig)

def test_pivot_empty_with_datetime(self):
# GH#59126
df = DataFrame(
{
"timestamp": Series([], dtype=pd.DatetimeTZDtype(tz="UTC")),
"category": Series([], dtype=str),
"value": Series([], dtype=str),
}
)
df_pivoted = df.pivot_table(
index="category", columns="value", values="timestamp"
)
assert df_pivoted.empty

0 comments on commit 23e592f

Please sign in to comment.