Skip to content

Commit

Permalink
Update after code review pandas-dev#1478
Browse files Browse the repository at this point in the history
Tests added for deprected 'raise_on_error' kwarg & new 'errors' kwarg.
Clarified docstring for DataFrame.astype method
  • Loading branch information
m-charlton committed Jan 3, 2017
1 parent 15bdcf4 commit 39314e6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
11 changes: 6 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3075,11 +3075,12 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
the same type. Alternatively, use {col: dtype, ...}, where col is a
column label and dtype is a numpy.dtype or Python type to cast one
or more of the DataFrame's columns to column-specific types.
raise_on_error : raise on invalid input. DEPRECATED use ``errors``
instead
errors : {'raise', 'ignore'}, default 'raise'
- ``raise`` : allow exceptions to be raised on invalid input
- ``ignore`` : suppress raising exceptions on invalid input
raise_on_error : DEPRECATED use ``errors`` instead
errors : {'raise', 'ignore'}, default 'raise'.
Control raising of exceptions on invalid data for provided dtype.
- ``raise`` : allow exceptions to be raised
- ``ignore`` : suppress exceptions. On error return original object
.. versionadded:: 0.20.0
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
errors_legal_values = ('raise', 'ignore')

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

# may need to convert to categorical
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,24 @@ def test_timedeltas(self):
result = df.get_dtype_counts().sort_values()
assert_series_equal(result, expected)

def test_illegal_arg_for_errors_in_astype(self):
# issue #14878

df = DataFrame([1, 2, 3])

with self.assertRaises(ValueError):
df.astype(np.float64, errors=True)

def test_depr_kwarg_produces_future_warning(self):
# issue #14878

df = DataFrame([1, 2, 3])

with tm.assert_produces_warning(FutureWarning):
df.astype(np.int8, raise_on_error=False)

df.astype(np.int8, errors='ignore')


class TestDataFrameDatetimeWithTZ(tm.TestCase, TestData):

Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,6 @@ def test_astype(self):
else:
self.assertEqual(tmgr.get('d').dtype.type, t)

def test_illegal_arg_for_errors_in_astype(self):
""" ValueError exception raised when illegal value used for errors """
mgr = create_mgr('a,b,c: i8')

with self.assertRaises(ValueError):
mgr.astype(np.float64, errors=True)

def test_convert(self):
def _compare(old_mgr, new_mgr):
""" compare the blocks, numeric compare ==, object don't """
Expand Down

0 comments on commit 39314e6

Please sign in to comment.