-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
REF: share code for scalar validation in datetimelike array methods #34076
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
jreback
merged 8 commits into
pandas-dev:master
from
jbrockmendel:dtlike-validators-scalar
May 12, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bcbcc9a
REF: implement _validate_scalar
jbrockmendel 355f956
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel 44ae767
standardize exception messages
jbrockmendel 825ec16
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel 73c05ee
restore exception messages
jbrockmendel f648488
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel f425972
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel c99b7a7
docstring
jbrockmendel 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from datetime import datetime, timedelta | ||
import operator | ||
from typing import Any, Callable, Sequence, Type, TypeVar, Union, cast | ||
from typing import Any, Callable, Sequence, Tuple, Type, TypeVar, Union, cast | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -437,6 +437,7 @@ class DatetimeLikeArrayMixin( | |
""" | ||
|
||
_is_recognized_dtype: Callable[[DtypeObj], bool] | ||
_recognized_scalars: Tuple[Type, ...] | ||
|
||
# ------------------------------------------------------------------ | ||
# NDArrayBackedExtensionArray compat | ||
|
@@ -718,16 +719,14 @@ def _validate_fill_value(self, fill_value): | |
------ | ||
ValueError | ||
""" | ||
if is_valid_nat_for_dtype(fill_value, self.dtype): | ||
fill_value = NaT | ||
elif isinstance(fill_value, self._recognized_scalars): | ||
fill_value = self._scalar_type(fill_value) | ||
else: | ||
raise ValueError( | ||
f"'fill_value' should be a {self._scalar_type}. " | ||
f"Got '{str(fill_value)}'." | ||
) | ||
|
||
msg = ( | ||
f"'fill_value' should be a {self._scalar_type}. " | ||
f"Got '{str(fill_value)}'." | ||
) | ||
try: | ||
fill_value = self._validate_scalar(fill_value, msg) | ||
except TypeError as err: | ||
raise ValueError(msg) from err | ||
return self._unbox(fill_value) | ||
|
||
def _validate_shift_value(self, fill_value): | ||
|
@@ -757,6 +756,41 @@ def _validate_shift_value(self, fill_value): | |
|
||
return self._unbox(fill_value) | ||
|
||
def _validate_scalar(self, value, msg: str, cast_str: bool = False): | ||
""" | ||
Validate that the input value can be cast to our scalar_type. | ||
|
||
Parameters | ||
---------- | ||
value : object | ||
msg : str | ||
Message to raise in TypeError on invalid input. | ||
cast_str : bool, default False | ||
Whether to try to parse string input to scalar_type. | ||
|
||
Returns | ||
------- | ||
self._scalar_type or NaT | ||
""" | ||
if cast_str and isinstance(value, str): | ||
# NB: Careful about tzawareness | ||
try: | ||
value = self._scalar_from_string(value) | ||
except ValueError as err: | ||
raise TypeError(msg) from err | ||
|
||
elif is_valid_nat_for_dtype(value, self.dtype): | ||
# GH#18295 | ||
value = NaT | ||
|
||
elif isinstance(value, self._recognized_scalars): | ||
value = self._scalar_type(value) # type: ignore | ||
|
||
else: | ||
raise TypeError(msg) | ||
|
||
return value | ||
|
||
def _validate_listlike( | ||
self, value, opname: str, cast_str: bool = False, allow_object: bool = False, | ||
): | ||
|
@@ -795,72 +829,42 @@ def _validate_listlike( | |
return value | ||
|
||
def _validate_searchsorted_value(self, value): | ||
if isinstance(value, str): | ||
try: | ||
value = self._scalar_from_string(value) | ||
except ValueError as err: | ||
raise TypeError( | ||
"searchsorted requires compatible dtype or scalar" | ||
) from err | ||
|
||
elif is_valid_nat_for_dtype(value, self.dtype): | ||
value = NaT | ||
|
||
elif isinstance(value, self._recognized_scalars): | ||
value = self._scalar_type(value) | ||
|
||
elif not is_list_like(value): | ||
raise TypeError(f"Unexpected type for 'value': {type(value)}") | ||
|
||
msg = "searchsorted requires compatible dtype or scalar" | ||
if not is_list_like(value): | ||
value = self._validate_scalar(value, msg, cast_str=True) | ||
else: | ||
# TODO: cast_str? we accept it for scalar | ||
value = self._validate_listlike(value, "searchsorted") | ||
|
||
return self._unbox(value) | ||
|
||
def _validate_setitem_value(self, value): | ||
|
||
msg = ( | ||
f"'value' should be a '{self._scalar_type.__name__}', 'NaT', " | ||
f"or array of those. Got '{type(value).__name__}' instead." | ||
) | ||
if is_list_like(value): | ||
value = self._validate_listlike(value, "setitem", cast_str=True) | ||
|
||
elif isinstance(value, self._recognized_scalars): | ||
value = self._scalar_type(value) | ||
elif is_valid_nat_for_dtype(value, self.dtype): | ||
value = NaT | ||
else: | ||
msg = ( | ||
f"'value' should be a '{self._scalar_type.__name__}', 'NaT', " | ||
f"or array of those. Got '{type(value).__name__}' instead." | ||
) | ||
raise TypeError(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. This message for example is a nicer, user-friendly one that would be good to keep. 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. ok @gfyoung ? |
||
# TODO: cast_str for consistency? | ||
value = self._validate_scalar(value, msg, cast_str=False) | ||
|
||
self._check_compatible_with(value, setitem=True) | ||
return self._unbox(value) | ||
|
||
def _validate_insert_value(self, value): | ||
if isinstance(value, self._recognized_scalars): | ||
value = self._scalar_type(value) | ||
elif is_valid_nat_for_dtype(value, self.dtype): | ||
# GH#18295 | ||
value = NaT | ||
else: | ||
raise TypeError( | ||
f"cannot insert {type(self).__name__} with incompatible label" | ||
) | ||
msg = f"cannot insert {type(self).__name__} with incompatible label" | ||
value = self._validate_scalar(value, msg, cast_str=False) | ||
|
||
self._check_compatible_with(value, setitem=True) | ||
# TODO: if we dont have compat, should we raise or astype(object)? | ||
# PeriodIndex does astype(object) | ||
return value | ||
|
||
def _validate_where_value(self, other): | ||
if is_valid_nat_for_dtype(other, self.dtype): | ||
other = NaT | ||
elif isinstance(other, self._recognized_scalars): | ||
other = self._scalar_type(other) | ||
elif not is_list_like(other): | ||
raise TypeError(f"Where requires matching dtype, not {type(other)}") | ||
|
||
msg = f"Where requires matching dtype, not {type(other)}" | ||
if not is_list_like(other): | ||
other = self._validate_scalar(other, msg) | ||
else: | ||
other = self._validate_listlike(other, "where") | ||
self._check_compatible_with(other, setitem=True) | ||
|
This file contains hidden or 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
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.
can you add a doc-string here
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.
updated