-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
API: Validate keyword arguments to fillna #19684
Changes from 5 commits
bfb868b
dddc7cf
f810f67
3919da4
c280eaa
96c1754
6c09f08
90f8fde
38aeecf
dc1f960
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 |
---|---|---|
|
@@ -40,7 +40,7 @@ | |
Appender, cache_readonly, deprecate_kwarg, Substitution) | ||
|
||
from pandas.io.formats.terminal import get_terminal_size | ||
from pandas.util._validators import validate_bool_kwarg | ||
from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs | ||
from pandas.core.config import get_option | ||
|
||
from .base import ExtensionArray | ||
|
@@ -1607,6 +1607,9 @@ def fillna(self, value=None, method=None, limit=None): | |
------- | ||
filled : Categorical with NA/NaN filled | ||
""" | ||
value, method = validate_fillna_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. Note, I added this keyword since it's possible have a tuple / list for a 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. Do we need to add a test for this? (I mean filling a categorical with such categories, so the purpose of |
||
value, method, validate_scalar_dict_value=False | ||
) | ||
|
||
if value is None: | ||
value = np.nan | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -320,3 +320,39 @@ def validate_axis_style_args(data, args, kwargs, arg_name, method_name): | |
msg = "Cannot specify all of '{}', 'index', 'columns'." | ||
raise TypeError(msg.format(arg_name)) | ||
return out | ||
|
||
|
||
def validate_fillna_kwargs(value, method, validate_scalar_dict_value=True): | ||
"""Validate the keyword arguments to 'fillna'. | ||
|
||
This checks that exactly one of 'value' and 'method' is specified. | ||
If 'method' is specified, this validates that it's a valid method. | ||
|
||
Parameters | ||
---------- | ||
value, method : object | ||
The 'value' and 'method' keyword arguments for 'fillna'. | ||
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. What happened to |
||
validate_scalar_dict_value : bool, default True | ||
Whether to validate that 'value' is a scalar or dict; specifically | ||
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. Nit: replace the semicolon with a comma. |
||
that it is not a list or tuple. | ||
|
||
Returns | ||
------- | ||
value, method : object | ||
""" | ||
from pandas.core.missing import clean_fill_method | ||
|
||
if value is None and method is None: | ||
raise ValueError("Must specify a fill 'value' or 'method'.") | ||
elif value is None and method is not None: | ||
method = clean_fill_method(method) | ||
|
||
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. extra line here (make consistent across this function) |
||
elif value is not None and method is None: | ||
if validate_scalar_dict_value and isinstance(value, (list, tuple)): | ||
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. This check doesn't seem especially useful / correct given the error message. There are many things that are not scalars or dicts, but also not But I was trying to keep this as a straight refactor for NDFrame.fillna, with the only behavior change being 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 dont' think this is odd to want to validate that a scalar can be only a certain type. maybe would change 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. this parameter name is terrible, ideally would like to change 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. Two things:
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. the parameter name is terrible, obviously NOT the contributor, which I didn't say or imply at all. It needs to be changed. 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. The parameter name is certainly descriptive in what it does, and it is for an internal method, so it's not much of a problem it is that long. Why bother for the rest? |
||
raise TypeError('"value" parameter must be a scalar or dict, but ' | ||
'you passed a "{0}"'.format(type(value).__name__)) | ||
|
||
elif value is not None and method is not None: | ||
raise ValueError("Cannot specify both 'value' and 'method'.") | ||
|
||
return value, method |
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.
there is a datetimelike section for other api changes now