Skip to content

Commit e2a3f32

Browse files
committed
Deprecate 'raise_on_error' kwarg with 'errors' for DataFrame.astype
Valid arguments for new 'errors' kwarg are 'ignore' or 'raise' see pandas-dev#14878
1 parent f51dbdf commit e2a3f32

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Deprecations
247247
- ``Index.repeat()`` and ``MultiIndex.repeat()`` have deprecated the ``n`` parameter in favor of ``repeats`` (:issue:`12662`)
248248
- ``Categorical.searchsorted()`` and ``Series.searchsorted()`` have deprecated the ``v`` parameter in favor of ``value`` (:issue:`12662`)
249249
- ``TimedeltaIndex.searchsorted()``, ``DatetimeIndex.searchsorted()``, and ``PeriodIndex.searchsorted()`` have deprecated the ``key`` parameter in favor of ``value`` (:issue:`12662`)
250-
250+
- ``DataFrame.astype()`` has deprecated the ``raise_on_error`` parameter in favor of ``errors`` (:issue:`14878`)
251251

252252

253253

pandas/core/generic.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -3061,7 +3061,9 @@ def blocks(self):
30613061
"""Internal property, property synonym for as_blocks()"""
30623062
return self.as_blocks()
30633063

3064-
def astype(self, dtype, copy=True, raise_on_error=True, **kwargs):
3064+
@deprecate_kwarg(old_arg_name='raise_on_error', new_arg_name='errors',
3065+
mapping={True: 'raise', False: 'ignore'})
3066+
def astype(self, dtype, copy=True, errors='raise', **kwargs):
30653067
"""
30663068
Cast object to input numpy.dtype
30673069
Return a copy when copy = True (be really careful with this!)
@@ -3073,7 +3075,9 @@ def astype(self, dtype, copy=True, raise_on_error=True, **kwargs):
30733075
the same type. Alternatively, use {col: dtype, ...}, where col is a
30743076
column label and dtype is a numpy.dtype or Python type to cast one
30753077
or more of the DataFrame's columns to column-specific types.
3076-
raise_on_error : raise on invalid input
3078+
errors : {'raise', 'ignore'}, default 'raise'
3079+
- ``raise`` : allow exceptions to be raised on invalid input
3080+
- ``ignore`` : suppress raising exceptions on invalid input
30773081
kwargs : keyword arguments to pass on to the constructor
30783082
30793083
Returns
@@ -3086,7 +3090,7 @@ def astype(self, dtype, copy=True, raise_on_error=True, **kwargs):
30863090
raise KeyError('Only the Series name can be used for '
30873091
'the key in Series dtype mappings.')
30883092
new_type = list(dtype.values())[0]
3089-
return self.astype(new_type, copy, raise_on_error, **kwargs)
3093+
return self.astype(new_type, copy, errors, **kwargs)
30903094
elif self.ndim > 2:
30913095
raise NotImplementedError(
30923096
'astype() only accepts a dtype arg of type dict when '
@@ -3107,8 +3111,8 @@ def astype(self, dtype, copy=True, raise_on_error=True, **kwargs):
31073111
return concat(results, axis=1, copy=False)
31083112

31093113
# else, only a single dtype is given
3110-
new_data = self._data.astype(dtype=dtype, copy=copy,
3111-
raise_on_error=raise_on_error, **kwargs)
3114+
new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
3115+
**kwargs)
31123116
return self._constructor(new_data).__finalize__(self)
31133117

31143118
def copy(self, deep=True):

pandas/core/internals.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,11 @@ def downcast(self, dtypes=None, mgr=None):
455455

456456
return blocks
457457

458-
def astype(self, dtype, copy=False, raise_on_error=True, values=None,
459-
**kwargs):
460-
return self._astype(dtype, copy=copy, raise_on_error=raise_on_error,
461-
values=values, **kwargs)
458+
def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
459+
return self._astype(dtype, copy=copy, errors=errors, values=values,
460+
**kwargs)
462461

463-
def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
462+
def _astype(self, dtype, copy=False, errors='raise', values=None,
464463
klass=None, mgr=None, **kwargs):
465464
"""
466465
Coerce to the new type (if copy=True, return a new copy)
@@ -507,7 +506,7 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
507506
newb = make_block(values, placement=self.mgr_locs, dtype=dtype,
508507
klass=klass)
509508
except:
510-
if raise_on_error is True:
509+
if errors == 'raise':
511510
raise
512511
newb = self.copy() if copy else self
513512

@@ -2147,7 +2146,7 @@ def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None):
21472146

21482147
return self.make_block_same_class(new_values, new_mgr_locs)
21492148

2150-
def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
2149+
def _astype(self, dtype, copy=False, errors='raise', values=None,
21512150
klass=None, mgr=None):
21522151
"""
21532152
Coerce to the new type (if copy=True, return a new copy)

pandas/tests/frame/test_dtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,15 @@ def test_astype_with_exclude_string(self):
357357
df = self.frame.copy()
358358
expected = self.frame.astype(int)
359359
df['string'] = 'foo'
360-
casted = df.astype(int, raise_on_error=False)
360+
casted = df.astype(int, errors='ignore')
361361

362362
expected['string'] = 'foo'
363363
assert_frame_equal(casted, expected)
364364

365365
df = self.frame.copy()
366366
expected = self.frame.astype(np.int32)
367367
df['string'] = 'foo'
368-
casted = df.astype(np.int32, raise_on_error=False)
368+
casted = df.astype(np.int32, errors='ignore')
369369

370370
expected['string'] = 'foo'
371371
assert_frame_equal(casted, expected)

pandas/tests/test_internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def test_astype(self):
553553
'e: f4; f: f2; g: f8')
554554
for t in ['float16', 'float32', 'float64', 'int32', 'int64']:
555555
t = np.dtype(t)
556-
tmgr = mgr.astype(t, raise_on_error=False)
556+
tmgr = mgr.astype(t, errors='ignore')
557557
self.assertEqual(tmgr.get('c').dtype.type, t)
558558
self.assertEqual(tmgr.get('e').dtype.type, t)
559559
self.assertEqual(tmgr.get('f').dtype.type, t)

0 commit comments

Comments
 (0)