-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
TST: restore type checks to maybe_promote tests #28561
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 |
---|---|---|
|
@@ -358,6 +358,7 @@ def maybe_promote(dtype, fill_value=np.nan): | |
fill_value = NaT | ||
elif is_extension_array_dtype(dtype) and isna(fill_value): | ||
fill_value = dtype.na_value | ||
|
||
elif is_float(fill_value): | ||
if issubclass(dtype.type, np.bool_): | ||
dtype = np.object_ | ||
|
@@ -366,6 +367,8 @@ def maybe_promote(dtype, fill_value=np.nan): | |
elif is_bool(fill_value): | ||
if not issubclass(dtype.type, np.bool_): | ||
dtype = np.object_ | ||
else: | ||
fill_value = np.bool_(fill_value) | ||
elif is_integer(fill_value): | ||
if issubclass(dtype.type, np.bool_): | ||
dtype = np.object_ | ||
|
@@ -374,6 +377,10 @@ def maybe_promote(dtype, fill_value=np.nan): | |
arr = np.asarray(fill_value) | ||
if arr != arr.astype(dtype): | ||
dtype = arr.dtype | ||
elif issubclass(dtype.type, np.floating): | ||
# check if we can cast | ||
if _check_lossless_cast(fill_value, dtype): | ||
fill_value = dtype.type(fill_value) | ||
elif is_complex(fill_value): | ||
if issubclass(dtype.type, np.bool_): | ||
dtype = np.object_ | ||
|
@@ -404,6 +411,25 @@ def maybe_promote(dtype, fill_value=np.nan): | |
return dtype, fill_value | ||
|
||
|
||
def _check_lossless_cast(value, dtype: np.dtype) -> bool: | ||
""" | ||
Check if we can cast the given value to the given dtype _losslessly_. | ||
|
||
Parameters | ||
---------- | ||
value : object | ||
dtype : np.dtype | ||
|
||
Returns | ||
------- | ||
bool | ||
""" | ||
casted = dtype.type(value) | ||
if casted == value: | ||
return True | ||
return False | ||
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 not just 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. Either way would work. I think in future steps this function is going to have to check a couple more things. |
||
|
||
|
||
def infer_dtype_from(val, pandas_dtype=False): | ||
""" | ||
interpret the dtype from a scalar or array. This is a convenience | ||
|
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.
you added some code here but are these actually hit?
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.
i see you have another PR on top; can you just do them in one
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. All the changes to the non-test code are needed to make the changes in the test code pass.
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.
Sure. Just close this and move conversation there?
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.
yeah let's just do it in one