-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG/API: dtype inconsistencies in .where / .setitem / .putmask / .fillna #16821
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,7 +273,7 @@ def maybe_promote(dtype, fill_value=np.nan): | |
else: | ||
if issubclass(dtype.type, np.datetime64): | ||
try: | ||
fill_value = lib.Timestamp(fill_value).value | ||
fill_value = tslib.Timestamp(fill_value).value | ||
except: | ||
# the proper thing to do here would probably be to upcast | ||
# to object (but numpy 1.6.1 doesn't do this properly) | ||
|
@@ -334,6 +334,23 @@ def maybe_promote(dtype, fill_value=np.nan): | |
return dtype, fill_value | ||
|
||
|
||
def infer_dtype_from(val, pandas_dtype=False): | ||
""" | ||
interpret the dtype from a scalar or array. This is a convenience | ||
routines to infer dtype from a scalar or an array | ||
|
||
Parameters | ||
---------- | ||
pandas_dtype : bool, default False | ||
whether to infer dtype including pandas extension types. | ||
If False, scalar/array belongs to pandas extension types is inferred as | ||
object | ||
""" | ||
if is_scalar(val): | ||
return infer_dtype_from_scalar(val, pandas_dtype=pandas_dtype) | ||
return infer_dtype_from_array(val, pandas_dtype=pandas_dtype) | ||
|
||
|
||
def infer_dtype_from_scalar(val, pandas_dtype=False): | ||
""" | ||
interpret the dtype from a scalar | ||
|
@@ -350,9 +367,9 @@ def infer_dtype_from_scalar(val, pandas_dtype=False): | |
|
||
# a 1-element ndarray | ||
if isinstance(val, np.ndarray): | ||
msg = "invalid ndarray passed to _infer_dtype_from_scalar" | ||
if val.ndim != 0: | ||
raise ValueError( | ||
"invalid ndarray passed to _infer_dtype_from_scalar") | ||
raise ValueError(msg) | ||
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. Seems to be untested, according to code-cov. 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. done |
||
|
||
dtype = val.dtype | ||
val = val.item() | ||
|
@@ -409,24 +426,31 @@ def infer_dtype_from_scalar(val, pandas_dtype=False): | |
return dtype, val | ||
|
||
|
||
def infer_dtype_from_array(arr): | ||
def infer_dtype_from_array(arr, pandas_dtype=False): | ||
""" | ||
infer the dtype from a scalar or array | ||
|
||
Parameters | ||
---------- | ||
arr : scalar or array | ||
pandas_dtype : bool, default False | ||
whether to infer dtype including pandas extension types. | ||
If False, array belongs to pandas extension types | ||
is inferred as object | ||
|
||
Returns | ||
------- | ||
tuple (numpy-compat dtype, array) | ||
tuple (numpy-compat/pandas-compat dtype, array) | ||
|
||
Notes | ||
----- | ||
These infer to numpy dtypes exactly | ||
with the exception that mixed / object dtypes | ||
if pandas_dtype=False. these infer to numpy dtypes | ||
exactly with the exception that mixed / object dtypes | ||
are not coerced by stringifying or conversion | ||
|
||
if pandas_dtype=True. datetime64tz-aware/categorical | ||
types will retain there character. | ||
|
||
Examples | ||
-------- | ||
>>> np.asarray([1, '1']) | ||
|
@@ -443,6 +467,12 @@ def infer_dtype_from_array(arr): | |
if not is_list_like(arr): | ||
arr = [arr] | ||
|
||
if pandas_dtype and is_extension_type(arr): | ||
return arr.dtype, arr | ||
|
||
elif isinstance(arr, ABCSeries): | ||
return arr.dtype, np.asarray(arr) | ||
|
||
# don't force numpy coerce with nan's | ||
inferred = lib.infer_dtype(arr) | ||
if inferred in ['string', 'bytes', 'unicode', | ||
|
@@ -553,7 +583,7 @@ def conv(r, dtype): | |
if isnull(r): | ||
pass | ||
elif dtype == _NS_DTYPE: | ||
r = lib.Timestamp(r) | ||
r = tslib.Timestamp(r) | ||
elif dtype == _TD_DTYPE: | ||
r = _coerce_scalar_to_timedelta_type(r) | ||
elif dtype == np.bool_: | ||
|
@@ -1027,3 +1057,31 @@ def find_common_type(types): | |
return np.object | ||
|
||
return np.find_common_type(types, []) | ||
|
||
|
||
def cast_scalar_to_array(shape, value, dtype=None): | ||
""" | ||
create np.ndarray of specified shape and dtype, filled with values | ||
|
||
Parameters | ||
---------- | ||
shape : tuple | ||
value : scalar value | ||
dtype : np.dtype, optional | ||
dtype to coerce | ||
|
||
Returns | ||
------- | ||
ndarray of shape, filled with value, of specified / inferred dtype | ||
|
||
""" | ||
|
||
if dtype is None: | ||
dtype, fill_value = infer_dtype_from_scalar(value) | ||
else: | ||
fill_value = value | ||
|
||
values = np.empty(shape, dtype=dtype) | ||
values.fill(fill_value) | ||
|
||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Repeated this sentance
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.
what do you mean?