-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: standardize fill_value behavior across the API #15587
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
Changes from 3 commits
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 |
---|---|---|
|
@@ -12,7 +12,11 @@ | |
is_float_dtype, is_datetime64_dtype, | ||
is_datetime64tz_dtype, is_integer_dtype, | ||
_ensure_float64, is_scalar, | ||
needs_i8_conversion, is_integer) | ||
needs_i8_conversion, is_integer, | ||
is_list_like, is_dict_like, | ||
is_numeric_dtype, | ||
is_datetime64_any_dtype, is_float, | ||
is_complex, is_timedelta64_dtype) | ||
from pandas.types.missing import isnull | ||
|
||
|
||
|
@@ -621,3 +625,29 @@ def fill_zeros(result, x, y, name, fill): | |
result = result.reshape(shape) | ||
|
||
return result | ||
|
||
|
||
def validate_fill_value(value, dtype): | ||
if is_list_like(value) or is_dict_like(value) or callable(value): | ||
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. why dont' you check |
||
raise TypeError('"fill_value" parameter must be ' | ||
'a scalar, but you passed a ' | ||
'"{0}"'.format(type(value).__name__)) | ||
elif not isnull(value): | ||
from datetime import datetime, timedelta | ||
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. put imports at the top of the file |
||
|
||
if is_numeric_dtype(dtype): | ||
if not (is_float(value) or is_integer(value) or is_complex(value)): | ||
raise TypeError('"fill_value" parameter must be ' | ||
'numeric, but you passed a ' | ||
'"{0}"'.format(type(value).__name__)) | ||
elif is_datetime64_any_dtype(dtype): | ||
if not isinstance(value, (np.datetime64, datetime)): | ||
raise TypeError('"fill_value" parameter must be a ' | ||
'datetime, but you passed a ' | ||
'"{0}"'.format(type(value).__name__)) | ||
elif is_timedelta64_dtype(dtype): | ||
if not isinstance(value, (np.timedelta64, timedelta)): | ||
raise TypeError('"value" parameter must be ' | ||
'a timedelta, but you passed a ' | ||
'"{0}"'.format(type(value).__name__)) | ||
# if object dtype, do nothing. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,6 +405,10 @@ def _slow_pivot(index, columns, values): | |
|
||
|
||
def unstack(obj, level, fill_value=None): | ||
if fill_value: | ||
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. actually I would always pass this (we will make |
||
from pandas.core.missing import validate_fill_value | ||
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. import at the top |
||
validate_fill_value(fill_value, obj.values.dtype) | ||
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. just pass 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.
|
||
|
||
if isinstance(level, (tuple, list)): | ||
return _unstack_multiple(obj, level) | ||
|
||
|
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.
can you add a doc-string :>