|
16 | 16 | from pandas.errors import (
|
17 | 17 | AbstractMethodError, NullFrequencyError, PerformanceWarning)
|
18 | 18 | from pandas.util._decorators import Appender, Substitution
|
| 19 | +from pandas.util._validators import validate_fillna_kwargs |
19 | 20 |
|
20 | 21 | from pandas.core.dtypes.common import (
|
21 | 22 | is_bool_dtype, is_categorical_dtype, is_datetime64_any_dtype,
|
|
25 | 26 | is_string_dtype, is_timedelta64_dtype, is_unsigned_integer_dtype,
|
26 | 27 | needs_i8_conversion, pandas_dtype)
|
27 | 28 | from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
|
| 29 | +from pandas.core.dtypes.inference import is_array_like |
28 | 30 | from pandas.core.dtypes.missing import isna
|
29 | 31 |
|
30 |
| -from pandas.core import nanops |
| 32 | +from pandas.core import missing, nanops |
31 | 33 | from pandas.core.algorithms import (
|
32 | 34 | checked_add_with_arr, take, unique1d, value_counts)
|
33 | 35 | import pandas.core.common as com
|
@@ -787,6 +789,52 @@ def _maybe_mask_results(self, result, fill_value=iNaT, convert=None):
|
787 | 789 | result[self._isnan] = fill_value
|
788 | 790 | return result
|
789 | 791 |
|
| 792 | + def fillna(self, value=None, method=None, limit=None): |
| 793 | + # TODO(GH-20300): remove this |
| 794 | + # Just overriding to ensure that we avoid an astype(object). |
| 795 | + # Either 20300 or a `_values_for_fillna` would avoid this duplication. |
| 796 | + if isinstance(value, ABCSeries): |
| 797 | + value = value.array |
| 798 | + |
| 799 | + value, method = validate_fillna_kwargs(value, method) |
| 800 | + |
| 801 | + mask = self.isna() |
| 802 | + |
| 803 | + if is_array_like(value): |
| 804 | + if len(value) != len(self): |
| 805 | + raise ValueError("Length of 'value' does not match. Got ({}) " |
| 806 | + " expected {}".format(len(value), len(self))) |
| 807 | + value = value[mask] |
| 808 | + |
| 809 | + if mask.any(): |
| 810 | + if method is not None: |
| 811 | + if method == 'pad': |
| 812 | + func = missing.pad_1d |
| 813 | + else: |
| 814 | + func = missing.backfill_1d |
| 815 | + |
| 816 | + values = self._data |
| 817 | + if not is_period_dtype(self): |
| 818 | + # For PeriodArray self._data is i8, which gets copied |
| 819 | + # by `func`. Otherwise we need to make a copy manually |
| 820 | + # to avoid modifying `self` in-place. |
| 821 | + values = values.copy() |
| 822 | + |
| 823 | + new_values = func(values, limit=limit, |
| 824 | + mask=mask) |
| 825 | + if is_datetime64tz_dtype(self): |
| 826 | + # we need to pass int64 values to the constructor to avoid |
| 827 | + # re-localizing incorrectly |
| 828 | + new_values = new_values.view("i8") |
| 829 | + new_values = type(self)(new_values, dtype=self.dtype) |
| 830 | + else: |
| 831 | + # fill with value |
| 832 | + new_values = self.copy() |
| 833 | + new_values[mask] = value |
| 834 | + else: |
| 835 | + new_values = self.copy() |
| 836 | + return new_values |
| 837 | + |
790 | 838 | # ------------------------------------------------------------------
|
791 | 839 | # Frequency Properties/Methods
|
792 | 840 |
|
|
0 commit comments