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

Method as_array

pandas/core/internals/managers.py:1822–1896  ·  view source on GitHub ↗

Convert the blockmanager data into a numpy array. Parameters ---------- dtype : np.dtype or None, default None Data type of the return array. copy : bool, default False If True then guarantee that a copy is returned. A value of

(
        self,
        dtype: np.dtype | None = None,
        copy: bool = False,
        na_value: object = lib.no_default,
    )

Source from the content-addressed store, hash-verified

1820 yield dtype, self._combine(list(blocks))
1821
1822 def as_array(
1823 self,
1824 dtype: np.dtype | None = None,
1825 copy: bool = False,
1826 na_value: object = lib.no_default,
1827 ) -> np.ndarray:
1828 """
1829 Convert the blockmanager data into a numpy array.
1830
1831 Parameters
1832 ----------
1833 dtype : np.dtype or None, default None
1834 Data type of the return array.
1835 copy : bool, default False
1836 If True then guarantee that a copy is returned. A value of
1837 False does not guarantee that the underlying data is not
1838 copied.
1839 na_value : object, default lib.no_default
1840 Value to be used as the missing value sentinel.
1841
1842 Returns
1843 -------
1844 arr : ndarray
1845 """
1846 passed_nan = lib.is_float(na_value) and isna(na_value)
1847
1848 if len(self.blocks) == 0:
1849 arr = np.empty(self.shape, dtype=float)
1850 return arr.transpose()
1851
1852 if self.is_single_block:
1853 blk = self.blocks[0]
1854
1855 if na_value is not lib.no_default:
1856 # We want to copy when na_value is provided to avoid
1857 # mutating the original object
1858 if lib.is_np_dtype(blk.dtype, "f") and passed_nan:
1859 # We are already numpy-float and na_value=np.nan
1860 pass
1861 else:
1862 copy = True
1863
1864 if blk.is_extension:
1865 # Avoid implicit conversion of extension blocks to object
1866
1867 # error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no
1868 # attribute "to_numpy"
1869 arr = blk.values.to_numpy( # type: ignore[union-attr]
1870 dtype=dtype,
1871 na_value=na_value,
1872 copy=copy,
1873 ).reshape(blk.shape)
1874 elif not copy:
1875 arr = np.asarray(blk.values, dtype=dtype)
1876 else:
1877 arr = np.array(blk.values, dtype=dtype, copy=copy)
1878 if passed_nan and blk.dtype.kind in "mM":
1879 arr[isna(blk.values)] = na_value

Callers 1

buffer_to_ndarrayFunction · 0.45

Calls 8

_interleaveMethod · 0.95
isnaFunction · 0.90
emptyMethod · 0.45
transposeMethod · 0.45
reshapeMethod · 0.45
to_numpyMethod · 0.45
arrayMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected