Skip to content

Commit 3f51060

Browse files
authored
CLN: remove unused coerce arg in NDFrame._convert (#38151)
1 parent 2f41109 commit 3f51060

File tree

7 files changed

+34
-119
lines changed

7 files changed

+34
-119
lines changed

pandas/core/dtypes/cast.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,45 +1128,32 @@ def soft_convert_objects(
11281128
datetime: bool = True,
11291129
numeric: bool = True,
11301130
timedelta: bool = True,
1131-
coerce: bool = False,
11321131
copy: bool = True,
11331132
):
1134-
""" if we have an object dtype, try to coerce dates and/or numbers """
1133+
"""
1134+
Try to coerce datetime, timedelta, and numeric object-dtype columns
1135+
to inferred dtype.
1136+
1137+
Parameters
1138+
----------
1139+
values : np.ndarray[object]
1140+
datetime : bool, default True
1141+
numeric: bool, default True
1142+
timedelta : bool, default True
1143+
copy : bool, default True
1144+
1145+
Returns
1146+
-------
1147+
np.ndarray
1148+
"""
11351149
validate_bool_kwarg(datetime, "datetime")
11361150
validate_bool_kwarg(numeric, "numeric")
11371151
validate_bool_kwarg(timedelta, "timedelta")
1138-
validate_bool_kwarg(coerce, "coerce")
11391152
validate_bool_kwarg(copy, "copy")
11401153

11411154
conversion_count = sum((datetime, numeric, timedelta))
11421155
if conversion_count == 0:
11431156
raise ValueError("At least one of datetime, numeric or timedelta must be True.")
1144-
elif conversion_count > 1 and coerce:
1145-
raise ValueError(
1146-
"Only one of 'datetime', 'numeric' or "
1147-
"'timedelta' can be True when coerce=True."
1148-
)
1149-
1150-
if not is_object_dtype(values.dtype):
1151-
# If not object, do not attempt conversion
1152-
values = values.copy() if copy else values
1153-
return values
1154-
1155-
# If 1 flag is coerce, ensure 2 others are False
1156-
if coerce:
1157-
# Immediate return if coerce
1158-
if datetime:
1159-
from pandas import to_datetime
1160-
1161-
return to_datetime(values, errors="coerce").to_numpy()
1162-
elif timedelta:
1163-
from pandas import to_timedelta
1164-
1165-
return to_timedelta(values, errors="coerce").to_numpy()
1166-
elif numeric:
1167-
from pandas import to_numeric
1168-
1169-
return to_numeric(values, errors="coerce")
11701157

11711158
# Soft conversions
11721159
if datetime:

pandas/core/generic.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5997,7 +5997,6 @@ def _convert(
59975997
datetime: bool_t = False,
59985998
numeric: bool_t = False,
59995999
timedelta: bool_t = False,
6000-
coerce: bool_t = False,
60016000
) -> FrameOrSeries:
60026001
"""
60036002
Attempt to infer better dtype for object columns
@@ -6011,9 +6010,6 @@ def _convert(
60116010
unconvertible values becoming NaN.
60126011
timedelta : bool, default False
60136012
If True, convert to timedelta where possible.
6014-
coerce : bool, default False
6015-
If True, force conversion with unconvertible values converted to
6016-
nulls (NaN or NaT).
60176013
60186014
Returns
60196015
-------
@@ -6022,13 +6018,11 @@ def _convert(
60226018
validate_bool_kwarg(datetime, "datetime")
60236019
validate_bool_kwarg(numeric, "numeric")
60246020
validate_bool_kwarg(timedelta, "timedelta")
6025-
validate_bool_kwarg(coerce, "coerce")
60266021
return self._constructor(
60276022
self._mgr.convert(
60286023
datetime=datetime,
60296024
numeric=numeric,
60306025
timedelta=timedelta,
6031-
coerce=coerce,
60326026
copy=True,
60336027
)
60346028
).__finalize__(self)
@@ -6076,9 +6070,7 @@ def infer_objects(self: FrameOrSeries) -> FrameOrSeries:
60766070
# python objects will still be converted to
60776071
# native numpy numeric types
60786072
return self._constructor(
6079-
self._mgr.convert(
6080-
datetime=True, numeric=False, timedelta=True, coerce=False, copy=True
6081-
)
6073+
self._mgr.convert(datetime=True, numeric=False, timedelta=True, copy=True)
60826074
).__finalize__(self, method="infer_objects")
60836075

60846076
@final

pandas/core/internals/blocks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ def convert(
700700
datetime: bool = True,
701701
numeric: bool = True,
702702
timedelta: bool = True,
703-
coerce: bool = False,
704703
) -> List["Block"]:
705704
"""
706705
attempt to coerce any object types to better types return a copy
@@ -2506,12 +2505,12 @@ def convert(
25062505
datetime: bool = True,
25072506
numeric: bool = True,
25082507
timedelta: bool = True,
2509-
coerce: bool = False,
25102508
) -> List["Block"]:
25112509
"""
2512-
attempt to coerce any object types to better types return a copy of
2510+
attempt to cast any object types to better types return a copy of
25132511
the block (if copy = True) by definition we ARE an ObjectBlock!!!!!
25142512
"""
2513+
25152514
# operate column-by-column
25162515
def f(mask, val, idx):
25172516
shape = val.shape
@@ -2520,7 +2519,6 @@ def f(mask, val, idx):
25202519
datetime=datetime,
25212520
numeric=numeric,
25222521
timedelta=timedelta,
2523-
coerce=coerce,
25242522
copy=copy,
25252523
)
25262524
if isinstance(values, np.ndarray):

