Skip to content

BUG: chokes on pd.DatetimeTZDtype if there are no rows. #59123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -594,6 +594,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
Loading