-
-
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 15 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,14 @@ 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 | ||
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 | ||
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. maybe specify what happens of there is an error? (original is returned) |
||
|
||
.. versionadded:: 0.20.0 | ||
|
||
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 +3095,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 +3116,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 %s. "\ | ||
"Supplied value is '%s'" % (', '.join("'%s'" % arg for arg in | ||
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. style comment: can you do the string continuation with putting 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. Will do. I'll change the output message to:
This will do away with the need for the messy formatting. 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. @m-charlton can you change this to the {} formatting syntax? 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. Will do |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -553,7 +553,7 @@ def test_astype(self): | |
'e: f4; f: f2; g: f8') | ||
for t in ['float16', 'float32', 'float64', 'int32', 'int64']: | ||
t = np.dtype(t) | ||
tmgr = mgr.astype(t, raise_on_error=False) | ||
tmgr = mgr.astype(t, errors='ignore') | ||
self.assertEqual(tmgr.get('c').dtype.type, t) | ||
self.assertEqual(tmgr.get('e').dtype.type, t) | ||
self.assertEqual(tmgr.get('f').dtype.type, t) | ||
|
@@ -566,6 +566,13 @@ 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 """ | ||
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 turn this into a normal comment (with 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 would also move the test to the high-level 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. will do 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 looked at the existing test cases in I was thinking of adding a new file called say 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. no just grep for astype and you will see lots of tests 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. ok this test is fine here. 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. @m-charlton astype tests are located in pandas/tests/frame/test_dtypes.py (you actually updated some tests there) 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. Will do |
||
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 """ | ||
|
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)