-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Depr] raise_on_error kwarg with errors kwarg in astype#14878 #14967
Changes from all commits
3f5d728
fddbb2e
a61ec51
e714d8c
136175c
6282de6
0c90785
bc65ff4
cba2b89
5626cdd
4eb7126
6467390
f51dbdf
e2a3f32
15bdcf4
39314e6
0ae8550
b174e6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3061,7 +3061,9 @@ def blocks(self): | |
"""Internal property, property synonym for as_blocks()""" | ||
return self.as_blocks() | ||
|
||
def astype(self, dtype, copy=True, raise_on_error=True, **kwargs): | ||
@deprecate_kwarg(old_arg_name='raise_on_error', new_arg_name='errors', | ||
mapping={True: 'raise', False: 'ignore'}) | ||
def astype(self, dtype, copy=True, errors='raise', **kwargs): | ||
""" | ||
Cast object to input numpy.dtype | ||
Return a copy when copy = True (be really careful with this!) | ||
|
@@ -3073,7 +3075,15 @@ def astype(self, dtype, copy=True, raise_on_error=True, **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 | ||
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 | ||
|
||
raise_on_error : DEPRECATED use ``errors`` instead | ||
kwargs : keyword arguments to pass on to the constructor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need a blank line before kwargs (to make the sub-list work) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just checked and the sublist is rendered fine with, or without a line between |
||
|
||
Returns | ||
|
@@ -3086,7 +3096,7 @@ def astype(self, dtype, copy=True, raise_on_error=True, **kwargs): | |
raise KeyError('Only the Series name can be used for ' | ||
'the key in Series dtype mappings.') | ||
new_type = list(dtype.values())[0] | ||
return self.astype(new_type, copy, raise_on_error, **kwargs) | ||
return self.astype(new_type, copy, errors, **kwargs) | ||
elif self.ndim > 2: | ||
raise NotImplementedError( | ||
'astype() only accepts a dtype arg of type dict when ' | ||
|
@@ -3107,8 +3117,8 @@ def astype(self, dtype, copy=True, raise_on_error=True, **kwargs): | |
return concat(results, axis=1, copy=False) | ||
|
||
# else, only a single dtype is given | ||
new_data = self._data.astype(dtype=dtype, copy=copy, | ||
raise_on_error=raise_on_error, **kwargs) | ||
new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors, | ||
**kwargs) | ||
return self._constructor(new_data).__finalize__(self) | ||
|
||
def copy(self, deep=True): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -455,17 +455,23 @@ def downcast(self, dtypes=None, mgr=None): | |
|
||
return blocks | ||
|
||
def astype(self, dtype, copy=False, raise_on_error=True, values=None, | ||
**kwargs): | ||
return self._astype(dtype, copy=copy, raise_on_error=raise_on_error, | ||
values=values, **kwargs) | ||
def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs): | ||
return self._astype(dtype, copy=copy, errors=errors, values=values, | ||
**kwargs) | ||
|
||
def _astype(self, dtype, copy=False, raise_on_error=True, values=None, | ||
def _astype(self, dtype, copy=False, errors='raise', values=None, | ||
klass=None, mgr=None, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you check the errors in ['raise', 'ignore'] at the beginning of the function and raise a ValueError otherwise (and add a test for this) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, there are two 'public'
In addition there is a 'protected' Bearing in mind that the I notice that both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block is completely internal we have centralized checks but no need in this case out in _astype as that's where it's actually used |
||
""" | ||
Coerce to the new type (if copy=True, return a new copy) | ||
raise on an except if raise == True | ||
""" | ||
errors_legal_values = ('raise', 'ignore') | ||
|
||
if errors not in errors_legal_values: | ||
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 | ||
# this is only called for non-categoricals | ||
|
@@ -507,7 +513,7 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None, | |
newb = make_block(values, placement=self.mgr_locs, dtype=dtype, | ||
klass=klass) | ||
except: | ||
if raise_on_error is True: | ||
if errors == 'raise': | ||
raise | ||
newb = self.copy() if copy else self | ||
|
||
|
@@ -2147,7 +2153,7 @@ def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None): | |
|
||
return self.make_block_same_class(new_values, new_mgr_locs) | ||
|
||
def _astype(self, dtype, copy=False, raise_on_error=True, values=None, | ||
def _astype(self, dtype, copy=False, errors='raise', values=None, | ||
klass=None, mgr=None): | ||
""" | ||
Coerce to the new type (if copy=True, return a new copy) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave this in here and just say DEPRECATED.
@jorisvandenbossche IIRC that is our convention?
add a versionadded tag 0.20.0 for errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, can leave it (but put it at the end)