Skip to content

Commit b99e296

Browse files
authored
PERF: maybe_convert_objects convert_numeric=False (#51517)
1 parent a80ffdb commit b99e296

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

pandas/_libs/lib.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def maybe_convert_objects(
7171
*,
7272
try_float: bool = ...,
7373
safe: bool = ...,
74+
convert_numeric: bool = ...,
7475
convert_datetime: Literal[False] = ...,
7576
convert_timedelta: Literal[False] = ...,
7677
convert_period: Literal[False] = ...,
@@ -84,6 +85,7 @@ def maybe_convert_objects(
8485
*,
8586
try_float: bool = ...,
8687
safe: bool = ...,
88+
convert_numeric: bool = ...,
8789
convert_datetime: Literal[False] = ...,
8890
convert_timedelta: bool = ...,
8991
convert_period: Literal[False] = ...,
@@ -97,6 +99,7 @@ def maybe_convert_objects(
9799
*,
98100
try_float: bool = ...,
99101
safe: bool = ...,
102+
convert_numeric: bool = ...,
100103
convert_datetime: bool = ...,
101104
convert_timedelta: bool = ...,
102105
convert_period: bool = ...,
@@ -110,6 +113,7 @@ def maybe_convert_objects(
110113
*,
111114
try_float: bool = ...,
112115
safe: bool = ...,
116+
convert_numeric: bool = ...,
113117
convert_datetime: Literal[True] = ...,
114118
convert_timedelta: bool = ...,
115119
convert_period: bool = ...,
@@ -123,6 +127,7 @@ def maybe_convert_objects(
123127
*,
124128
try_float: bool = ...,
125129
safe: bool = ...,
130+
convert_numeric: bool = ...,
126131
convert_datetime: bool = ...,
127132
convert_timedelta: bool = ...,
128133
convert_period: Literal[True] = ...,
@@ -136,6 +141,7 @@ def maybe_convert_objects(
136141
*,
137142
try_float: bool = ...,
138143
safe: bool = ...,
144+
convert_numeric: bool = ...,
139145
convert_datetime: bool = ...,
140146
convert_timedelta: bool = ...,
141147
convert_period: bool = ...,

pandas/_libs/lib.pyx

+17
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,7 @@ def maybe_convert_objects(ndarray[object] objects,
23672367
*,
23682368
bint try_float=False,
23692369
bint safe=False,
2370+
bint convert_numeric=True, # NB: different default!
23702371
bint convert_datetime=False,
23712372
bint convert_timedelta=False,
23722373
bint convert_period=False,
@@ -2386,6 +2387,8 @@ def maybe_convert_objects(ndarray[object] objects,
23862387
safe : bool, default False
23872388
Whether to upcast numeric type (e.g. int cast to float). If set to
23882389
True, no upcasting will be performed.
2390+
convert_numeric : bool, default True
2391+
Whether to convert numeric entries.
23892392
convert_datetime : bool, default False
23902393
If an array-like object contains only datetime values or NaT is
23912394
encountered, whether to convert and return an array of M8[ns] dtype.
@@ -2463,9 +2466,13 @@ def maybe_convert_objects(ndarray[object] objects,
24632466
elif util.is_bool_object(val):
24642467
seen.bool_ = True
24652468
bools[i] = val
2469+
if not convert_numeric:
2470+
break
24662471
elif util.is_float_object(val):
24672472
floats[i] = complexes[i] = val
24682473
seen.float_ = True
2474+
if not convert_numeric:
2475+
break
24692476
elif is_timedelta(val):
24702477
if convert_timedelta:
24712478
seen.timedelta_ = True
@@ -2497,10 +2504,14 @@ def maybe_convert_objects(ndarray[object] objects,
24972504
else:
24982505
uints[i] = val
24992506
ints[i] = val
2507+
if not convert_numeric:
2508+
break
25002509

25012510
elif util.is_complex_object(val):
25022511
complexes[i] = val
25032512
seen.complex_ = True
2513+
if not convert_numeric:
2514+
break
25042515
elif PyDateTime_Check(val) or util.is_datetime64_object(val):
25052516

25062517
# if we have an tz's attached then return the objects
@@ -2638,6 +2649,12 @@ def maybe_convert_objects(ndarray[object] objects,
26382649
else:
26392650
seen.object_ = True
26402651

2652+
if not convert_numeric:
2653+
# Note: we count "bool" as numeric here. This is becase
2654+
# np.array(list_of_items) will convert bools just like it will numeric
2655+
# entries.
2656+
return objects
2657+
26412658
if seen.bool_:
26422659
if seen.is_bool:
26432660
# is_bool property rules out everything else

pandas/core/dtypes/cast.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1156,23 +1156,20 @@ def maybe_infer_to_datetimelike(
11561156
if not len(value):
11571157
return value
11581158

1159-
out = lib.maybe_convert_objects(
1159+
# error: Incompatible return value type (got "Union[ExtensionArray,
1160+
# ndarray[Any, Any]]", expected "Union[ndarray[Any, Any], DatetimeArray,
1161+
# TimedeltaArray, PeriodArray, IntervalArray]")
1162+
return lib.maybe_convert_objects( # type: ignore[return-value]
11601163
value,
1164+
# Here we do not convert numeric dtypes, as if we wanted that,
1165+
# numpy would have done it for us.
1166+
convert_numeric=False,
11611167
convert_period=True,
11621168
convert_interval=True,
11631169
convert_timedelta=True,
11641170
convert_datetime=True,
11651171
dtype_if_all_nat=np.dtype("M8[ns]"),
11661172
)
1167-
if out.dtype.kind in ["i", "u", "f", "b", "c"]:
1168-
# Here we do not convert numeric dtypes, as if we wanted that,
1169-
# numpy would have done it for us.
1170-
# See also _maybe_cast_data_without_dtype
1171-
return value
1172-
# Incompatible return value type (got "Union[ExtensionArray, ndarray[Any, Any]]",
1173-
# expected "Union[ndarray[Any, Any], DatetimeArray, TimedeltaArray, PeriodArray,
1174-
# IntervalArray]")
1175-
return out # type: ignore[return-value]
11761173

11771174

11781175
def maybe_cast_to_datetime(

0 commit comments

Comments
 (0)