Skip to content

Commit 2fcd8b7

Browse files
committed
REF: separate out seen.nat_ cases in maybe_convert_objects
1 parent 081c06b commit 2fcd8b7

File tree

2 files changed

+60
-68
lines changed

2 files changed

+60
-68
lines changed

pandas/_libs/lib.pyx

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,36 @@ def maybe_convert_objects(ndarray[object] objects,
26412641

26422642
seen.object_ = True
26432643

2644+
if seen.nat_:
2645+
if seen.object_:
2646+
result = objects
2647+
elif seen.bool_:
2648+
result = objects
2649+
elif seen.null_:
2650+
result = objects
2651+
elif not safe and seen.nan_:
2652+
result = objects
2653+
elif seen.numeric_:
2654+
result = objects
2655+
else:
2656+
if convert_datetime and convert_timedelta:
2657+
dtype = dtype_if_all_nat
2658+
if dtype is not None:
2659+
# otherwise we keep object dtype
2660+
result = _infer_all_nats(
2661+
dtype, objects.shape
2662+
)
2663+
else:
2664+
result = objects
2665+
elif convert_datetime:
2666+
result = datetimes
2667+
elif convert_timedelta:
2668+
result = timedeltas
2669+
else:
2670+
result = objects
2671+
return result
2672+
return result
2673+
26442674
if not seen.object_:
26452675
result = None
26462676
if not safe:
@@ -2660,36 +2690,15 @@ def maybe_convert_objects(ndarray[object] objects,
26602690
result = floats
26612691
else:
26622692
if not seen.bool_:
2663-
if seen.datetime_:
2664-
if not seen.numeric_ and not seen.timedelta_:
2665-
result = datetimes
2666-
elif seen.timedelta_:
2667-
if not seen.numeric_:
2668-
result = timedeltas
2669-
elif seen.nat_:
2670-
if not seen.numeric_:
2671-
if convert_datetime and convert_timedelta:
2672-
dtype = dtype_if_all_nat
2673-
if dtype is not None:
2674-
# otherwise we keep object dtype
2675-
result = _infer_all_nats(
2676-
dtype, datetimes, timedeltas
2677-
)
2678-
2679-
elif convert_datetime:
2680-
result = datetimes
2681-
elif convert_timedelta:
2682-
result = timedeltas
2683-
else:
2684-
if seen.complex_:
2685-
result = complexes
2686-
elif seen.float_:
2687-
result = floats
2688-
elif seen.int_:
2689-
if seen.uint_:
2690-
result = uints
2691-
else:
2692-
result = ints
2693+
if seen.complex_:
2694+
result = complexes
2695+
elif seen.float_:
2696+
result = floats
2697+
elif seen.int_:
2698+
if seen.uint_:
2699+
result = uints
2700+
else:
2701+
result = ints
26932702
elif seen.is_bool:
26942703
result = bools.view(np.bool_)
26952704

@@ -2705,38 +2714,17 @@ def maybe_convert_objects(ndarray[object] objects,
27052714
result = floats
27062715
else:
27072716
if not seen.bool_:
2708-
if seen.datetime_:
2709-
if not seen.numeric_ and not seen.timedelta_:
2710-
result = datetimes
2711-
elif seen.timedelta_:
2712-
if not seen.numeric_:
2713-
result = timedeltas
2714-
elif seen.nat_:
2715-
if not seen.numeric_:
2716-
if convert_datetime and convert_timedelta:
2717-
dtype = dtype_if_all_nat
2718-
if dtype is not None:
2719-
# otherwise we keep object dtype
2720-
result = _infer_all_nats(
2721-
dtype, datetimes, timedeltas
2722-
)
2723-
2724-
elif convert_datetime:
2725-
result = datetimes
2726-
elif convert_timedelta:
2727-
result = timedeltas
2728-
else:
2729-
if seen.complex_:
2730-
if not seen.int_:
2731-
result = complexes
2732-
elif seen.float_ or seen.nan_:
2733-
if not seen.int_:
2734-
result = floats
2735-
elif seen.int_:
2736-
if seen.uint_:
2737-
result = uints
2738-
else:
2739-
result = ints
2717+
if seen.complex_:
2718+
if not seen.int_:
2719+
result = complexes
2720+
elif seen.float_ or seen.nan_:
2721+
if not seen.int_:
2722+
result = floats
2723+
elif seen.int_:
2724+
if seen.uint_:
2725+
result = uints
2726+
else:
2727+
result = ints
27402728
elif seen.is_bool and not seen.nan_:
27412729
result = bools.view(np.bool_)
27422730

@@ -2751,22 +2739,24 @@ def maybe_convert_objects(ndarray[object] objects,
27512739
return objects
27522740

27532741

2754-
cdef _infer_all_nats(dtype, ndarray datetimes, ndarray timedeltas):
2742+
cdef _infer_all_nats(dtype, cnp.npy_intp* shape):
27552743
"""
27562744
If we have all-NaT values, cast these to the given dtype.
27572745
"""
27582746
if cnp.PyArray_DescrCheck(dtype):
27592747
# i.e. isinstance(dtype, np.dtype):
2760-
if dtype == "M8[ns]":
2761-
result = datetimes
2762-
elif dtype == "m8[ns]":
2763-
result = timedeltas
2748+
if dtype == "M8[ns]" or dtype == "m8[ns]":
2749+
pass
27642750
else:
27652751
raise ValueError(dtype)
2752+
2753+
i8vals = cnp.PyArray_EMPTY(1, shape, cnp.NPY_INT64, 0)
2754+
i8vals.fill(NPY_NAT)
2755+
result = i8vals.view(dtype)
27662756
else:
27672757
# ExtensionDtype
27682758
cls = dtype.construct_array_type()
2769-
i8vals = cnp.PyArray_EMPTY(1, datetimes.shape, cnp.NPY_INT64, 0)
2759+
i8vals = cnp.PyArray_EMPTY(1, shape, cnp.NPY_INT64, 0)
27702760
i8vals.fill(NPY_NAT)
27712761
result = cls(i8vals, dtype=dtype)
27722762
return result

pandas/core/indexes/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,8 @@ def _format_with_header(self, header: list[str_t], na_rep: str_t) -> list[str_t]
13611361

13621362
if is_object_dtype(values.dtype):
13631363
values = cast(np.ndarray, values)
1364+
# Only place where we pass safe=True, only needed for
1365+
# test_format_missing
13641366
values = lib.maybe_convert_objects(values, safe=True)
13651367

13661368
result = [pprint_thing(x, escape_chars=("\t", "\r", "\n")) for x in values]

0 commit comments

Comments
 (0)