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

Method _apply

pandas/io/formats/style.py:1925–1982  ·  view source on GitHub ↗
(
        self,
        func: Callable,
        axis: Axis | None = 0,
        subset: Subset | None = None,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

1923 delattr(self, attr)
1924
1925 def _apply(
1926 self,
1927 func: Callable,
1928 axis: Axis | None = 0,
1929 subset: Subset | None = None,
1930 **kwargs,
1931 ) -> Styler:
1932 subset = slice(None) if subset is None else subset
1933 subset = non_reducing_slice(subset)
1934 data = self.data.loc[subset]
1935 if data.empty:
1936 result = DataFrame()
1937 elif axis is None:
1938 result = func(data, **kwargs)
1939 if not isinstance(result, DataFrame):
1940 if not isinstance(result, np.ndarray):
1941 raise TypeError(
1942 f"Function {func!r} must return a DataFrame or ndarray "
1943 f"when passed to `Styler.apply` with axis=None"
1944 )
1945 if data.shape != result.shape:
1946 raise ValueError(
1947 f"Function {func!r} returned ndarray with wrong shape.\n"
1948 f"Result has shape: {result.shape}\n"
1949 f"Expected shape: {data.shape}"
1950 )
1951 result = DataFrame(result, index=data.index, columns=data.columns)
1952 else:
1953 axis = self.data._get_axis_number(axis)
1954 if axis == 0:
1955 result = data.apply(func, axis=0, **kwargs)
1956 else:
1957 result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005
1958
1959 if isinstance(result, Series):
1960 raise ValueError(
1961 f"Function {func!r} resulted in the apply method collapsing to a "
1962 f"Series.\nUsually, this is the result of the function returning a "
1963 f"single value, instead of list-like."
1964 )
1965 msg = (
1966 f"Function {func!r} created invalid {{0}} labels.\nUsually, this is "
1967 f"the result of the function returning a "
1968 f"{'Series' if axis is not None else 'DataFrame'} which contains invalid "
1969 f"labels, or returning an incorrectly shaped, list-like object which "
1970 f"cannot be mapped to labels, possibly due to applying the function along "
1971 f"the wrong axis.\n"
1972 f"Result {{0}} has shape: {{1}}\n"
1973 f"Expected {{0}} shape: {{2}}"
1974 )
1975 if not all(result.index.isin(data.index)):
1976 raise ValueError(msg.format("index", result.index.shape, data.index.shape))
1977 if not all(result.columns.isin(data.columns)):
1978 raise ValueError(
1979 msg.format("columns", result.columns.shape, data.columns.shape)
1980 )
1981 self._update_ctx(result)
1982 return self

Callers 10

test_normalizeMethod · 0.45
test_return_typeMethod · 0.45
test_normalizeMethod · 0.45
test_rsubFunction · 0.45
assert_offset_equalFunction · 0.45
test_apply_cornerMethod · 0.45
test_bunched_yearendsFunction · 0.45
test_bad_apply_shapeMethod · 0.45
test_apply_bad_returnMethod · 0.45
test_apply_bad_labelsMethod · 0.45

Calls 8

_update_ctxMethod · 0.95
non_reducing_sliceFunction · 0.90
DataFrameClass · 0.90
_get_axis_numberMethod · 0.80
funcFunction · 0.50
applyMethod · 0.45
isinMethod · 0.45
formatMethod · 0.45

Tested by 9

test_normalizeMethod · 0.36
test_return_typeMethod · 0.36
test_normalizeMethod · 0.36
test_rsubFunction · 0.36
test_apply_cornerMethod · 0.36
test_bunched_yearendsFunction · 0.36
test_bad_apply_shapeMethod · 0.36
test_apply_bad_returnMethod · 0.36
test_apply_bad_labelsMethod · 0.36