|
21 | 21 | from pandas.core.dtypes.cast import (
|
22 | 22 | astype_dt64_to_dt64tz,
|
23 | 23 | astype_nansafe,
|
| 24 | + can_hold_element, |
24 | 25 | convert_scalar_for_putitemlike,
|
25 | 26 | find_common_type,
|
26 | 27 | infer_dtype_from,
|
|
40 | 41 | is_datetime64tz_dtype,
|
41 | 42 | is_dtype_equal,
|
42 | 43 | is_extension_array_dtype,
|
43 |
| - is_float, |
44 | 44 | is_integer,
|
45 | 45 | is_list_like,
|
46 | 46 | is_object_dtype,
|
@@ -1906,24 +1906,14 @@ class NumericBlock(Block):
|
1906 | 1906 | is_numeric = True
|
1907 | 1907 | _can_hold_na = True
|
1908 | 1908 |
|
| 1909 | + def _can_hold_element(self, element: Any) -> bool: |
| 1910 | + return can_hold_element(self.dtype, element) |
| 1911 | + |
1909 | 1912 |
|
1910 | 1913 | class FloatBlock(NumericBlock):
|
1911 | 1914 | __slots__ = ()
|
1912 | 1915 | is_float = True
|
1913 | 1916 |
|
1914 |
| - def _can_hold_element(self, element: Any) -> bool: |
1915 |
| - tipo = maybe_infer_dtype_type(element) |
1916 |
| - if tipo is not None: |
1917 |
| - return issubclass(tipo.type, (np.floating, np.integer)) and not issubclass( |
1918 |
| - tipo.type, np.timedelta64 |
1919 |
| - ) |
1920 |
| - return isinstance( |
1921 |
| - element, (float, int, np.floating, np.int_) |
1922 |
| - ) and not isinstance( |
1923 |
| - element, |
1924 |
| - (bool, np.bool_, np.timedelta64), |
1925 |
| - ) |
1926 |
| - |
1927 | 1917 | def to_native_types(
|
1928 | 1918 | self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
|
1929 | 1919 | ):
|
@@ -1962,32 +1952,12 @@ class ComplexBlock(NumericBlock):
|
1962 | 1952 | __slots__ = ()
|
1963 | 1953 | is_complex = True
|
1964 | 1954 |
|
1965 |
| - def _can_hold_element(self, element: Any) -> bool: |
1966 |
| - tipo = maybe_infer_dtype_type(element) |
1967 |
| - if tipo is not None: |
1968 |
| - return tipo.kind in ["c", "f", "i", "u"] |
1969 |
| - return ( |
1970 |
| - lib.is_integer(element) or lib.is_complex(element) or lib.is_float(element) |
1971 |
| - ) |
1972 |
| - |
1973 | 1955 |
|
1974 | 1956 | class IntBlock(NumericBlock):
|
1975 | 1957 | __slots__ = ()
|
1976 | 1958 | is_integer = True
|
1977 | 1959 | _can_hold_na = False
|
1978 | 1960 |
|
1979 |
| - def _can_hold_element(self, element: Any) -> bool: |
1980 |
| - tipo = maybe_infer_dtype_type(element) |
1981 |
| - if tipo is not None: |
1982 |
| - return ( |
1983 |
| - issubclass(tipo.type, np.integer) |
1984 |
| - and not issubclass(tipo.type, np.timedelta64) |
1985 |
| - and self.dtype.itemsize >= tipo.itemsize |
1986 |
| - ) |
1987 |
| - # We have not inferred an integer from the dtype |
1988 |
| - # check if we have a builtin int or a float equal to an int |
1989 |
| - return is_integer(element) or (is_float(element) and element.is_integer()) |
1990 |
| - |
1991 | 1961 |
|
1992 | 1962 | class DatetimeLikeBlockMixin(Block):
|
1993 | 1963 | """Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
|
@@ -2284,12 +2254,6 @@ class BoolBlock(NumericBlock):
|
2284 | 2254 | is_bool = True
|
2285 | 2255 | _can_hold_na = False
|
2286 | 2256 |
|
2287 |
| - def _can_hold_element(self, element: Any) -> bool: |
2288 |
| - tipo = maybe_infer_dtype_type(element) |
2289 |
| - if tipo is not None: |
2290 |
| - return issubclass(tipo.type, np.bool_) |
2291 |
| - return isinstance(element, (bool, np.bool_)) |
2292 |
| - |
2293 | 2257 |
|
2294 | 2258 | class ObjectBlock(Block):
|
2295 | 2259 | __slots__ = ()
|
|
0 commit comments