Skip to content

Commit 6ceb303

Browse files
author
Kevin Sheppard
committed
BUG: Fix issue with old-style usage in convert_objects
Fix to temporary allow passing 'coerce' to variables closes #10601
1 parent 931e0e5 commit 6ceb303

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pandas/core/generic.py

Lines changed: 22 additions & 0 deletions
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()
@@ -2473,6 +2475,26 @@ def convert_objects(self, datetime=False, numeric=False,
24732475
-------
24742476
converted : same as input object
24752477
"""
2478+
2479+
# Deprecation code to handle usage change
2480+
issue_warning = False
2481+
if datetime == 'coerce':
2482+
datetime = coerce = True
2483+
numeric = timedelta = False
2484+
issue_warning = True
2485+
elif numeric == 'coerce':
2486+
numeric = coerce = True
2487+
datetime = timedelta = False
2488+
issue_warning = True
2489+
elif timedelta == 'coerce':
2490+
timedelta = coerce = True
2491+
datetime = numeric = False
2492+
issue_warning = True
2493+
if issue_warning:
2494+
warnings.warn("The use of 'coerce' as an input is deprecated. "
2495+
"Instead set coerce=True.",
2496+
FutureWarning)
2497+
24762498
return self._constructor(
24772499
self._data.convert(datetime=datetime,
24782500
numeric=numeric,

pandas/tests/test_series.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6209,6 +6209,34 @@ def test_convert_objects(self):
62096209
result = s.convert_objects(datetime=True, coerce=True)
62106210
assert_series_equal(result, expected)
62116211

6212+
# GH 10601
6213+
# Remove test after deprecation to convert_objects is final
6214+
def test_convert_objects_old_style_deprecation(self):
6215+
s = Series(['foo', 'bar', 1, 1.0], dtype='O')
6216+
with warnings.catch_warnings(record=True) as w:
6217+
warnings.simplefilter('always', FutureWarning)
6218+
new_style = s.convert_objects(datetime=True, coerce=True)
6219+
old_style = s.convert_objects(convert_dates='coerce')
6220+
self.assertEqual(len(w), 2)
6221+
assert_series_equal(new_style, old_style)
6222+
6223+
with warnings.catch_warnings(record=True) as w:
6224+
warnings.simplefilter('always', FutureWarning)
6225+
new_style = s.convert_objects(numeric=True, coerce=True)
6226+
old_style = s.convert_objects(convert_numeric='coerce')
6227+
self.assertEqual(len(w), 2)
6228+
assert_series_equal(new_style, old_style)
6229+
6230+
dt = datetime(2001, 1, 1, 0, 0)
6231+
td = dt - datetime(2000, 1, 1, 0, 0)
6232+
s = Series(['a', '3.1415', dt, td])
6233+
with warnings.catch_warnings(record=True) as w:
6234+
warnings.simplefilter('always', FutureWarning)
6235+
new_style = s.convert_objects(timedelta=True, coerce=True)
6236+
old_style = s.convert_objects(convert_timedeltas='coerce')
6237+
self.assertEqual(len(w), 2)
6238+
assert_series_equal(new_style, old_style)
6239+
62126240
def test_convert_objects_no_arg_warning(self):
62136241
s = Series(['1.0','2'])
62146242
with warnings.catch_warnings(record=True) as w:

0 commit comments

Comments
 (0)