pandas/core/internals/managers.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,15 +636,13 @@ def convert(
636636
datetime: bool = True,
637637
numeric: bool = True,
638638
timedelta: bool = True,
639-
coerce: bool = False,
640639
) -> "BlockManager":
641640
return self.apply(
642641
"convert",
643642
copy=copy,
644643
datetime=datetime,
645644
numeric=numeric,
646645
timedelta=timedelta,
647-
coerce=coerce,
648646
)
649647

650648
def replace(self, to_replace, value, inplace: bool, regex: bool) -> "BlockManager":

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def make_dtnat_arr(n, nnat=None):
251251
df = DataFrame(dict(a=s1, b=s2))
252252
df.to_csv(pth, chunksize=chunksize)
253253

254-
recons = self.read_csv(pth)._convert(datetime=True, coerce=True)
254+
recons = self.read_csv(pth).apply(to_datetime)
255255
tm.assert_frame_equal(df, recons, check_names=False)
256256

257257
@pytest.mark.slow

pandas/tests/io/test_html.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
from pandas.errors import ParserError
1515
import pandas.util._test_decorators as td
1616

17-
from pandas import DataFrame, MultiIndex, Series, Timestamp, date_range, read_csv
17+
from pandas import (
18+
DataFrame,
19+
MultiIndex,
20+
Series,
21+
Timestamp,
22+
date_range,
23+
read_csv,
24+
to_datetime,
25+
)
1826
import pandas._testing as tm
1927

2028
from pandas.io.common import file_path_to_url
@@ -610,7 +618,7 @@ def try_remove_ws(x):
610618
gtnew = ground_truth.applymap(try_remove_ws)
611619
converted = dfnew._convert(datetime=True, numeric=True)
612620
date_cols = ["Closing Date", "Updated Date"]
613-
converted[date_cols] = converted[date_cols]._convert(datetime=True, coerce=True)
621+
converted[date_cols] = converted[date_cols].apply(to_datetime)
614622
tm.assert_frame_equal(converted, gtnew)
615623

616624
@pytest.mark.slow

pandas/tests/series/methods/test_convert.py

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,23 @@
33
import numpy as np
44
import pytest
55

6-
from pandas import NaT, Series, Timestamp
6+
from pandas import Series, Timestamp
77
import pandas._testing as tm
88

99

1010
class TestConvert:
1111
def test_convert(self):
1212
# GH#10265
13-
# Tests: All to nans, coerce, true
14-
# Test coercion returns correct type
15-
ser = Series(["a", "b", "c"])
16-
results = ser._convert(datetime=True, coerce=True)
17-
expected = Series([NaT] * 3)
18-
tm.assert_series_equal(results, expected)
19-
20-
results = ser._convert(numeric=True, coerce=True)
21-
expected = Series([np.nan] * 3)
22-
tm.assert_series_equal(results, expected)
23-
24-
expected = Series([NaT] * 3, dtype=np.dtype("m8[ns]"))
25-
results = ser._convert(timedelta=True, coerce=True)
26-
tm.assert_series_equal(results, expected)
27-
2813
dt = datetime(2001, 1, 1, 0, 0)
2914
td = dt - datetime(2000, 1, 1, 0, 0)
3015

