@@ -2641,6 +2641,36 @@ def maybe_convert_objects(ndarray[object] objects,
2641
2641
2642
2642
seen.object_ = True
2643
2643
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
+
2644
2674
if not seen.object_:
2645
2675
result = None
2646
2676
if not safe:
@@ -2660,36 +2690,15 @@ def maybe_convert_objects(ndarray[object] objects,
2660
2690
result = floats
2661
2691
else :
2662
2692
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
2693
2702
elif seen.is_bool:
2694
2703
result = bools.view(np.bool_)
2695
2704
@@ -2705,38 +2714,17 @@ def maybe_convert_objects(ndarray[object] objects,
2705
2714
result = floats
2706
2715
else :
2707
2716
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
2740
2728
elif seen.is_bool and not seen.nan_:
2741
2729
result = bools.view(np.bool_)
2742
2730
@@ -2751,22 +2739,24 @@ def maybe_convert_objects(ndarray[object] objects,
2751
2739
return objects
2752
2740
2753
2741
2754
- cdef _infer_all_nats(dtype, ndarray datetimes, ndarray timedeltas ):
2742
+ cdef _infer_all_nats(dtype, cnp.npy_intp * shape ):
2755
2743
"""
2756
2744
If we have all-NaT values, cast these to the given dtype.
2757
2745
"""
2758
2746
if cnp.PyArray_DescrCheck(dtype):
2759
2747
# 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
2764
2750
else :
2765
2751
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)
2766
2756
else :
2767
2757
# ExtensionDtype
2768
2758
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 )
2770
2760
i8vals.fill(NPY_NAT)
2771
2761
result = cls (i8vals, dtype = dtype)
2772
2762
return result
0 commit comments