Skip to content

Commit 15bdcf4

Browse files
committed
Check & unit test added for validity of arguments to 'errors' kwarg. Docstrings
to astype clarified.
1 parent e2a3f32 commit 15bdcf4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pandas/core/generic.py

+5
Original file line numberDiff line numberDiff line change
@@ -3075,9 +3075,14 @@ 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
30783080
errors : {'raise', 'ignore'}, default 'raise'
30793081
- ``raise`` : allow exceptions to be raised on invalid input
30803082
- ``ignore`` : suppress raising exceptions on invalid input
3083+
3084+
.. versionadded:: 0.20.0
3085+
30813086
kwargs : keyword arguments to pass on to the constructor
30823087
30833088
Returns

pandas/core/internals.py

+7
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,13 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
465465
Coerce to the new type (if copy=True, return a new copy)
466466
raise on an except if raise == True
467467
"""
468+
errors_legal_values = ('raise', 'ignore')
469+
470+
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)
474+
raise ValueError(invalid_arg)
468475

469476
# may need to convert to categorical
470477
# this is only called for non-categoricals

pandas/tests/test_internals.py

+7
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ 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+
569576
def test_convert(self):
570577
def _compare(old_mgr, new_mgr):
571578
""" compare the blocks, numeric compare ==, object don't """

0 commit comments

Comments
 (0)