Skip to content

Standardize cast_str behavior in all datetimelike fill_value validators #36746

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 4 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,7 @@ def _validate_shift_value(self, fill_value):

return self._unbox(fill_value)

def _validate_scalar(
self, value, msg: Optional[str] = None, cast_str: bool = False
):
def _validate_scalar(self, value, msg: Optional[str] = None):
"""
Validate that the input value can be cast to our scalar_type.

Expand All @@ -769,14 +767,12 @@ def _validate_scalar(
Message to raise in TypeError on invalid input.
If not provided, `value` is cast to a str and used
as the message.
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):
if isinstance(value, str):
# NB: Careful about tzawareness
try:
value = self._scalar_from_string(value)
Expand All @@ -798,9 +794,7 @@ def _validate_scalar(

return value

def _validate_listlike(
self, value, opname: str, cast_str: bool = False, allow_object: bool = False
):
def _validate_listlike(self, value, opname: str, allow_object: bool = False):
if isinstance(value, type(self)):
return value

Expand All @@ -809,7 +803,7 @@ def _validate_listlike(
value = array(value)
value = extract_array(value, extract_numpy=True)

if cast_str and is_dtype_equal(value.dtype, "string"):
if is_dtype_equal(value.dtype, "string"):
# We got a StringArray
try:
# TODO: Could use from_sequence_of_strings if implemented
Expand Down Expand Up @@ -839,9 +833,9 @@ def _validate_listlike(
def _validate_searchsorted_value(self, value):
msg = "searchsorted requires compatible dtype or scalar"
if not is_list_like(value):
value = self._validate_scalar(value, msg, cast_str=True)
value = self._validate_scalar(value, msg)
else:
value = self._validate_listlike(value, "searchsorted", cast_str=True)
value = self._validate_listlike(value, "searchsorted")

rv = self._unbox(value)
return self._rebox_native(rv)
Expand All @@ -852,15 +846,15 @@ def _validate_setitem_value(self, value):
f"or array of those. Got '{type(value).__name__}' instead."
)
if is_list_like(value):
value = self._validate_listlike(value, "setitem", cast_str=True)
value = self._validate_listlike(value, "setitem")
else:
value = self._validate_scalar(value, msg, cast_str=True)
value = self._validate_scalar(value, msg)

return self._unbox(value, setitem=True)

def _validate_insert_value(self, value):
msg = f"cannot insert {type(self).__name__} with incompatible label"
value = self._validate_scalar(value, msg, cast_str=False)
value = self._validate_scalar(value, msg)

self._check_compatible_with(value, setitem=True)
# TODO: if we dont have compat, should we raise or astype(object)?
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def _wrap_joined_index(self, joined: np.ndarray, other):
def _convert_arr_indexer(self, keyarr):
try:
return self._data._validate_listlike(
keyarr, "convert_arr_indexer", cast_str=True, allow_object=True
keyarr, "convert_arr_indexer", allow_object=True
)
except (ValueError, TypeError):
return com.asarray_tuplesafe(keyarr)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def get_loc(self, key, method=None, tolerance=None):
raise InvalidIndexError(key)

try:
key = self._data._validate_scalar(key, cast_str=True)
key = self._data._validate_scalar(key)
except TypeError as err:
raise KeyError(key) from err

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ def test_take_fill(self):
result = arr.take([-1, 1], allow_fill=True, fill_value=pd.NaT)
assert result[0] is pd.NaT

def test_take_fill_str(self, arr1d):
# Cast str fill_value matching other fill_value-taking methods
result = arr1d.take([-1, 1], allow_fill=True, fill_value=str(arr1d[-1]))
expected = arr1d[[-1, 1]]
tm.assert_equal(result, expected)

msg = r"'fill_value' should be a <.*>\. Got 'foo'"
with pytest.raises(ValueError, match=msg):
arr1d.take([-1, 1], allow_fill=True, fill_value="foo")

def test_concat_same_type(self):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9

Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,24 @@ def test_not_equals_numeric(self):
assert not index.equals(pd.Index(index.asi8))
assert not index.equals(pd.Index(index.asi8.astype("u8")))
assert not index.equals(pd.Index(index.asi8).astype("f8"))

def test_where_cast_str(self):
index = self.create_index()

mask = np.ones(len(index), dtype=bool)
mask[-1] = False

result = index.where(mask, str(index[0]))
expected = index.where(mask, index[0])
tm.assert_index_equal(result, expected)

result = index.where(mask, [str(index[0])])
tm.assert_index_equal(result, expected)

msg = "Where requires matching dtype, not foo"
with pytest.raises(TypeError, match=msg):
index.where(mask, "foo")

msg = r"Where requires matching dtype, not \['foo'\]"
with pytest.raises(TypeError, match=msg):
index.where(mask, ["foo"])