From c77bfe531ae1bb0d01e81c38f73cfabc0d48c34c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 6 Jan 2020 11:07:33 -0800 Subject: [PATCH 1/2] REF: share _validate_fill_value --- pandas/core/arrays/datetimelike.py | 12 +++++++++++- pandas/core/arrays/datetimes.py | 15 --------------- pandas/core/arrays/period.py | 13 +------------ pandas/core/arrays/timedeltas.py | 11 ----------- 4 files changed, 12 insertions(+), 39 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index b9a6daf7c630a..9e5900469b731 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -591,7 +591,17 @@ def _validate_fill_value(self, fill_value): ------ ValueError """ - raise AbstractMethodError(self) + if isna(fill_value): + fill_value = iNaT + elif isinstance(fill_value, self._recognized_scalars): + self._check_compatible_with(fill_value) + fill_value = self._scalar_type(fill_value) + fill_value = self._unbox_scalar(fill_value) + else: + raise ValueError( + f"'fill_value' should be a {self._scalar_type}. " "Got '{fill_value}'." + ) + return fill_value def take(self, indices, allow_fill=False, fill_value=None): if allow_fill: diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 06d280a3dc25b..267780219c74e 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -20,7 +20,6 @@ ) import pandas.compat as compat from pandas.errors import PerformanceWarning -from pandas.util._decorators import Appender from pandas.core.dtypes.common import ( _INT64_DTYPE, @@ -700,20 +699,6 @@ def astype(self, dtype, copy=True): return self.to_period(freq=dtype.freq) return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy) - # ---------------------------------------------------------------- - # ExtensionArray Interface - - @Appender(dtl.DatetimeLikeArrayMixin._validate_fill_value.__doc__) - def _validate_fill_value(self, fill_value): - if isna(fill_value): - fill_value = iNaT - elif isinstance(fill_value, (datetime, np.datetime64)): - self._assert_tzawareness_compat(fill_value) - fill_value = Timestamp(fill_value).value - else: - raise ValueError(f"'fill_value' should be a Timestamp. Got '{fill_value}'.") - return fill_value - # ----------------------------------------------------------------- # Rendering Methods diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index e7e1c84b1c070..2b92d6f1cdbe3 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -21,7 +21,7 @@ ) from pandas._libs.tslibs.timedeltas import Timedelta, delta_to_nanoseconds import pandas.compat as compat -from pandas.util._decorators import Appender, cache_readonly +from pandas.util._decorators import cache_readonly from pandas.core.dtypes.common import ( _TD_DTYPE, @@ -505,17 +505,6 @@ def to_timestamp(self, freq=None, how="start"): # -------------------------------------------------------------------- # Array-like / EA-Interface Methods - @Appender(dtl.DatetimeLikeArrayMixin._validate_fill_value.__doc__) - def _validate_fill_value(self, fill_value): - if isna(fill_value): - fill_value = iNaT - elif isinstance(fill_value, Period): - self._check_compatible_with(fill_value) - fill_value = fill_value.ordinal - else: - raise ValueError(f"'fill_value' should be a Period. Got '{fill_value}'.") - return fill_value - def _values_for_argsort(self): return self._data diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index ddf3af538874e..616f7b63ab25c 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -13,7 +13,6 @@ ) import pandas.compat as compat from pandas.compat.numpy import function as nv -from pandas.util._decorators import Appender from pandas.core.dtypes.common import ( _NS_DTYPE, @@ -367,16 +366,6 @@ def _maybe_clear_freq(self): # ---------------------------------------------------------------- # Array-Like / EA-Interface Methods - @Appender(dtl.DatetimeLikeArrayMixin._validate_fill_value.__doc__) - def _validate_fill_value(self, fill_value): - if isna(fill_value): - fill_value = iNaT - elif isinstance(fill_value, (timedelta, np.timedelta64, Tick)): - fill_value = Timedelta(fill_value).value - else: - raise ValueError(f"'fill_value' should be a Timedelta. Got '{fill_value}'.") - return fill_value - def astype(self, dtype, copy=True): # We handle # --> timedelta64[ns] From 5afe957a4b3786a1aeb2ba2abc790fa6d8e4fc68 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 6 Jan 2020 11:08:56 -0800 Subject: [PATCH 2/2] post-black fixup --- pandas/core/arrays/datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 9e5900469b731..b0985332092ae 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -599,7 +599,7 @@ def _validate_fill_value(self, fill_value): fill_value = self._unbox_scalar(fill_value) else: raise ValueError( - f"'fill_value' should be a {self._scalar_type}. " "Got '{fill_value}'." + f"'fill_value' should be a {self._scalar_type}. Got '{fill_value}'." ) return fill_value