3116
# Test coercion with mixed types
3217
ser = Series(["a", "3.1415", dt, td])
33-
results = ser._convert(datetime=True, coerce=True)
34-
expected = Series([NaT, NaT, dt, NaT])
35-
tm.assert_series_equal(results, expected)
3618

37-
results = ser._convert(numeric=True, coerce=True)
19+
results = ser._convert(numeric=True)
3820
expected = Series([np.nan, 3.1415, np.nan, np.nan])
3921
tm.assert_series_equal(results, expected)
4022

41-
results = ser._convert(timedelta=True, coerce=True)
42-
expected = Series([NaT, NaT, NaT, td], dtype=np.dtype("m8[ns]"))
43-
tm.assert_series_equal(results, expected)
44-
4523
# Test standard conversion returns original
4624
results = ser._convert(datetime=True)
4725
tm.assert_series_equal(results, ser)
@@ -116,19 +94,6 @@ def test_convert(self):
11694
datetime(2001, 1, 3, 0, 0),
11795
]
11896
)
119-
s2 = Series(
120-
[
121-
datetime(2001, 1, 1, 0, 0),
122-
datetime(2001, 1, 2, 0, 0),
123-
datetime(2001, 1, 3, 0, 0),
124-
"foo",
125-
1.0,
126-
1,
127-
Timestamp("20010104"),
128-
"20010105",
129-
],
130-
dtype="O",
131-
)
13297

13398
result = ser._convert(datetime=True)
13499
expected = Series(
@@ -137,35 +102,12 @@ def test_convert(self):
137102
)
138103
tm.assert_series_equal(result, expected)
139104

140-
result = ser._convert(datetime=True, coerce=True)
141-
tm.assert_series_equal(result, expected)
142-
143-
expected = Series(
144-
[
145-
Timestamp("20010101"),
146-
Timestamp("20010102"),
147-
Timestamp("20010103"),
148-
NaT,
149-
NaT,
150-
NaT,
151-
Timestamp("20010104"),
152-
Timestamp("20010105"),
153-
],
154-
dtype="M8[ns]",
155-
)
156-
result = s2._convert(datetime=True, numeric=False, timedelta=False, coerce=True)
157-
tm.assert_series_equal(result, expected)
158-
result = s2._convert(datetime=True, coerce=True)
159-
tm.assert_series_equal(result, expected)
160-
161-
ser = Series(["foo", "bar", 1, 1.0], dtype="O")
162-
result = ser._convert(datetime=True, coerce=True)
163-
expected = Series([NaT] * 2 + [Timestamp(1)] * 2)
105+
result = ser._convert(datetime=True)
164106
tm.assert_series_equal(result, expected)
165107

166108
# preserver if non-object
167109
ser = Series([1], dtype="float32")
168-
result = ser._convert(datetime=True, coerce=True)
110+
result = ser._convert(datetime=True)
169111
tm.assert_series_equal(result, ser)
170112

171113
# FIXME: dont leave commented-out
@@ -174,16 +116,6 @@ def test_convert(self):
174116
# result = res._convert(convert_dates=True,convert_numeric=False)
175117
# assert result.dtype == 'M8[ns]'
176118

177-
# dateutil parses some single letters into today's value as a date
178-
expected = Series([NaT])
179-
for x in "abcdefghijklmnopqrstuvwxyz":
180-
ser = Series([x])
181-
result = ser._convert(datetime=True, coerce=True)
182-
tm.assert_series_equal(result, expected)
183-
ser = Series([x.upper()])
184-
result = ser._convert(datetime=True, coerce=True)
185-
tm.assert_series_equal(result, expected)
186-
187119
def test_convert_no_arg_error(self):
188120
ser = Series(["1.0", "2"])
189121
msg = r"At least one of datetime, numeric or timedelta must be True\."

0 commit comments

Comments
 (0)