Skip to content

REF: share _validate_fill_value #30752

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
Jan 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
12 changes: 11 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 0 additions & 15 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
13 changes: 1 addition & 12 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
11 changes: 0 additions & 11 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down