Skip to content

Commit 597d354

Browse files
meeseeksmachinejbrockmendelphofl
authored
Backport PR #51517 on branch 2.0.x (PERF: maybe_convert_objects convert_numeric=False) (#51661)
Backport PR #51517: PERF: maybe_convert_objects convert_numeric=False Co-authored-by: jbrockmendel <[email protected]> Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 28dba44 commit 597d354

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
@@ -2365,6 +2365,7 @@ def maybe_convert_objects(ndarray[object] objects,
23652365
*,
23662366
bint try_float=False,
23672367
bint safe=False,
2368+
bint convert_numeric=True, # NB: different default!
23682369
bint convert_datetime=False,
23692370
bint convert_timedelta=False,
23702371
bint convert_period=False,
@@ -2384,6 +2385,8 @@ def maybe_convert_objects(ndarray[object] objects,
23842385
safe : bool, default False
23852386
Whether to upcast numeric type (e.g. int cast to float). If set to
23862387
True, no upcasting will be performed.
2388+
convert_numeric : bool, default True
2389+
Whether to convert numeric entries.
23872390
convert_datetime : bool, default False
23882391
If an array-like object contains only datetime values or NaT is
23892392
encountered, whether to convert and return an array of M8[ns] dtype.
@@ -2461,9 +2464,13 @@ def maybe_convert_objects(ndarray[object] objects,
24612464
elif util.is_bool_object(val):
24622465
seen.bool_ = True
24632466
bools[i] = val
2467+
if not convert_numeric:
2468+
break
24642469
elif util.is_float_object(val):
24652470
floats[i] = complexes[i] = val
24662471
seen.float_ = True
2472+
if not convert_numeric:
2473+
break
24672474
elif is_timedelta(val):
24682475
if convert_timedelta:
24692476
seen.timedelta_ = True
@@ -2495,10 +2502,14 @@ def maybe_convert_objects(ndarray[object] objects,
24952502
else:
24962503
uints[i] = val
24972504
ints[i] = val
2505+
if not convert_numeric:
2506+
break
24982507

24992508
elif util.is_complex_object(val):
25002509
complexes[i] = val
25012510
seen.complex_ = True
2511+
if not convert_numeric:
2512+
break
25022513
elif PyDateTime_Check(val) or util.is_datetime64_object(val):
25032514

25042515
# if we have an tz's attached then return the objects
@@ -2636,6 +2647,12 @@ def maybe_convert_objects(ndarray[object] objects,
26362647
else:
26372648
seen.object_ = True
26382649

2650+
if not convert_numeric:
2651+
# Note: we count "bool" as numeric here. This is becase
2652+
# np.array(list_of_items) will convert bools just like it will numeric
2653+
# entries.
2654+
return objects
2655+
26392656
if seen.bool_:
26402657
if seen.is_bool:
26412658
# 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)