Skip to content

Commit 2d7ab83

Browse files
committed
only replace float-nans with NaN and not other null objects
1 parent 50ac498 commit 2d7ab83

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pandas/_libs/missing.pyx

+25
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,31 @@ cdef bint checknull_with_nat_and_na(object obj):
248248
return checknull_with_nat(obj) or obj is C_NA
249249

250250

251+
@cython.wraparound(False)
252+
@cython.boundscheck(False)
253+
def is_float_nan(values: ndarray) -> ndarray:
254+
"""
255+
True for elements which correspond to a float nan
256+
257+
Returns
258+
-------
259+
ndarray[bool]
260+
"""
261+
cdef:
262+
ndarray[uint8_t] result
263+
Py_ssize_t i, N
264+
object val
265+
266+
N = len(values)
267+
result = np.zeros(N, dtype=np.uint8)
268+
269+
for i in range(N):
270+
val = values[i]
271+
if util.is_nan(val):
272+
result[i] = True
273+
return result.view(bool)
274+
275+
251276
@cython.wraparound(False)
252277
@cython.boundscheck(False)
253278
def is_numeric_na(values: ndarray) -> ndarray:

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
is_datetime_array,
3434
no_default,
3535
)
36+
from pandas._libs.missing import is_float_nan
3637
from pandas._libs.tslibs import (
3738
IncompatibleFrequency,
3839
OutOfBoundsDatetime,
@@ -1392,7 +1393,7 @@ def _format_with_header(self, header: list[str_t], na_rep: str_t) -> list[str_t]
13921393
result = [pprint_thing(x, escape_chars=("\t", "\r", "\n")) for x in values]
13931394

13941395
# could have nans
1395-
mask = isna(values)
1396+
mask = is_float_nan(values)
13961397
if mask.any():
13971398
result_arr = np.array(result)
13981399
result_arr[mask] = na_rep

0 commit comments

Comments
 (0)