Skip to content

REF: share IntervalArray._validate_foo #36483

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 2 commits into from
Sep 19, 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
62 changes: 31 additions & 31 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
is_datetime64_any_dtype,
is_float_dtype,
is_integer_dtype,
is_interval,
is_interval_dtype,
is_list_like,
is_object_dtype,
Expand Down Expand Up @@ -813,7 +812,9 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):

fill_left = fill_right = fill_value
if allow_fill:
fill_left, fill_right = self._validate_fill_value(fill_value)
if (np.asarray(indices) == -1).any():
# We have excel tests that pass fill_value=True, xref GH#36466
fill_left, fill_right = self._validate_fill_value(fill_value)

left_take = take(
self.left, indices, allow_fill=allow_fill, fill_value=fill_left
Expand All @@ -824,20 +825,33 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):

return self._shallow_copy(left_take, right_take)

def _validate_fill_value(self, value):
if is_interval(value):
self._check_closed_matches(value, name="fill_value")
fill_left, fill_right = value.left, value.right
elif not is_scalar(value) and notna(value):
msg = (
"'IntervalArray.fillna' only supports filling with a "
"'scalar pandas.Interval or NA'. "
f"Got a '{type(value).__name__}' instead."
)
raise ValueError(msg)
def _validate_listlike(self, value):
# list-like of intervals
try:
array = IntervalArray(value)
# TODO: self._check_closed_matches(array, name="value")
value_left, value_right = array.left, array.right
except TypeError as err:
# wrong type: not interval or NA
msg = f"'value' should be an interval type, got {type(value)} instead."
raise TypeError(msg) from err
return value_left, value_right

def _validate_scalar(self, value):
if isinstance(value, Interval):
self._check_closed_matches(value, name="value")
left, right = value.left, value.right
elif is_valid_nat_for_dtype(value, self.left.dtype):
# GH#18295
left = right = value
else:
fill_left = fill_right = self.left._na_value
return fill_left, fill_right
raise ValueError(
"can only insert Interval objects and NA into an IntervalArray"
)
return left, right

def _validate_fill_value(self, value):
return self._validate_scalar(value)

def _validate_fillna_value(self, value):
if not isinstance(value, Interval):
Expand All @@ -851,26 +865,12 @@ def _validate_fillna_value(self, value):
return value.left, value.right

def _validate_insert_value(self, value):
if isinstance(value, Interval):
if value.closed != self.closed:
raise ValueError(
"inserted item must be closed on the same side as the index"
)
left_insert = value.left
right_insert = value.right
elif is_valid_nat_for_dtype(value, self.left.dtype):
# GH#18295
left_insert = right_insert = value
else:
raise ValueError(
"can only insert Interval objects and NA into an IntervalIndex"
)
return left_insert, right_insert
return self._validate_scalar(value)

def _validate_setitem_value(self, value):
needs_float_conversion = False

if is_scalar(value) and isna(value):
if is_valid_nat_for_dtype(value, self.left.dtype):
# na value: need special casing to set directly on numpy arrays
if is_integer_dtype(self.dtype.subtype):
# can't set NaN on a numpy integer array
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def test_set_na(self, left_right_dtypes):
left, right = left_right_dtypes
result = IntervalArray.from_arrays(left, right)

if result.dtype.subtype.kind not in ["m", "M"]:
msg = "'value' should be an interval type, got <.*NaTType'> instead."
with pytest.raises(TypeError, match=msg):
result[0] = pd.NaT
if result.dtype.subtype.kind in ["i", "u"]:
msg = "Cannot set float NaN to integer-backed IntervalArray"
with pytest.raises(ValueError, match=msg):
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ def test_insert(self, data):
tm.assert_index_equal(result, expected)

# invalid type
msg = "can only insert Interval objects and NA into an IntervalIndex"
msg = "can only insert Interval objects and NA into an IntervalArray"
with pytest.raises(ValueError, match=msg):
data.insert(1, "foo")

# invalid closed
msg = "inserted item must be closed on the same side as the index"
msg = "'value.closed' is 'left', expected 'right'."
for closed in {"left", "right", "both", "neither"} - {item.closed}:
msg = f"'value.closed' is '{closed}', expected '{item.closed}'."
with pytest.raises(ValueError, match=msg):
bad_item = Interval(item.left, item.right, closed=closed)
data.insert(1, bad_item)
Expand All @@ -211,7 +212,7 @@ def test_insert(self, data):

if data.left.dtype.kind not in ["m", "M"]:
# trying to insert pd.NaT into a numeric-dtyped Index should cast/raise
msg = "can only insert Interval objects and NA into an IntervalIndex"
msg = "can only insert Interval objects and NA into an IntervalArray"
with pytest.raises(ValueError, match=msg):
result = data.insert(1, pd.NaT)
else:
Expand Down