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

Method apply_raw

pandas/core/apply.py:1086–1135  ·  view source on GitHub ↗

apply to the values as a numpy array

(self, engine="python", engine_kwargs=None)

Source from the content-addressed store, hash-verified

1084 return self.obj.copy()
1085
1086 def apply_raw(self, engine="python", engine_kwargs=None):
1087 """apply to the values as a numpy array"""
1088
1089 def wrap_function(func):
1090 """
1091 Wrap user supplied function to work around numpy issue.
1092
1093 see https://github.com/numpy/numpy/issues/8352
1094 """
1095
1096 def wrapper(*args, **kwargs):
1097 result = func(*args, **kwargs)
1098 if isinstance(result, str):
1099 result = np.array(result, dtype=object)
1100 return result
1101
1102 return wrapper
1103
1104 if engine == "numba":
1105 args, kwargs = prepare_function_arguments(
1106 self.func, # type: ignore[arg-type]
1107 self.args,
1108 self.kwargs,
1109 num_required_args=1,
1110 )
1111 # error: Argument 1 to "__call__" of "_lru_cache_wrapper" has
1112 # incompatible type "Callable[..., Any] | str | list[Callable
1113 # [..., Any] | str] | dict[Hashable,Callable[..., Any] | str |
1114 # list[Callable[..., Any] | str]]"; expected "Hashable"
1115 nb_looper = generate_apply_looper(
1116 self.func, # type: ignore[arg-type]
1117 **get_jit_arguments(engine_kwargs),
1118 )
1119 result = nb_looper(self.values, self.axis, *args)
1120 # If we made the result 2-D, squeeze it back to 1-D
1121 result = np.squeeze(result)
1122 else:
1123 result = np.apply_along_axis(
1124 wrap_function(self.func),
1125 self.axis,
1126 self.values,
1127 *self.args,
1128 **self.kwargs,
1129 )
1130
1131 # TODO: mixed type case
1132 if result.ndim == 2:
1133 return self.obj._constructor(result, index=self.index, columns=self.columns)
1134 else:
1135 return self.obj._constructor_sliced(result, index=self.agg_axis)
1136
1137 def apply_broadcast(self, target: DataFrame) -> DataFrame:
1138 assert callable(self.func)

Callers 1

applyMethod · 0.95

Calls 7

generate_apply_looperFunction · 0.90
get_jit_argumentsFunction · 0.90
nb_looperFunction · 0.85
squeezeMethod · 0.80
_constructorMethod · 0.45
_constructor_slicedMethod · 0.45

Tested by

no test coverage detected