Skip to content

Commit 943c3cb

Browse files
authored
BUG: repr of inf values with use_inf_as_na (#55483)
* BUG: repr of inf values with use_inf_as_na * GH ref
1 parent 6786846 commit 943c3cb

File tree

6 files changed

+14
-50
lines changed

6 files changed

+14
-50
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Other
394394
^^^^^
395395
- Bug in :func:`cut` incorrectly allowing cutting of timezone-aware datetimes with timezone-naive bins (:issue:`54964`)
396396
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`)
397+
- Bug in rendering ``inf`` values inside a a :class:`DataFrame` with the ``use_inf_as_na`` option enabled (:issue:`55483`)
397398
- Bug in rendering a :class:`Series` with a :class:`MultiIndex` when one of the index level's names is 0 not having that name displayed (:issue:`55415`)
398399
-
399400

pandas/_libs/missing.pyi

-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ def isneginf_scalar(val: object) -> bool: ...
1414
def checknull(val: object, inf_as_na: bool = ...) -> bool: ...
1515
def isnaobj(arr: np.ndarray, inf_as_na: bool = ...) -> npt.NDArray[np.bool_]: ...
1616
def is_numeric_na(values: np.ndarray) -> npt.NDArray[np.bool_]: ...
17-
def is_float_nan(values: np.ndarray) -> npt.NDArray[np.bool_]: ...

pandas/_libs/missing.pyx

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

257257

258-
@cython.wraparound(False)
259-
@cython.boundscheck(False)
260-
def is_float_nan(values: ndarray) -> ndarray:
261-
"""
262-
True for elements which correspond to a float nan
263-
264-
Returns
265-
-------
266-
ndarray[bool]
267-
"""
268-
cdef:
269-
ndarray[uint8_t] result
270-
Py_ssize_t i, N
271-
object val
272-
273-
N = len(values)
274-
result = np.zeros(N, dtype=np.uint8)
275-
276-
for i in range(N):
277-
val = values[i]
278-
if util.is_nan(val):
279-
result[i] = True
280-
return result.view(bool)
281-
282-
283258
@cython.wraparound(False)
284259
@cython.boundscheck(False)
285260
def is_numeric_na(values: ndarray) -> ndarray:

pandas/core/indexes/base.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
is_datetime_array,
3838
no_default,
3939
)
40-
from pandas._libs.missing import is_float_nan
4140
from pandas._libs.tslibs import (
4241
IncompatibleFrequency,
4342
OutOfBoundsDatetime,
@@ -1390,16 +1389,8 @@ def _format_with_header(self, *, header: list[str_t], na_rep: str_t) -> list[str
13901389

13911390
if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
13921391
values = np.asarray(values)
1393-
values = lib.maybe_convert_objects(values, safe=True)
1394-
1395-
result = [pprint_thing(x, escape_chars=("\t", "\r", "\n")) for x in values]
1396-
1397-
# could have nans
1398-
mask = is_float_nan(values)
1399-
if mask.any():
1400-
result_arr = np.array(result)
1401-
result_arr[mask] = na_rep
1402-
result = result_arr.tolist()
1392+
# TODO: why do we need different justify for these cases?
1393+
result = trim_front(format_array(values, None, justify="all"))
14031394
else:
14041395
result = trim_front(format_array(values, None, justify="left"))
14051396
return header + result

pandas/io/formats/format.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -1216,18 +1216,16 @@ def _format_strings(self) -> list[str]:
12161216

12171217
def _format(x):
12181218
if self.na_rep is not None and is_scalar(x) and isna(x):
1219-
try:
1220-
# try block for np.isnat specifically
1221-
# determine na_rep if x is None or NaT-like
1222-
if x is None:
1223-
return "None"
1224-
elif x is NA:
1225-
return str(NA)
1226-
elif x is NaT or np.isnat(x):
1227-
return "NaT"
1228-
except (TypeError, ValueError):
1229-
# np.isnat only handles datetime or timedelta objects
1230-
pass
1219+
if x is None:
1220+
return "None"
1221+
elif x is NA:
1222+
return str(NA)
1223+
elif lib.is_float(x) and np.isinf(x):
1224+
# TODO(3.0): this will be unreachable when use_inf_as_na
1225+
# deprecation is enforced
1226+
return str(x)
1227+
elif x is NaT or isinstance(x, (np.datetime64, np.timedelta64)):
1228+
return "NaT"
12311229
return self.na_rep
12321230
elif isinstance(x, PandasObject):
12331231
return str(x)

pandas/tests/frame/test_repr_info.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def test_to_records_with_na_record(self):
411411
def test_to_records_with_inf_as_na_record(self):
412412
# GH 48526
413413
expected = """ NaN inf record
414-
0 NaN b [0, inf, b]
414+
0 inf b [0, inf, b]
415415
1 NaN NaN [1, nan, nan]
416416
2 e f [2, e, f]"""
417417
msg = "use_inf_as_na option is deprecated"

0 commit comments

Comments
 (0)