|
10 | 10 | cast,
|
11 | 11 | final,
|
12 | 12 | )
|
| 13 | +import warnings |
13 | 14 |
|
14 | 15 | import numpy as np
|
15 | 16 |
|
|
41 | 42 | )
|
42 | 43 | from pandas.errors import AbstractMethodError
|
43 | 44 | from pandas.util._decorators import cache_readonly
|
| 45 | +from pandas.util._exceptions import find_stack_level |
44 | 46 | from pandas.util._validators import validate_bool_kwarg
|
45 | 47 |
|
46 | 48 | from pandas.core.dtypes.astype import (
|
@@ -1895,12 +1897,32 @@ def pad_or_backfill(
|
1895 | 1897 | using_cow: bool = False,
|
1896 | 1898 | ) -> list[Block]:
|
1897 | 1899 | values = self.values
|
| 1900 | + copy, refs = self._get_refs_and_copy(using_cow, inplace) |
| 1901 | + |
1898 | 1902 | if values.ndim == 2 and axis == 1:
|
1899 | 1903 | # NDArrayBackedExtensionArray.fillna assumes axis=0
|
1900 |
| - new_values = values.T.fillna(method=method, limit=limit).T |
| 1904 | + new_values = values.T.fillna(method=method, limit=limit, copy=copy).T |
1901 | 1905 | else:
|
1902 |
| - new_values = values.fillna(method=method, limit=limit) |
1903 |
| - return [self.make_block_same_class(new_values)] |
| 1906 | + try: |
| 1907 | + new_values = values.fillna(method=method, limit=limit, copy=copy) |
| 1908 | + except TypeError: |
| 1909 | + # 3rd party EA that has not implemented copy keyword yet |
| 1910 | + refs = None |
| 1911 | + new_values = values.fillna(method=method, limit=limit) |
| 1912 | + # issue the warning *after* retrying, in case the TypeError |
| 1913 | + # was caused by an invalid fill_value |
| 1914 | + warnings.warn( |
| 1915 | + # GH#53278 |
| 1916 | + "ExtensionArray.fillna added a 'copy' keyword in pandas " |
| 1917 | + "2.1.0. In a future version, ExtensionArray subclasses will " |
| 1918 | + "need to implement this keyword or an exception will be " |
| 1919 | + "raised. In the interim, the keyword is ignored by " |
| 1920 | + f"{type(self.values).__name__}.", |
| 1921 | + FutureWarning, |
| 1922 | + stacklevel=find_stack_level(), |
| 1923 | + ) |
| 1924 | + |
| 1925 | + return [self.make_block_same_class(new_values, refs=refs)] |
1904 | 1926 |
|
1905 | 1927 |
|
1906 | 1928 | class ExtensionBlock(libinternals.Block, EABackedBlock):
|
@@ -1938,8 +1960,29 @@ def fillna(
|
1938 | 1960 | refs = self.refs
|
1939 | 1961 | new_values = self.values
|
1940 | 1962 | else:
|
1941 |
| - refs = None |
1942 |
| - new_values = self.values.fillna(value=value, method=None, limit=limit) |
| 1963 | + copy, refs = self._get_refs_and_copy(using_cow, inplace) |
| 1964 | + |
| 1965 | + try: |
| 1966 | + new_values = self.values.fillna( |
| 1967 | + value=value, method=None, limit=limit, copy=copy |
| 1968 | + ) |
| 1969 | + except TypeError: |
| 1970 | + # 3rd party EA that has not implemented copy keyword yet |
| 1971 | + refs = None |
| 1972 | + new_values = self.values.fillna(value=value, method=None, limit=limit) |
| 1973 | + # issue the warning *after* retrying, in case the TypeError |
| 1974 | + # was caused by an invalid fill_value |
| 1975 | + warnings.warn( |
| 1976 | + # GH#53278 |
| 1977 | + "ExtensionArray.fillna added a 'copy' keyword in pandas " |
| 1978 | + "2.1.0. In a future version, ExtensionArray subclasses will " |
| 1979 | + "need to implement this keyword or an exception will be " |
| 1980 | + "raised. In the interim, the keyword is ignored by " |
| 1981 | + f"{type(self.values).__name__}.", |
| 1982 | + FutureWarning, |
| 1983 | + stacklevel=find_stack_level(), |
| 1984 | + ) |
| 1985 | + |
1943 | 1986 | nb = self.make_block_same_class(new_values, refs=refs)
|
1944 | 1987 | return nb._maybe_downcast([nb], downcast, using_cow=using_cow)
|
1945 | 1988 |
|
|
0 commit comments