Skip to content

Commit b2756a7

Browse files
committed
Merge pull request #10602 from bashtage/convert-object-dep-fix
BUG: Fix issue with old-style usage in convert_objects
2 parents 0deb21e + 6ceb303 commit b2756a7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pandas/core/generic.py

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import operator
44
import weakref
55
import gc
6+
67
import numpy as np
78
import pandas.lib as lib
89

@@ -27,6 +28,7 @@
2728
from pandas.util.decorators import Appender, Substitution, deprecate_kwarg
2829
from pandas.core import config
2930

31+
3032
# goal is to be able to define the docs close to function, while still being
3133
# able to share
3234
_shared_docs = dict()
@@ -2559,6 +2561,26 @@ def convert_objects(self, datetime=False, numeric=False,
25592561
-------
25602562
converted : same as input object
25612563
"""
2564+
2565+
# Deprecation code to handle usage change
2566+
issue_warning = False
2567+
if datetime == 'coerce':
2568+
datetime = coerce = True
2569+
numeric = timedelta = False
2570+
issue_warning = True
2571+
elif numeric == 'coerce':
2572+
numeric = coerce = True
2573+
datetime = timedelta = False
2574+
issue_warning = True
2575+
elif timedelta == 'coerce':
2576+
timedelta = coerce = True
2577+
datetime = numeric = False
2578+
issue_warning = True
2579+
if issue_warning:
2580+
warnings.warn("The use of 'coerce' as an input is deprecated. "
2581+
"Instead set coerce=True.",
2582+
FutureWarning)
2583+
25622584
return self._constructor(
25632585
self._data.convert(datetime=datetime,
25642586
numeric=numeric,

pandas/tests/test_series.py

+28
Original file line numberDiff line numberDiff line change
@@ -6469,6 +6469,34 @@ def test_convert_objects(self):
64696469
result = s.convert_objects(datetime=True, coerce=True)
64706470
assert_series_equal(result, expected)
64716471

6472+
# GH 10601
6473+
# Remove test after deprecation to convert_objects is final
6474+
def test_convert_objects_old_style_deprecation(self):
6475+
s = Series(['foo', 'bar', 1, 1.0], dtype='O')
6476+
with warnings.catch_warnings(record=True) as w:
6477+
warnings.simplefilter('always', FutureWarning)
6478+
new_style = s.convert_objects(datetime=True, coerce=True)
6479+
old_style = s.convert_objects(convert_dates='coerce')
6480+
self.assertEqual(len(w), 2)
6481+
assert_series_equal(new_style, old_style)
6482+
6483+
with warnings.catch_warnings(record=True) as w:
6484+
warnings.simplefilter('always', FutureWarning)
6485+
new_style = s.convert_objects(numeric=True, coerce=True)
6486+
old_style = s.convert_objects(convert_numeric='coerce')
6487+
self.assertEqual(len(w), 2)
6488+
assert_series_equal(new_style, old_style)
6489+
6490+
dt = datetime(2001, 1, 1, 0, 0)
6491+
td = dt - datetime(2000, 1, 1, 0, 0)
6492+
s = Series(['a', '3.1415', dt, td])
6493+
with warnings.catch_warnings(record=True) as w:
6494+
warnings.simplefilter('always', FutureWarning)
6495+
new_style = s.convert_objects(timedelta=True, coerce=True)
6496+
old_style = s.convert_objects(convert_timedeltas='coerce')
6497+
self.assertEqual(len(w), 2)
6498+
assert_series_equal(new_style, old_style)
6499+
64726500
def test_convert_objects_no_arg_warning(self):
64736501
s = Series(['1.0','2'])
64746502
with warnings.catch_warnings(record=True) as w:

0 commit comments

Comments
 (0)