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

Method apply_empty_result

pandas/core/apply.py:1040–1084  ·  view source on GitHub ↗

we have an empty result; at least 1 axis is 0 we will try to apply the function to an empty series in order to see if this is a reduction function

(self)

Source from the content-addressed store, hash-verified

1038 return result
1039
1040 def apply_empty_result(self):
1041 """
1042 we have an empty result; at least 1 axis is 0
1043
1044 we will try to apply the function to an empty
1045 series in order to see if this is a reduction function
1046 """
1047 assert callable(self.func)
1048
1049 # we are not asked to reduce or infer reduction
1050 # so just return a copy of the existing object
1051 if self.result_type not in ["reduce", None]:
1052 return self.obj.copy()
1053
1054 # we may need to infer
1055 should_reduce = self.result_type == "reduce"
1056
1057 from pandas import Series
1058
1059 if not should_reduce:
1060 try:
1061 if self.axis == 0:
1062 r = self.func(
1063 Series([], dtype=np.float64), *self.args, **self.kwargs
1064 )
1065 else:
1066 r = self.func(
1067 Series(index=self.columns, dtype=np.float64),
1068 *self.args,
1069 **self.kwargs,
1070 )
1071 except Exception:
1072 pass
1073 else:
1074 should_reduce = not isinstance(r, Series)
1075
1076 if should_reduce:
1077 if len(self.agg_axis):
1078 r = self.func(Series([], dtype=np.float64), *self.args, **self.kwargs)
1079 else:
1080 r = np.nan
1081
1082 return self.obj._constructor_sliced(r, index=self.agg_axis)
1083 else:
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"""

Callers 1

applyMethod · 0.95

Calls 4

SeriesClass · 0.90
copyMethod · 0.45
funcMethod · 0.45
_constructor_slicedMethod · 0.45

Tested by

no test coverage detected