Skip to content

Commit 66e9a99

Browse files
committed
API: convert_objects, xref pandas-dev#11116, instead of warning, raise a ValueError on old-style input
- raise a ValueError for df.convert_objects('coerce') - raise a ValueError for df.convert_objects(convert_dates='coerce') (and convert_numeric,convert_timedelta)
1 parent 1b2583b commit 66e9a99

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

pandas/core/common.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Misc tools for implementing data structures
33
"""
44

5+
import warnings
56
import re
67
import collections
78
import numbers
@@ -1868,7 +1869,12 @@ def _possibly_convert_objects(values,
18681869

18691870
conversion_count = sum((datetime, numeric, timedelta))
18701871
if conversion_count == 0:
1871-
import warnings
1872+
1873+
if coerce:
1874+
raise ValueError("coerce=True was provided, with no options for conversions."
1875+
"excatly one of 'datetime', 'numeric' or "
1876+
"'timedelta' must be True when when coerce=True.")
1877+
18721878
warnings.warn('Must explicitly pass type for conversion. Defaulting to '
18731879
'pre-0.17 behavior where datetime=True, numeric=True, '
18741880
'timedelta=True and coerce=False', DeprecationWarning)

pandas/core/generic.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,24 +2564,11 @@ def convert_objects(self, datetime=False, numeric=False,
25642564
converted : same as input object
25652565
"""
25662566

2567-
# Deprecation code to handle usage change
2568-
issue_warning = False
2569-
if datetime == 'coerce':
2570-
datetime = coerce = True
2571-
numeric = timedelta = False
2572-
issue_warning = True
2573-
elif numeric == 'coerce':
2574-
numeric = coerce = True
2575-
datetime = timedelta = False
2576-
issue_warning = True
2577-
elif timedelta == 'coerce':
2578-
timedelta = coerce = True
2579-
datetime = numeric = False
2580-
issue_warning = True
2581-
if issue_warning:
2582-
warnings.warn("The use of 'coerce' as an input is deprecated. "
2583-
"Instead set coerce=True.",
2584-
FutureWarning)
2567+
# passing 'coerce' is deprecated, but we don't want to accidently
2568+
# force conversions
2569+
if datetime == 'coerce' or numeric == 'coerce' or timedelta == 'coerce':
2570+
raise ValueError("The use of 'coerce' as an input is deprecated. "
2571+
"Instead set coerce=True.")
25852572

25862573
return self._constructor(
25872574
self._data.convert(datetime=datetime,

pandas/tests/test_series.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,29 +6492,34 @@ def test_convert_objects(self):
64926492
# Remove test after deprecation to convert_objects is final
64936493
def test_convert_objects_old_style_deprecation(self):
64946494
s = Series(['foo', 'bar', 1, 1.0], dtype='O')
6495-
with warnings.catch_warnings(record=True) as w:
6496-
warnings.simplefilter('always', FutureWarning)
6497-
new_style = s.convert_objects(datetime=True, coerce=True)
6498-
old_style = s.convert_objects(convert_dates='coerce')
6499-
self.assertEqual(len(w), 2)
6500-
assert_series_equal(new_style, old_style)
6501-
6502-
with warnings.catch_warnings(record=True) as w:
6503-
warnings.simplefilter('always', FutureWarning)
6504-
new_style = s.convert_objects(numeric=True, coerce=True)
6505-
old_style = s.convert_objects(convert_numeric='coerce')
6506-
self.assertEqual(len(w), 2)
6507-
assert_series_equal(new_style, old_style)
6495+
def f():
6496+
with tm.assert_produces_warning(FutureWarning):
6497+
s.convert_objects(convert_dates='coerce')
6498+
self.assertRaises(ValueError, f)
6499+
def f():
6500+
with tm.assert_produces_warning(FutureWarning):
6501+
s.convert_objects(convert_numeric='coerce')
6502+
self.assertRaises(ValueError, f)
65086503

65096504
dt = datetime(2001, 1, 1, 0, 0)
65106505
td = dt - datetime(2000, 1, 1, 0, 0)
65116506
s = Series(['a', '3.1415', dt, td])
6512-
with warnings.catch_warnings(record=True) as w:
6513-
warnings.simplefilter('always', FutureWarning)
6514-
new_style = s.convert_objects(timedelta=True, coerce=True)
6515-
old_style = s.convert_objects(convert_timedeltas='coerce')
6516-
self.assertEqual(len(w), 2)
6517-
assert_series_equal(new_style, old_style)
6507+
6508+
def f():
6509+
with tm.assert_produces_warning(FutureWarning):
6510+
s.convert_objects(convert_timedeltas='coerce')
6511+
self.assertRaises(ValueError, f)
6512+
6513+
# back-compat xref GH 11116
6514+
data = """foo,bar
6515+
2015-09-14,True
6516+
2015-09-15,
6517+
"""
6518+
df = pd.read_csv(StringIO(data),sep=',')
6519+
6520+
# we want to be vocal about the changes
6521+
self.assertRaises(ValueError, lambda : df.convert_objects(coerce=True))
6522+
self.assertRaises(ValueError, lambda : df.convert_objects('coerce'))
65186523

65196524
def test_convert_objects_no_arg_warning(self):
65206525
s = Series(['1.0','2'])

0 commit comments

Comments
 (0)