Skip to content

Commit 0b16108

Browse files
authored
REF: share fillna TDBlock/DTBlock (#39893)
1 parent b88660b commit 0b16108

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

pandas/core/internals/blocks.py

+16-21
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,21 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo
20482048
new_values = values.shift(periods, fill_value=fill_value, axis=axis)
20492049
return [self.make_block_same_class(new_values)]
20502050

2051+
def fillna(
2052+
self, value, limit=None, inplace: bool = False, downcast=None
2053+
) -> List[Block]:
2054+
2055+
if not self._can_hold_element(value) and self.dtype.kind != "m":
2056+
# We support filling a DatetimeTZ with a `value` whose timezone
2057+
# is different by coercing to object.
2058+
# TODO: don't special-case td64
2059+
return self.astype(object).fillna(value, limit, inplace, downcast)
2060+
2061+
values = self.array_values()
2062+
values = values if inplace else values.copy()
2063+
new_values = values.fillna(value=value, limit=limit)
2064+
return [self.make_block_same_class(values=new_values)]
2065+
20512066

20522067
class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
20532068
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
@@ -2134,6 +2149,7 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
21342149
fill_value = NaT
21352150
where = DatetimeBlock.where
21362151
putmask = DatetimeLikeBlockMixin.putmask
2152+
fillna = DatetimeLikeBlockMixin.fillna
21372153

21382154
array_values = ExtensionBlock.array_values
21392155

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

2175-
def fillna(
2176-
self, value, limit=None, inplace: bool = False, downcast=None
2177-
) -> List[Block]:
2178-
# We support filling a DatetimeTZ with a `value` whose timezone
2179-
# is different by coercing to object.
2180-
if self._can_hold_element(value):
2181-
return super().fillna(value, limit, inplace, downcast)
2182-
2183-
# different timezones, or a non-tz
2184-
return self.astype(object).fillna(
2185-
value, limit=limit, inplace=inplace, downcast=downcast
2186-
)
2187-
21882191

21892192
class TimeDeltaBlock(DatetimeLikeBlockMixin):
21902193
__slots__ = ()
@@ -2194,14 +2197,6 @@ class TimeDeltaBlock(DatetimeLikeBlockMixin):
21942197
fill_value = np.timedelta64("NaT", "ns")
21952198
_dtype = fill_value.dtype
21962199

2197-
def fillna(
2198-
self, value, limit=None, inplace: bool = False, downcast=None
2199-
) -> List[Block]:
2200-
values = self.array_values()
2201-
values = values if inplace else values.copy()
2202-
new_values = values.fillna(value=value, limit=limit)
2203-
return [self.make_block_same_class(values=new_values)]
2204-
22052200

22062201
class ObjectBlock(Block):
22072202
__slots__ = ()

0 commit comments

Comments
 (0)