Skip to content

REF: share fillna TDBlock/DTBlock #39893

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
Feb 19, 2021
Merged
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
37 changes: 16 additions & 21 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,21 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo
new_values = values.shift(periods, fill_value=fill_value, axis=axis)
return [self.make_block_same_class(new_values)]

def fillna(
self, value, limit=None, inplace: bool = False, downcast=None
) -> List[Block]:

if not self._can_hold_element(value) and self.dtype.kind != "m":
# We support filling a DatetimeTZ with a `value` whose timezone
# is different by coercing to object.
# TODO: don't special-case td64
return self.astype(object).fillna(value, limit, inplace, downcast)

values = self.array_values()
values = values if inplace else values.copy()
new_values = values.fillna(value=value, limit=limit)
return [self.make_block_same_class(values=new_values)]


class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
Expand Down Expand Up @@ -2134,6 +2149,7 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
fill_value = NaT
where = DatetimeBlock.where
putmask = DatetimeLikeBlockMixin.putmask
fillna = DatetimeLikeBlockMixin.fillna

array_values = ExtensionBlock.array_values

Expand Down Expand Up @@ -2172,19 +2188,6 @@ def external_values(self):
# Avoid FutureWarning in .astype in casting from dt64tz to dt64
return self.values._data

def fillna(
self, value, limit=None, inplace: bool = False, downcast=None
) -> List[Block]:
# We support filling a DatetimeTZ with a `value` whose timezone
# is different by coercing to object.
if self._can_hold_element(value):
return super().fillna(value, limit, inplace, downcast)

# different timezones, or a non-tz
return self.astype(object).fillna(
value, limit=limit, inplace=inplace, downcast=downcast
)


class TimeDeltaBlock(DatetimeLikeBlockMixin):
__slots__ = ()
Expand All @@ -2194,14 +2197,6 @@ class TimeDeltaBlock(DatetimeLikeBlockMixin):
fill_value = np.timedelta64("NaT", "ns")
_dtype = fill_value.dtype

def fillna(
self, value, limit=None, inplace: bool = False, downcast=None
) -> List[Block]:
values = self.array_values()
values = values if inplace else values.copy()
new_values = values.fillna(value=value, limit=limit)
return [self.make_block_same_class(values=new_values)]


class ObjectBlock(Block):
__slots__ = ()
Expand Down