-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
ENH: standardize fill_value behavior across the API #15587
Changes from all 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 |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
|
||
import pandas.core.algorithms as algos | ||
import pandas.algos as _algos | ||
from pandas.core.missing import validate_fill_value | ||
|
||
from pandas.core.index import MultiIndex, _get_na_value | ||
|
||
|
@@ -405,6 +406,9 @@ def _slow_pivot(index, columns, values): | |
|
||
|
||
def unstack(obj, level, fill_value=None): | ||
if fill_value: | ||
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) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
DatetimeIndex, TimedeltaIndex, date_range) | ||
from pandas.types.dtypes import DatetimeTZDtype | ||
from pandas.types.missing import (array_equivalent, isnull, notnull, | ||
na_value_for_dtype) | ||
na_value_for_dtype, | ||
validate_fill_value) | ||
|
||
|
||
def test_notnull(): | ||
|
@@ -301,3 +302,11 @@ def test_na_value_for_dtype(): | |
|
||
for dtype in ['O']: | ||
assert np.isnan(na_value_for_dtype(np.dtype(dtype))) | ||
|
||
|
||
class TestValidateFillValue(tm.TestCase): | ||
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. don't use a class, just create a function (and use parametrize) |
||
# TODO: Fill out the test cases. | ||
def test_validate_fill_value(self): | ||
# validate_fill_value() | ||
# import pdb; pdb.set_trace() | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,10 @@ | |
is_object_dtype, | ||
is_integer, | ||
_TD_DTYPE, | ||
_NS_DTYPE) | ||
_NS_DTYPE, | ||
is_datetime64_any_dtype, is_float, | ||
is_numeric_dtype, is_complex) | ||
from datetime import datetime, timedelta | ||
from .inference import is_list_like | ||
|
||
|
||
|
@@ -391,3 +394,30 @@ def na_value_for_dtype(dtype): | |
elif is_bool_dtype(dtype): | ||
return False | ||
return np.nan | ||
|
||
|
||
def validate_fill_value(value, dtype): | ||
""" | ||
Make sure the fill value is appropriate for the given 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. add in a Parameters, Returns, Raises section |
||
""" | ||
if not is_scalar(value): | ||
raise TypeError('"fill_value" parameter must be ' | ||
'a scalar, but you passed a ' | ||
'"{0}"'.format(type(value).__name__)) | ||
elif not isnull(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 you already check that |
||
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. |
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.
actually I would always pass this (we will make
None
an acceptable fill_value below)