Skip to content

BUG: Fix bug in reindexing of period columns after unstack #61114

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
Mar 17, 2025
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 @@ -699,6 +699,7 @@ Indexing
- Bug in :meth:`Index.get_indexer` and similar methods when ``NaN`` is located at or after position 128 (:issue:`58924`)
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)

Missing
^^^^^^^
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,9 @@ class Block(PandasObject, libinternals.Block):
def _validate_ndim(self) -> bool:
"""
We validate dimension for blocks that can hold 2D values, which for now
means numpy dtypes or DatetimeTZDtype.
means numpy dtypes or EA dtypes like DatetimeTZDtype and PeriodDtype.
"""
dtype = self.dtype
return not isinstance(dtype, ExtensionDtype) or isinstance(
dtype, DatetimeTZDtype
)
return not is_1d_only_ea_dtype(self.dtype)

@final
@cache_readonly
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,27 @@ def test_partial_boolean_frame_indexing(self):
)
tm.assert_frame_equal(result, expected)

def test_period_column_slicing(self):
# GH#60273 The transpose operation creates a single 5x1 block of PeriodDtype
# Make sure it is reindexed correctly
df = DataFrame(
pd.period_range("2021-01-01", periods=5, freq="D"),
columns=["A"],
).T
result = df[[0, 1, 2]]
expected = DataFrame(
[
[
pd.Period("2021-01-01", freq="D"),
pd.Period("2021-01-02", freq="D"),
pd.Period("2021-01-03", freq="D"),
]
],
index=["A"],
columns=[0, 1, 2],
)
tm.assert_frame_equal(result, expected)

def test_no_reference_cycle(self):
df = DataFrame({"a": [0, 1], "b": [2, 3]})
for name in ("loc", "iloc", "at", "iat"):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,16 @@ def test_period_can_hold_element(self, element):
with pytest.raises(TypeError, match="Invalid value"):
self.check_series_setitem(elem, pi, False)

def test_period_reindex_axis(self):
# GH#60273 Test reindexing of block with PeriodDtype
pi = period_range("2020", periods=5, freq="Y")
blk = new_block(pi._data.reshape(5, 1), BlockPlacement(slice(5)), ndim=2)
mgr = BlockManager(blocks=(blk,), axes=[Index(np.arange(5)), Index(["a"])])
reindexed = mgr.reindex_axis(Index([0, 2, 4]), axis=0)
result = DataFrame._from_mgr(reindexed, axes=reindexed.axes)
expected = DataFrame([[pi[0], pi[2], pi[4]]], columns=[0, 2, 4], index=["a"])
tm.assert_frame_equal(result, expected)

def check_can_hold_element(self, obj, elem, inplace: bool):
blk = obj._mgr.blocks[0]
if inplace:
Expand Down
Loading