Skip to content

Commit 3bbb93c

Browse files
authored
REF: de-duplicate _validate_fill_value/_validate_scalar (#41790)
1 parent 7dc65ae commit 3bbb93c

File tree

5 files changed

+8
-50
lines changed

5 files changed

+8
-50
lines changed

pandas/core/arrays/_mixins.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def take(
9494
axis: int = 0,
9595
) -> NDArrayBackedExtensionArrayT:
9696
if allow_fill:
97-
fill_value = self._validate_fill_value(fill_value)
97+
fill_value = self._validate_scalar(fill_value)
9898

9999
new_data = take(
100100
self._ndarray,
@@ -107,25 +107,6 @@ def take(
107107
)
108108
return self._from_backing_data(new_data)
109109

110-
def _validate_fill_value(self, fill_value):
111-
"""
112-
If a fill_value is passed to `take` convert it to a representation
113-
suitable for self._ndarray, raising TypeError if this is not possible.
114-
115-
Parameters
116-
----------
117-
fill_value : object
118-
119-
Returns
120-
-------
121-
fill_value : native representation
122-
123-
Raises
124-
------
125-
TypeError
126-
"""
127-
raise AbstractMethodError(self)
128-
129110
# ------------------------------------------------------------------------
130111

131112
def equals(self, other) -> bool:
@@ -194,7 +175,7 @@ def shift(self, periods=1, fill_value=None, axis=0):
194175
def _validate_shift_value(self, fill_value):
195176
# TODO: after deprecation in datetimelikearraymixin is enforced,
196177
# we can remove this and ust validate_fill_value directly
197-
return self._validate_fill_value(fill_value)
178+
return self._validate_scalar(fill_value)
198179

199180
def __setitem__(self, key, value):
200181
key = check_array_indexer(self, key)

pandas/core/arrays/categorical.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ def _validate_searchsorted_value(self, value):
14071407
codes = np.array(locs, dtype=self.codes.dtype) # type: ignore[assignment]
14081408
return codes
14091409

1410-
def _validate_fill_value(self, fill_value):
1410+
def _validate_scalar(self, fill_value):
14111411
"""
14121412
Convert a user-facing fill_value to a representation to use with our
14131413
underlying ndarray, raising TypeError if this is not possible.
@@ -1436,8 +1436,6 @@ def _validate_fill_value(self, fill_value):
14361436
)
14371437
return fill_value
14381438

1439-
_validate_scalar = _validate_fill_value
1440-
14411439
# -------------------------------------------------------------
14421440

14431441
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:

pandas/core/arrays/datetimelike.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -557,27 +557,8 @@ def _validate_comparison_value(self, other):
557557

558558
return other
559559

560-
def _validate_fill_value(self, fill_value):
561-
"""
562-
If a fill_value is passed to `take` convert it to an i8 representation,
563-
raising TypeError if this is not possible.
564-
565-
Parameters
566-
----------
567-
fill_value : object
568-
569-
Returns
570-
-------
571-
fill_value : np.int64, np.datetime64, or np.timedelta64
572-
573-
Raises
574-
------
575-
TypeError
576-
"""
577-
return self._validate_scalar(fill_value)
578-
579560
def _validate_shift_value(self, fill_value):
580-
# TODO(2.0): once this deprecation is enforced, use _validate_fill_value
561+
# TODO(2.0): once this deprecation is enforced, use _validate_scalar
581562
if is_valid_na_for_dtype(fill_value, self.dtype):
582563
fill_value = NaT
583564
elif isinstance(fill_value, self._recognized_scalars):

pandas/core/arrays/interval.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ def fillna(
801801
if limit is not None:
802802
raise TypeError("limit is not supported for IntervalArray.")
803803

804-
value_left, value_right = self._validate_fill_value(value)
804+
value_left, value_right = self._validate_scalar(value)
805805

806806
left = self.left.fillna(value=value_left)
807807
right = self.right.fillna(value=value_right)
@@ -1000,7 +1000,7 @@ def take(
10001000

10011001
fill_left = fill_right = fill_value
10021002
if allow_fill:
1003-
fill_left, fill_right = self._validate_fill_value(fill_value)
1003+
fill_left, fill_right = self._validate_scalar(fill_value)
10041004

10051005
left_take = take(
10061006
self._left, indices, allow_fill=allow_fill, fill_value=fill_left
@@ -1037,6 +1037,7 @@ def _validate_scalar(self, value):
10371037
if isinstance(value, Interval):
10381038
self._check_closed_matches(value, name="value")
10391039
left, right = value.left, value.right
1040+
# TODO: check subdtype match like _validate_setitem_value?
10401041
elif is_valid_na_for_dtype(value, self.left.dtype):
10411042
# GH#18295
10421043
left = right = value
@@ -1046,9 +1047,6 @@ def _validate_scalar(self, value):
10461047
)
10471048
return left, right
10481049

1049-
def _validate_fill_value(self, value):
1050-
return self._validate_scalar(value)
1051-
10521050
def _validate_setitem_value(self, value):
10531051
needs_float_conversion = False
10541052

pandas/core/arrays/numpy_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
190190
def isna(self) -> np.ndarray:
191191
return isna(self._ndarray)
192192

193-
def _validate_fill_value(self, fill_value):
193+
def _validate_scalar(self, fill_value):
194194
if fill_value is None:
195195
# Primarily for subclasses
196196
fill_value = self.dtype.na_value

0 commit comments

Comments
 (0)