Skip to content

Commit 9f64e26

Browse files
jbrockmendelclaude
andcommitted
DEPR: convert_dtypes boolean keywords
Deprecate the infer_objects, convert_string, convert_integer, convert_boolean, and convert_floating keywords in DataFrame.convert_dtypes and Series.convert_dtypes. closes #62022 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 48295cd commit 9f64e26

8 files changed

Lines changed: 116 additions & 37 deletions

File tree

doc/source/whatsnew/v3.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Deprecations
122122
:meth:`DataFrame.var`, :meth:`DataFrame.sem`, :meth:`DataFrame.sum`,
123123
:meth:`DataFrame.prod` (and their :class:`Series` equivalents); this will raise in
124124
a future version of pandas (:issue:`53098`)
125+
- Deprecated the ``infer_objects``, ``convert_string``, ``convert_integer``, ``convert_boolean``, and ``convert_floating`` keywords in :meth:`DataFrame.convert_dtypes` and :meth:`Series.convert_dtypes` (:issue:`62022`)
125126
- Deprecated the inference of ``datetime64`` dtype from data containing ``datetime.date`` objects when used in comparisons or :meth:`Index.equals` with :class:`DatetimeIndex`. Use :func:`pandas.to_datetime` to explicitly convert to ``datetime64`` instead (:issue:`65056`)
126127
-
127128

