MCPcopy
hub / github.com/pandas-dev/pandas / to_numpy

Method to_numpy

pandas/core/arrays/arrow/array.py:1701–1760  ·  view source on GitHub ↗
(
        self,
        dtype: npt.DTypeLike | None = None,
        copy: bool = False,
        na_value: object = lib.no_default,
    )

Source from the content-addressed store, hash-verified

1699
1700 @doc(ExtensionArray.to_numpy)
1701 def to_numpy(
1702 self,
1703 dtype: npt.DTypeLike | None = None,
1704 copy: bool = False,
1705 na_value: object = lib.no_default,
1706 ) -> np.ndarray:
1707 original_na_value = na_value
1708 dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, self._hasna)
1709 pa_type = self._pa_array.type
1710 if not self._hasna or isna(na_value) or pa.types.is_null(pa_type):
1711 data = self
1712 else:
1713 data = self.fillna(na_value)
1714 copy = False
1715
1716 if pa.types.is_timestamp(pa_type) or pa.types.is_duration(pa_type):
1717 # GH 55997
1718 if dtype != object and na_value is self.dtype.na_value:
1719 na_value = lib.no_default
1720 result = data._maybe_convert_datelike_array().to_numpy(
1721 dtype=dtype, na_value=na_value
1722 )
1723 elif pa.types.is_time(pa_type) or pa.types.is_date(pa_type):
1724 # convert to list of python datetime.time objects before
1725 # wrapping in ndarray
1726 result = np.array(list(data), dtype=dtype)
1727 if data._hasna:
1728 result[data.isna()] = na_value
1729 elif pa.types.is_null(pa_type):
1730 if dtype is not None and isna(na_value):
1731 na_value = None
1732 result = np.full(len(data), fill_value=na_value, dtype=dtype)
1733 elif not data._hasna or (
1734 pa.types.is_floating(pa_type)
1735 and (
1736 na_value is np.nan
1737 or (
1738 original_na_value is lib.no_default
1739 and is_float_dtype(dtype)
1740 and is_nan_na()
1741 )
1742 )
1743 ):
1744 result = data._pa_array.to_numpy()
1745 if dtype is not None:
1746 result = result.astype(dtype, copy=False)
1747 if copy:
1748 result = result.copy()
1749 else:
1750 if dtype is None:
1751 empty = pa.array([], type=pa_type).to_numpy(zero_copy_only=False)
1752 if can_hold_element(empty, na_value):
1753 dtype = empty.dtype
1754 else:
1755 dtype = np.object_
1756 result = np.empty(len(data), dtype=dtype)
1757 mask = data.isna()
1758 result[mask] = na_value

Callers 15

__array__Method · 0.95
searchsortedMethod · 0.95
mapMethod · 0.95
duplicatedMethod · 0.95
interpolateMethod · 0.95
_to_maskedMethod · 0.95
_infer_typesMethod · 0.95
test_to_numpy_temporalFunction · 0.95
_box_pa_arrayMethod · 0.45
_arith_methodMethod · 0.45
isnaMethod · 0.45
argsortMethod · 0.45

Calls 12

fillnaMethod · 0.95
to_numpy_dtype_inferenceFunction · 0.90
isnaFunction · 0.90
is_float_dtypeFunction · 0.90
is_nan_naFunction · 0.90
can_hold_elementFunction · 0.90
arrayMethod · 0.45
isnaMethod · 0.45
astypeMethod · 0.45
copyMethod · 0.45
emptyMethod · 0.45

Tested by 1

test_to_numpy_temporalFunction · 0.76