Skip to content

BUG: df.replace over pd.Period columns (#34871) #36867

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 23 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
17 changes: 16 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pandas._libs.internals as libinternals
from pandas._libs.internals import BlockPlacement
from pandas._libs.tslibs import conversion
from pandas._libs.tslibs.period import Period
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import ArrayLike, Scalar
from pandas.util._validators import validate_bool_kwarg
Expand Down Expand Up @@ -1975,6 +1976,18 @@ def external_values(self):
return self.values.astype(object)


class PeriodExtensionBlock(ObjectValuesExtensionBlock):
"""
Used by PeriodArray to ensure proper type conversions
"""

def _can_hold_element(self, element: Any) -> bool:
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
return issubclass(tipo.type, Period)
return isinstance(element, Period)


class NumericBlock(Block):
__slots__ = ()
is_numeric = True
Expand Down Expand Up @@ -2747,8 +2760,10 @@ def get_block_type(values, dtype=None):
cls = DatetimeBlock
elif is_datetime64tz_dtype(values.dtype):
cls = DatetimeTZBlock
elif is_interval_dtype(dtype) or is_period_dtype(dtype):
elif is_interval_dtype(dtype):
cls = ObjectValuesExtensionBlock
elif is_period_dtype(dtype):
cls = PeriodExtensionBlock
elif is_extension_array_dtype(values.dtype):
cls = ExtensionBlock
elif issubclass(vtype, np.floating):
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
DatetimeTZBlock,
ExtensionBlock,
ObjectValuesExtensionBlock,
PeriodExtensionBlock,
extend_blocks,
get_block_type,
make_block,
Expand Down Expand Up @@ -1743,6 +1744,14 @@ def form_blocks(arrays, names: Index, axes) -> List[Block]:

blocks.extend(external_blocks)

if len(items_dict["PeriodExtensionBlock"]):
external_blocks = [
make_block(array, klass=PeriodExtensionBlock, placement=i)
for i, _, array in items_dict["PeriodExtensionBlock"]
]

blocks.extend(external_blocks)

if len(extra_locs):
shape = (len(extra_locs),) + tuple(len(x) for x in axes[1:])

Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,9 +1517,6 @@ def test_replace_with_duplicate_columns(self, replacement):

tm.assert_frame_equal(result, expected)

@pytest.mark.xfail(
reason="replace() changes dtype from period to object, see GH34871", strict=True
)
def test_replace_period_ignore_float(self):
"""
Regression test for GH#34871: if df.replace(1.0, 0.0) is called on a df
Expand Down