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

Method _interleave

pandas/core/internals/managers.py:1898–1952  ·  view source on GitHub ↗

Return ndarray from blocks with specified item order Items must be contained in the blocks

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

Source from the content-addressed store, hash-verified

1896 return arr.transpose()
1897
1898 def _interleave(
1899 self,
1900 dtype: np.dtype | None = None,
1901 na_value: object = lib.no_default,
1902 ) -> np.ndarray:
1903 """
1904 Return ndarray from blocks with specified item order
1905 Items must be contained in the blocks
1906 """
1907 if not dtype:
1908 # Incompatible types in assignment (expression has type
1909 # "Optional[Union[dtype[Any], ExtensionDtype]]", variable has
1910 # type "Optional[dtype[Any]]")
1911 dtype = interleaved_dtype( # type: ignore[assignment]
1912 [blk.dtype for blk in self.blocks]
1913 )
1914
1915 # error: Argument 1 to "ensure_np_dtype" has incompatible type
1916 # "Optional[dtype[Any]]"; expected "Union[dtype[Any], ExtensionDtype]"
1917 dtype = ensure_np_dtype(dtype) # type: ignore[arg-type]
1918 result = np.empty(self.shape, dtype=dtype)
1919
1920 itemmask = np.zeros(self.shape[0])
1921
1922 if dtype == np.dtype("object") and na_value is lib.no_default:
1923 # much more performant than using to_numpy below
1924 for blk in self.blocks:
1925 rl = blk.mgr_locs
1926 arr = blk.get_values(dtype)
1927 result[rl.indexer] = arr
1928 itemmask[rl.indexer] = 1
1929 return result
1930
1931 for blk in self.blocks:
1932 rl = blk.mgr_locs
1933 if blk.is_extension:
1934 # Avoid implicit conversion of extension blocks to object
1935
1936 # error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no
1937 # attribute "to_numpy"
1938 arr = blk.values.to_numpy( # type: ignore[union-attr]
1939 dtype=dtype,
1940 na_value=na_value,
1941 )
1942 else:
1943 arr = blk.get_values(dtype)
1944 result[rl.indexer] = arr
1945 if na_value is not lib.no_default and blk.dtype.kind in "mM":
1946 result[rl.indexer][isna(arr)] = na_value
1947 itemmask[rl.indexer] = 1
1948
1949 if not itemmask.all():
1950 raise AssertionError("Some items were not contained in blocks")
1951
1952 return result
1953
1954 # ----------------------------------------------------------------
1955 # Consolidation

Callers 1

as_arrayMethod · 0.95

Calls 8

isnaFunction · 0.90
interleaved_dtypeFunction · 0.85
ensure_np_dtypeFunction · 0.85
emptyMethod · 0.45
dtypeMethod · 0.45
get_valuesMethod · 0.45
to_numpyMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected