diff --git a/pandas/core/common.py b/pandas/core/common.py index 9189b0d89de4f..5784f7966d1f6 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -2,6 +2,7 @@ Misc tools for implementing data structures """ +import warnings import re import collections import numbers @@ -1868,7 +1869,12 @@ def _possibly_convert_objects(values, conversion_count = sum((datetime, numeric, timedelta)) if conversion_count == 0: - import warnings + + if coerce: + raise ValueError("coerce=True was provided, with no options for conversions." + "excatly one of 'datetime', 'numeric' or " + "'timedelta' must be True when when coerce=True.") + warnings.warn('Must explicitly pass type for conversion. Defaulting to ' 'pre-0.17 behavior where datetime=True, numeric=True, ' 'timedelta=True and coerce=False', DeprecationWarning) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6aec297c31d2b..152e4ce465a8e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2564,24 +2564,11 @@ def convert_objects(self, datetime=False, numeric=False, converted : same as input object """ - # Deprecation code to handle usage change - issue_warning = False - if datetime == 'coerce': - datetime = coerce = True - numeric = timedelta = False - issue_warning = True - elif numeric == 'coerce': - numeric = coerce = True - datetime = timedelta = False - issue_warning = True - elif timedelta == 'coerce': - timedelta = coerce = True - datetime = numeric = False - issue_warning = True - if issue_warning: - warnings.warn("The use of 'coerce' as an input is deprecated. " - "Instead set coerce=True.", - FutureWarning) + # passing 'coerce' is deprecated, but we don't want to accidently + # force conversions + if datetime == 'coerce' or numeric == 'coerce' or timedelta == 'coerce': + raise ValueError("The use of 'coerce' as an input is deprecated. " + "Instead set coerce=True.") return self._constructor( self._data.convert(datetime=datetime, diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 7fb89b30cec97..7b7cdc9b7a19d 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -6492,29 +6492,34 @@ def test_convert_objects(self): # Remove test after deprecation to convert_objects is final def test_convert_objects_old_style_deprecation(self): s = Series(['foo', 'bar', 1, 1.0], dtype='O') - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', FutureWarning) - new_style = s.convert_objects(datetime=True, coerce=True) - old_style = s.convert_objects(convert_dates='coerce') - self.assertEqual(len(w), 2) - assert_series_equal(new_style, old_style) - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', FutureWarning) - new_style = s.convert_objects(numeric=True, coerce=True) - old_style = s.convert_objects(convert_numeric='coerce') - self.assertEqual(len(w), 2) - assert_series_equal(new_style, old_style) + def f(): + with tm.assert_produces_warning(FutureWarning): + s.convert_objects(convert_dates='coerce') + self.assertRaises(ValueError, f) + def f(): + with tm.assert_produces_warning(FutureWarning): + s.convert_objects(convert_numeric='coerce') + self.assertRaises(ValueError, f) dt = datetime(2001, 1, 1, 0, 0) td = dt - datetime(2000, 1, 1, 0, 0) s = Series(['a', '3.1415', dt, td]) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', FutureWarning) - new_style = s.convert_objects(timedelta=True, coerce=True) - old_style = s.convert_objects(convert_timedeltas='coerce') - self.assertEqual(len(w), 2) - assert_series_equal(new_style, old_style) + + def f(): + with tm.assert_produces_warning(FutureWarning): + s.convert_objects(convert_timedeltas='coerce') + self.assertRaises(ValueError, f) + + # back-compat xref GH 11116 + data = """foo,bar +2015-09-14,True +2015-09-15, +""" + df = pd.read_csv(StringIO(data),sep=',') + + # we want to be vocal about the changes + self.assertRaises(ValueError, lambda : df.convert_objects(coerce=True)) + self.assertRaises(ValueError, lambda : df.convert_objects('coerce')) def test_convert_objects_no_arg_warning(self): s = Series(['1.0','2'])