Skip to content

Commit 16b5f0c

Browse files
authored
REF: remove numeric arg from NDFrame._convert (#50011)
1 parent bdd9629 commit 16b5f0c

File tree

10 files changed

+34
-128
lines changed

10 files changed

+34
-128
lines changed

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,8 @@ def coerce_indexer_dtype(indexer, categories) -> np.ndarray:
954954

955955
def soft_convert_objects(
956956
values: np.ndarray,
957+
*,
957958
datetime: bool = True,
958-
numeric: bool = True,
959959
timedelta: bool = True,
960960
period: bool = True,
961961
copy: bool = True,
@@ -968,7 +968,6 @@ def soft_convert_objects(
968968
----------
969969
values : np.ndarray[object]
970970
datetime : bool, default True
971-
numeric: bool, default True
972971
timedelta : bool, default True
973972
period : bool, default True
974973
copy : bool, default True
@@ -978,16 +977,15 @@ def soft_convert_objects(
978977
np.ndarray or ExtensionArray
979978
"""
980979
validate_bool_kwarg(datetime, "datetime")
981-
validate_bool_kwarg(numeric, "numeric")
982980
validate_bool_kwarg(timedelta, "timedelta")
983981
validate_bool_kwarg(copy, "copy")
984982

985-
conversion_count = sum((datetime, numeric, timedelta))
983+
conversion_count = sum((datetime, timedelta))
986984
if conversion_count == 0:
987-
raise ValueError("At least one of datetime, numeric or timedelta must be True.")
985+
raise ValueError("At least one of datetime or timedelta must be True.")
988986

989987
# Soft conversions
990-
if datetime or timedelta:
988+
if datetime or timedelta or period:
991989
# GH 20380, when datetime is beyond year 2262, hence outside
992990
# bound of nanosecond-resolution 64-bit integers.
993991
converted = lib.maybe_convert_objects(
@@ -999,13 +997,6 @@ def soft_convert_objects(
999997
if converted is not values:
1000998
return converted
1001999

1002-
if numeric and is_object_dtype(values.dtype):
1003-
converted, _ = lib.maybe_convert_numeric(values, set(), coerce_numeric=True)
1004-
1005-
# If all NaNs, then do not-alter
1006-
values = converted if not isna(converted).all() else values
1007-
values = values.copy() if copy else values
1008-
10091000
return values
10101001

10111002

pandas/core/generic.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6318,8 +6318,8 @@ def __deepcopy__(self: NDFrameT, memo=None) -> NDFrameT:
63186318
@final
63196319
def _convert(
63206320
self: NDFrameT,
6321+
*,
63216322
datetime: bool_t = False,
6322-
numeric: bool_t = False,
63236323
timedelta: bool_t = False,
63246324
) -> NDFrameT:
63256325
"""
@@ -6329,9 +6329,6 @@ def _convert(
63296329
----------
63306330
datetime : bool, default False
63316331
If True, convert to date where possible.
6332-
numeric : bool, default False
6333-
If True, attempt to convert to numbers (including strings), with
6334-
unconvertible values becoming NaN.
63356332
timedelta : bool, default False
63366333
If True, convert to timedelta where possible.
63376334
@@ -6340,12 +6337,10 @@ def _convert(
63406337
converted : same as input object
63416338
"""
63426339
validate_bool_kwarg(datetime, "datetime")
6343-
validate_bool_kwarg(numeric, "numeric")
63446340
validate_bool_kwarg(timedelta, "timedelta")
63456341
return self._constructor(
63466342
self._mgr.convert(
63476343
datetime=datetime,
6348-
numeric=numeric,
63496344
timedelta=timedelta,
63506345
copy=True,
63516346
)
@@ -6390,11 +6385,8 @@ def infer_objects(self: NDFrameT) -> NDFrameT:
63906385
A int64
63916386
dtype: object
63926387
"""
6393-
# numeric=False necessary to only soft convert;
6394-
# python objects will still be converted to
6395-
# native numpy numeric types
63966388
return self._constructor(
6397-
self._mgr.convert(datetime=True, numeric=False, timedelta=True, copy=True)
6389+
self._mgr.convert(datetime=True, timedelta=True, copy=True)
63986390
).__finalize__(self, method="infer_objects")
63996391

64006392
@final

pandas/core/internals/array_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,9 @@ def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T:
377377

378378
def convert(
379379
self: T,
380+
*,
380381
copy: bool = True,
381382
datetime: bool = True,
382-
numeric: bool = True,
383383
timedelta: bool = True,
384384
) -> T:
385385
def _convert(arr):
@@ -389,7 +389,6 @@ def _convert(arr):
389389
return soft_convert_objects(
390390
arr,
391391
datetime=datetime,
392-
numeric=numeric,
393392
timedelta=timedelta,
394393
copy=copy,
395394
)

pandas/core/internals/blocks.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,7 @@ def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]:
429429
# but ATM it breaks too much existing code.
430430
# split and convert the blocks
431431

432-
return extend_blocks(
433-
[blk.convert(datetime=True, numeric=False) for blk in blocks]
434-
)
432+
return extend_blocks([blk.convert(datetime=True) for blk in blocks])
435433

436434
if downcast is None:
437435
return blocks
@@ -451,9 +449,9 @@ def _downcast_2d(self, dtype) -> list[Block]:
451449

452450
def convert(
453451
self,
452+
*,
454453
copy: bool = True,
455454
datetime: bool = True,
456-
numeric: bool = True,
457455
timedelta: bool = True,
458456
) -> list[Block]:
459457
"""
@@ -570,7 +568,7 @@ def replace(
570568
if not (self.is_object and value is None):
571569
# if the user *explicitly* gave None, we keep None, otherwise
572570
# may downcast to NaN
573-
blocks = blk.convert(numeric=False, copy=False)
571+
blocks = blk.convert(copy=False)
574572
else:
575573
blocks = [blk]
576574
return blocks
@@ -642,7 +640,7 @@ def _replace_regex(
642640
replace_regex(new_values, rx, value, mask)
643641

644642
block = self.make_block(new_values)
645-
return block.convert(numeric=False, copy=False)
643+
return block.convert(copy=False)
646644

647645
@final
648646
def replace_list(
@@ -712,9 +710,7 @@ def replace_list(
712710
)
713711
if convert and blk.is_object and not all(x is None for x in dest_list):
714712
# GH#44498 avoid unwanted cast-back
715-
result = extend_blocks(
716-
[b.convert(numeric=False, copy=True) for b in result]
717-
)
713+
result = extend_blocks([b.convert(copy=True) for b in result])
718714
new_rb.extend(result)
719715
rb = new_rb
720716
return rb
@@ -1969,9 +1965,9 @@ def reduce(self, func) -> list[Block]:
19691965
@maybe_split
19701966
def convert(
19711967
self,
1968+
*,
19721969
copy: bool = True,
19731970
datetime: bool = True,
1974-
numeric: bool = True,
19751971
timedelta: bool = True,
19761972
) -> list[Block]:
19771973
"""
@@ -1987,7 +1983,6 @@ def convert(
19871983
res_values = soft_convert_objects(
19881984
values,
19891985
datetime=datetime,
1990-
numeric=numeric,
19911986
timedelta=timedelta,
19921987
copy=copy,
19931988
)

pandas/core/internals/managers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,15 @@ def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T:
443443

444444
def convert(
445445
self: T,
446+
*,
446447
copy: bool = True,
447448
datetime: bool = True,
448-
numeric: bool = True,
449449
timedelta: bool = True,
450450
) -> T:
451451
return self.apply(
452452
"convert",
453453
copy=copy,
454454
datetime=datetime,
455-
numeric=numeric,
456455
timedelta=timedelta,
457456
)
458457

pandas/tests/frame/methods/test_convert.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import (
5-
DataFrame,
6-
Series,
7-
)
4+
from pandas import DataFrame
85
import pandas._testing as tm
96

107

@@ -21,17 +18,11 @@ def test_convert_objects(self, float_string_frame):
2118
float_string_frame["I"] = "1"
2219

2320
# add in some items that will be nan
24-
length = len(float_string_frame)
2521
float_string_frame["J"] = "1."
2622
float_string_frame["K"] = "1"
2723
float_string_frame.loc[float_string_frame.index[0:5], ["J", "K"]] = "garbled"
28-
converted = float_string_frame._convert(datetime=True, numeric=True)
29-
assert converted["H"].dtype == "float64"
30-
assert converted["I"].dtype == "int64"
31-
assert converted["J"].dtype == "float64"
32-
assert converted["K"].dtype == "float64"
33-
assert len(converted["J"].dropna()) == length - 5
34-
assert len(converted["K"].dropna()) == length - 5
24+
converted = float_string_frame._convert(datetime=True)
25+
tm.assert_frame_equal(converted, float_string_frame)
3526

3627
# via astype
3728
converted = float_string_frame.copy()
@@ -45,14 +36,6 @@ def test_convert_objects(self, float_string_frame):
4536
with pytest.raises(ValueError, match="invalid literal"):
4637
converted["H"].astype("int32")
4738

48-
def test_convert_mixed_single_column(self):
49-
# GH#4119, not converting a mixed type (e.g.floats and object)
50-
# mixed in a single column
51-
df = DataFrame({"s": Series([1, "na", 3, 4])})
52-
result = df._convert(datetime=True, numeric=True)
53-
expected = DataFrame({"s": Series([1, np.nan, 3, 4])})
54-
tm.assert_frame_equal(result, expected)
55-
5639
def test_convert_objects_no_conversion(self):
5740
mixed1 = DataFrame({"a": [1, 2, 3], "b": [4.0, 5, 6], "c": ["x", "y", "z"]})
5841
mixed2 = mixed1._convert(datetime=True)

pandas/tests/indexing/test_indexing.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,14 +557,6 @@ def test_astype_assignment(self):
557557
)
558558
tm.assert_frame_equal(df, expected)
559559

560-
df = df_orig.copy()
561-
with tm.assert_produces_warning(FutureWarning, match=msg):
562-
df.iloc[:, 0:2] = df.iloc[:, 0:2]._convert(datetime=True, numeric=True)
563-
expected = DataFrame(
564-
[[1, 2, "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
565-
)
566-
tm.assert_frame_equal(df, expected)
567-
568560
# GH5702 (loc)
569561
df = df_orig.copy()
570562
with tm.assert_produces_warning(FutureWarning, match=msg):

pandas/tests/internals/test_internals.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ def _compare(old_mgr, new_mgr):
599599
mgr.iset(0, np.array(["1"] * N, dtype=np.object_))
600600
mgr.iset(1, np.array(["2."] * N, dtype=np.object_))
601601
mgr.iset(2, np.array(["foo."] * N, dtype=np.object_))
602-
new_mgr = mgr.convert(numeric=True)
603-
assert new_mgr.iget(0).dtype == np.int64
604-
assert new_mgr.iget(1).dtype == np.float64
602+
new_mgr = mgr.convert()
603+
assert new_mgr.iget(0).dtype == np.object_
604+
assert new_mgr.iget(1).dtype == np.object_
605605
assert new_mgr.iget(2).dtype == np.object_
606606
assert new_mgr.iget(3).dtype == np.int64
607607
assert new_mgr.iget(4).dtype == np.float64
@@ -612,9 +612,9 @@ def _compare(old_mgr, new_mgr):
612612
mgr.iset(0, np.array(["1"] * N, dtype=np.object_))
613613
mgr.iset(1, np.array(["2."] * N, dtype=np.object_))
614614
mgr.iset(2, np.array(["foo."] * N, dtype=np.object_))
615-
new_mgr = mgr.convert(numeric=True)
616-
assert new_mgr.iget(0).dtype == np.int64
617-
assert new_mgr.iget(1).dtype == np.float64
615+
new_mgr = mgr.convert()
616+
assert new_mgr.iget(0).dtype == np.object_
617+
assert new_mgr.iget(1).dtype == np.object_
618618
assert new_mgr.iget(2).dtype == np.object_
619619
assert new_mgr.iget(3).dtype == np.int32
620620
assert new_mgr.iget(4).dtype == np.bool_

pandas/tests/io/test_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def try_remove_ws(x):
627627
]
628628
dfnew = df.applymap(try_remove_ws).replace(old, new)
629629
gtnew = ground_truth.applymap(try_remove_ws)
630-
converted = dfnew._convert(datetime=True, numeric=True)
630+
converted = dfnew._convert(datetime=True)
631631
date_cols = ["Closing Date", "Updated Date"]
632632
converted[date_cols] = converted[date_cols].apply(to_datetime)
633633
tm.assert_frame_equal(converted, gtnew)

0 commit comments

Comments
 (0)