pandas/core/generic.py

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6776,11 +6776,11 @@ def infer_objects(self, copy: bool | lib.NoDefault = lib.no_default) -> Self:
67766776
@final
67776777
def convert_dtypes(
67786778
self,
6779-
infer_objects: bool = True,
6780-
convert_string: bool = True,
6781-
convert_integer: bool = True,
6782-
convert_boolean: bool = True,
6783-
convert_floating: bool = True,
6779+
infer_objects: bool | lib.NoDefault = lib.no_default,
6780+
convert_string: bool | lib.NoDefault = lib.no_default,
6781+
convert_integer: bool | lib.NoDefault = lib.no_default,
6782+
convert_boolean: bool | lib.NoDefault = lib.no_default,
6783+
convert_floating: bool | lib.NoDefault = lib.no_default,
67846784
dtype_backend: DtypeBackend = "numpy_nullable",
67856785
) -> Self:
67866786
"""
@@ -6794,16 +6794,41 @@ def convert_dtypes(
67946794
----------
67956795
infer_objects : bool, default True
67966796
Whether object dtypes should be converted to the best possible types.
6797+
6798+
.. deprecated:: 3.1.0
6799+
The ``infer_objects`` keyword is deprecated and will be removed
6800+
in a future version.
6801+
67976802
convert_string : bool, default True
67986803
Whether object dtypes should be converted to ``StringDtype()``.
6804+
6805+
.. deprecated:: 3.1.0
6806+
The ``convert_string`` keyword is deprecated and will be removed
6807+
in a future version.
6808+
67996809
convert_integer : bool, default True
68006810
Whether, if possible, conversion can be done to integer extension types.
6811+
6812+
.. deprecated:: 3.1.0
6813+
The ``convert_integer`` keyword is deprecated and will be removed
6814+
in a future version.
6815+
68016816
convert_boolean : bool, defaults True
68026817
Whether object dtypes should be converted to ``BooleanDtypes()``.
6818+
6819+
.. deprecated:: 3.1.0
6820+
The ``convert_boolean`` keyword is deprecated and will be removed
6821+
in a future version.
6822+
68036823
convert_floating : bool, defaults True
68046824
Whether, if possible, conversion can be done to floating extension types.
68056825
If `convert_integer` is also True, preference will be give to integer
68066826
dtypes if the floats can be faithfully casted to integers.
6827+
6828+
.. deprecated:: 3.1.0
6829+
The ``convert_floating`` keyword is deprecated and will be removed
6830+
in a future version.
6831+
68076832
dtype_backend : {'numpy_nullable', 'pyarrow'}, default 'numpy_nullable'
68086833
Back-end data type applied to the resultant :class:`DataFrame` or
68096834
:class:`Series` (still experimental). Behaviour is as follows:
@@ -6916,6 +6941,38 @@ def convert_dtypes(
69166941
dtype: string
69176942
"""
69186943
check_dtype_backend(dtype_backend)
6944+
deprecated_args = {
6945+
"infer_objects": infer_objects,
6946+
"convert_string": convert_string,
6947+
"convert_integer": convert_integer,
6948+
"convert_boolean": convert_boolean,
6949+
"convert_floating": convert_floating,
6950+
}
6951+
for arg_name, arg_val in deprecated_args.items():
6952+
if arg_val is not lib.no_default:
6953+
warnings.warn(
6954+
f"The {arg_name} keyword in {type(self).__name__}."
6955+
f"convert_dtypes is deprecated and will be removed in a "
6956+
f"future version.",
6957+
Pandas4Warning,
6958+
stacklevel=find_stack_level(),
6959+
)
6960+
6961+
# Resolve no_default to the current default values (True)
6962+
infer_objects = infer_objects if infer_objects is not lib.no_default else True
6963+
convert_string = (
6964+
convert_string if convert_string is not lib.no_default else True
6965+
)
6966+
convert_integer = (
6967+
convert_integer if convert_integer is not lib.no_default else True
6968+
)
6969+
convert_boolean = (
6970+
convert_boolean if convert_boolean is not lib.no_default else True
6971+
)
6972+
convert_floating = (
6973+
convert_floating if convert_floating is not lib.no_default else True
6974+
)
6975+
69196976
new_mgr = self._mgr.convert_dtypes(
69206977
infer_objects=infer_objects,
69216978
convert_string=convert_string,

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,13 +2711,7 @@ def size(self) -> DataFrame | Series:
27112711
result = self._obj_1d_constructor(result)
27122712

27132713
if dtype_backend is not None:
2714-
result = result.convert_dtypes(
2715-
infer_objects=False,
2716-
convert_string=False,
2717-
convert_boolean=False,
2718-
convert_floating=False,
2719-
dtype_backend=dtype_backend,
2720-
)
2714+
result = result.convert_dtypes(dtype_backend=dtype_backend)
27212715

27222716
if not self.as_index:
27232717
result = result.rename("size").reset_index()

pandas/io/json/_json.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,7 @@ def _read_ujson(self) -> DataFrame | Series:
10471047
obj = self._get_object_parser(self.data)
10481048
if self.dtype_backend is not lib.no_default:
10491049
with option_context("future.distinguish_nan_and_na", False):
1050-
return obj.convert_dtypes(
1051-
infer_objects=False, dtype_backend=self.dtype_backend
1052-
)
1050+
return obj.convert_dtypes(dtype_backend=self.dtype_backend)
10531051
else:
10541052
return obj
10551053

@@ -1134,9 +1132,7 @@ def __next__(self) -> DataFrame | Series:
11341132

11351133
if self.dtype_backend is not lib.no_default:
11361134
with option_context("future.distinguish_nan_and_na", False):
1137-
return obj.convert_dtypes(
1138-
infer_objects=False, dtype_backend=self.dtype_backend
1139-
)
1135+
return obj.convert_dtypes(dtype_backend=self.dtype_backend)
11401136
else:
11411137
return obj
11421138

pandas/tests/copy_view/test_astype.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.errors import Pandas4Warning
7+
68
from pandas import (
79
DataFrame,
810
Series,
@@ -199,12 +201,13 @@ def test_astype_arrow_timestamp():
199201
def test_convert_dtypes_infer_objects():
200202
ser = Series(["a", "b", "c"])
201203
ser_orig = ser.copy()
202-
result = ser.convert_dtypes(
203-
convert_integer=False,
204-
convert_boolean=False,
205-
convert_floating=False,
206-
convert_string=False,
207-
)
204+
with tm.assert_produces_warning(Pandas4Warning):
205+
result = ser.convert_dtypes(
206+
convert_integer=False,
207+
convert_boolean=False,
208+
convert_floating=False,
209+
convert_string=False,
210+
)
208211

209212
assert tm.shares_memory(get_array(ser), get_array(result))
210213
result.iloc[0] = "x"

pandas/tests/frame/methods/test_convert_dtypes.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.errors import Pandas4Warning
67
import pandas.util._test_decorators as td
78

89
import pandas as pd
@@ -23,7 +24,8 @@ def test_convert_dtypes(self, convert_integer, expected, string_storage):
2324
}
2425
)
2526
with pd.option_context("string_storage", string_storage):
26-
result = df.convert_dtypes(True, True, convert_integer, False)
27+
with tm.assert_produces_warning(Pandas4Warning):
28+
result = df.convert_dtypes(True, True, convert_integer, False)
2729
expected = pd.DataFrame(
2830
{
2931
"a": pd.Series([1, 2, 3], dtype=expected),
@@ -168,13 +170,14 @@ def test_pyarrow_backend_no_conversion(self):
168170
pytest.importorskip("pyarrow")
169171
df = pd.DataFrame({"a": [1, 2], "b": 1.5, "c": True, "d": "x"})
170172
expected = df.copy()
171-
result = df.convert_dtypes(
172-
convert_floating=False,
173-
convert_integer=False,
174-
convert_boolean=False,
175-
convert_string=False,
176-
dtype_backend="pyarrow",
177-
)
173+
with tm.assert_produces_warning(Pandas4Warning):
174+
result = df.convert_dtypes(
175+
convert_floating=False,
176+
convert_integer=False,
177+
convert_boolean=False,
178+
convert_string=False,
179+
dtype_backend="pyarrow",
180+
)
178181
tm.assert_frame_equal(result, expected)
179182

180183
def test_convert_dtypes_pyarrow_to_np_nullable(self):
@@ -196,7 +199,8 @@ def test_convert_dtypes_pyarrow_timestamp(self):
196199
def test_convert_dtypes_avoid_block_splitting(self):
197200
# GH#55341
198201
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": "a"})
199-
result = df.convert_dtypes(convert_integer=False)
202+
with tm.assert_produces_warning(Pandas4Warning):
203+
result = df.convert_dtypes(convert_integer=False)
200204
expected = pd.DataFrame(
201205
{
202206
"a": [1, 2, 3],
@@ -253,3 +257,20 @@ def test_convert_dtypes_mixed_column_after_slice(self):
253257
}
254258
)
255259
tm.assert_frame_equal(result, expected)
260+
261+
@pytest.mark.parametrize(
262+
"kwarg",
263+
[
264+
"infer_objects",
265+
"convert_string",
266+
"convert_integer",
267+
"convert_boolean",
268+
"convert_floating",
269+
],
270+
)
271+
def test_convert_dtypes_deprecated_kwargs(self, kwarg):
272+
# GH#62022
273+
df = pd.DataFrame({"a": [1, 2, 3]})
274+
msg = f"The {kwarg} keyword in DataFrame.convert_dtypes"
275+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
276+
df.convert_dtypes(**{kwarg: True})

pandas/tests/groupby/aggregate/test_cython.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
from pandas.errors import Pandas4Warning
9+
810
from pandas.core.dtypes.common import (
911
is_float_dtype,
1012
is_integer_dtype,
@@ -326,7 +328,8 @@ def test_cython_agg_nullable_int(op_name):
326328
convert_integer = False
327329
else:
328330
convert_integer = True
329-
expected = expected.convert_dtypes(convert_integer=convert_integer)
331+
with tm.assert_produces_warning(Pandas4Warning):
332+
expected = expected.convert_dtypes(convert_integer=convert_integer)
330333
tm.assert_series_equal(result, expected)
331334

332335

pandas/tests/series/methods/test_convert_dtypes.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from pandas._libs import lib
7+
from pandas.errors import Pandas4Warning
78
import pandas.util._test_decorators as td
89

910
import pandas as pd
@@ -200,7 +201,8 @@ def test_convert_dtypes(
200201
else:
201202
series = pd.Series(data)
202203

203-
result = series.convert_dtypes(*params)
204+
with tm.assert_produces_warning(Pandas4Warning):
205+
result = series.convert_dtypes(*params)
204206

205207
param_names = [
206208
"infer_objects",
@@ -283,7 +285,8 @@ def test_convert_byte_string_dtype(self):
283285
def test_convert_dtype_object_with_na(self, infer_objects, dtype):
284286
# GH#48791
285287
ser = pd.Series([1, pd.NA])
286-
result = ser.convert_dtypes(infer_objects=infer_objects)
288+
with tm.assert_produces_warning(Pandas4Warning):
289+
result = ser.convert_dtypes(infer_objects=infer_objects)
287290
expected = pd.Series([1, pd.NA], dtype=dtype)
288291
tm.assert_series_equal(result, expected)
289292

@@ -293,7 +296,8 @@ def test_convert_dtype_object_with_na(self, infer_objects, dtype):
293296
def test_convert_dtype_object_with_na_float(self, infer_objects, dtype):
294297
# GH#48791
295298
ser = pd.Series([1.5, pd.NA])
296-
result = ser.convert_dtypes(infer_objects=infer_objects)
299+
with tm.assert_produces_warning(Pandas4Warning):
300+
result = ser.convert_dtypes(infer_objects=infer_objects)
297301
expected = pd.Series([1.5, pd.NA], dtype=dtype)
298302
tm.assert_series_equal(result, expected)
299303

0 commit comments

Comments
 (0)