Skip to content

Commit 39314e6

Browse files
committed
Update after code review pandas-dev#1478
Tests added for deprected 'raise_on_error' kwarg & new 'errors' kwarg. Clarified docstring for DataFrame.astype method
1 parent 15bdcf4 commit 39314e6

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

pandas/core/generic.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3075,11 +3075,12 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
30753075
the same type. Alternatively, use {col: dtype, ...}, where col is a
30763076
column label and dtype is a numpy.dtype or Python type to cast one
30773077
or more of the DataFrame's columns to column-specific types.
3078-
raise_on_error : raise on invalid input. DEPRECATED use ``errors``
3079-
instead
3080-
errors : {'raise', 'ignore'}, default 'raise'
3081-
- ``raise`` : allow exceptions to be raised on invalid input
3082-
- ``ignore`` : suppress raising exceptions on invalid input
3078+
raise_on_error : DEPRECATED use ``errors`` instead
3079+
errors : {'raise', 'ignore'}, default 'raise'.
3080+
Control raising of exceptions on invalid data for provided dtype.
3081+
3082+
- ``raise`` : allow exceptions to be raised
3083+
- ``ignore`` : suppress exceptions. On error return original object
30833084
30843085
.. versionadded:: 0.20.0
30853086

pandas/core/internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,9 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
468468
errors_legal_values = ('raise', 'ignore')
469469

470470
if errors not in errors_legal_values:
471-
invalid_arg = "Expected value of kwarg 'errors' to be one of %s. "\
472-
"Supplied value is '%s'" % (', '.join("'%s'" % arg for arg in
473-
errors_legal_values), errors)
471+
invalid_arg = ("Expected value of kwarg 'errors' to be one of {}. "
472+
"Supplied value is '{}'".format(
473+
list(errors_legal_values), errors))
474474
raise ValueError(invalid_arg)
475475

476476
# may need to convert to categorical

pandas/tests/frame/test_dtypes.py

+18
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,24 @@ def test_timedeltas(self):
523523
result = df.get_dtype_counts().sort_values()
524524
assert_series_equal(result, expected)
525525

526+
def test_illegal_arg_for_errors_in_astype(self):
527+
# issue #14878
528+
529+
df = DataFrame([1, 2, 3])
530+
531+
with self.assertRaises(ValueError):
532+
df.astype(np.float64, errors=True)
533+
534+
def test_depr_kwarg_produces_future_warning(self):
535+
# issue #14878
536+
537+
df = DataFrame([1, 2, 3])
538+
539+
with tm.assert_produces_warning(FutureWarning):
540+
df.astype(np.int8, raise_on_error=False)
541+
542+
df.astype(np.int8, errors='ignore')
543+
526544

527545
class TestDataFrameDatetimeWithTZ(tm.TestCase, TestData):
528546

pandas/tests/test_internals.py

-7
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,6 @@ def test_astype(self):
566566
else:
567567
self.assertEqual(tmgr.get('d').dtype.type, t)
568568

569-
def test_illegal_arg_for_errors_in_astype(self):
570-
""" ValueError exception raised when illegal value used for errors """
571-
mgr = create_mgr('a,b,c: i8')
572-
573-
with self.assertRaises(ValueError):
574-
mgr.astype(np.float64, errors=True)
575-
576569
def test_convert(self):
577570
def _compare(old_mgr, new_mgr):
578571
""" compare the blocks, numeric compare ==, object don't """

0 commit comments

Comments
 (0)