Skip to content

Commit f60d43e

Browse files
committed
Type more and fix some bugs
1 parent e5e87fe commit f60d43e

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

pandas/_libs/algos.pyx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,20 @@ def backfill_2d_inplace(algos_t[:, :] values,
833833

834834

835835
# Fillna logic
836+
# We have our own fused type since we don't need to support
837+
# types that can't hold NAs
838+
ctypedef fused fillna_t:
839+
float64_t
840+
float32_t
841+
object
842+
# TODO: Add datetime64 support through viewing data as uint64
843+
uint64_t # Datetime64
844+
# TODO: Complex support?
845+
846+
836847
@cython.boundscheck(False)
837848
@cython.wraparound(False)
838-
def fillna1d(ndarray arr,
849+
def fillna1d(fillna_t[:] arr,
839850
object value,
840851
limit=None,
841852
bint inf_as_na=False
@@ -856,15 +867,15 @@ def fillna1d(ndarray arr,
856867
arr : ndarray
857868
value : object
858869
The value to use to replace nans
859-
limit : int, default -1
860-
The number of elements to fill. If -1, fills all NaN values
870+
limit : int, default None
871+
The number of elements to fill. If None, fills all NaN values
861872
inf_as_na:
862873
Whether to consider INF and NEGINF as NA
863874
"""
864875
cdef:
865876
Py_ssize_t i, N, lim
866877
Py_ssize_t count=0
867-
object val
878+
fillna_t val
868879

869880
assert arr.ndim == 1, "'arr' must be 1-D."
870881

@@ -883,7 +894,7 @@ def fillna1d(ndarray arr,
883894

884895
@cython.boundscheck(False)
885896
@cython.wraparound(False)
886-
def fillna2d(ndarray arr,
897+
def fillna2d(fillna_t[:, :] arr,
887898
object value,
888899
limit=None,
889900
bint inf_as_na=False
@@ -904,25 +915,26 @@ def fillna2d(ndarray arr,
904915
arr : ndarray
905916
value : object
906917
The value to use to replace nans
907-
limit : int, default -1
908-
The number of elements to fill. If -1, fills all NaN values
918+
limit : int, default None
919+
The number of elements to fill. If None, fills all NaN values
909920
inf_as_na:
910921
Whether to consider INF and NEGINF as NA
911922
"""
912923
cdef:
913924
Py_ssize_t i, j, n, m, lim
914925
Py_ssize_t count=0
915-
object val
926+
fillna_t val
916927

917928
assert arr.ndim == 2, "'arr' must be 2-D."
918929

919930
n, m = (<object>arr).shape
920-
lim = validate_limit(n, limit)
931+
lim = validate_limit(m, limit)
921932
if inf_as_na:
922933
check_func = missing.checknull_old
923934
else:
924935
check_func = missing.checknull
925936
for i in range(n):
937+
count = 0 # Limit is per axis
926938
for j in range(m):
927939
val = arr[i, j]
928940
if check_func(val) and count < lim:

pandas/core/internals/blocks.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def fillna(
436436
"""
437437
inplace = validate_bool_kwarg(inplace, "inplace")
438438
arr = self if inplace else self.copy()
439+
limit = libalgos.validate_limit(None, limit=limit)
439440

440441
if not self._can_hold_na:
441442
return [arr]
@@ -461,37 +462,12 @@ def fillna(
461462
downcast=None,
462463
)
463464
else:
464-
# print("hello")
465+
# TODO: Verify that this works for EAs
465466
return [
466467
self.make_block_same_class(
467468
values=self.values.fillna(value=value, limit=limit)
468469
)
469470
]
470-
# mask = isna(self.values)
471-
# mask, noop = validate_putmask(self.values, mask)
472-
#
473-
# if limit is not None:
474-
# #limit = libalgos.validate_limit(None, limit=limit)
475-
# #mask[mask.cumsum(self.ndim - 1) > limit] = False
476-
#
477-
# if self._can_hold_element(value):
478-
# putmask_inplace(arr.values, mask, value)
479-
# return arr._maybe_downcast([arr], downcast)
480-
#
481-
# if noop:
482-
# # we can't process the value, but nothing to do
483-
# return arr
484-
#
485-
# elif self.ndim == 1 or self.shape[0] == 1:
486-
# blk = self.coerce_to_target_dtype(value)
487-
# # bc we have already cast, inplace=True may avoid an extra copy
488-
# return blk.fillna(value, limit=limit, inplace=True, downcast=None)
489-
#
490-
# else:
491-
# # operate column-by-column
492-
# return self.split_and_operate(
493-
# type(self).fillna, value, limit=limit, inplace=inplace, downcast=None
494-
# )
495471

496472
@final
497473
def _split(self) -> list[Block]:

0 commit comments

Comments
 (0)