Apply the given pairwise function given 2 pandas objects (DataFrame/Series)
(
self,
target: DataFrame | Series,
other: DataFrame | Series | None,
pairwise: bool | None,
func: Callable[[DataFrame | Series, DataFrame | Series], DataFrame | Series],
numeric_only: bool,
)
| 751 | return result |
| 752 | |
| 753 | def _apply_pairwise( |
| 754 | self, |
| 755 | target: DataFrame | Series, |
| 756 | other: DataFrame | Series | None, |
| 757 | pairwise: bool | None, |
| 758 | func: Callable[[DataFrame | Series, DataFrame | Series], DataFrame | Series], |
| 759 | numeric_only: bool, |
| 760 | ) -> DataFrame | Series: |
| 761 | """ |
| 762 | Apply the given pairwise function given 2 pandas objects (DataFrame/Series) |
| 763 | """ |
| 764 | # Manually drop the grouping column first |
| 765 | target = target.drop(columns=self._grouper.names, errors="ignore") |
| 766 | result = super()._apply_pairwise(target, other, pairwise, func, numeric_only) |
| 767 | # 1) Determine the levels + codes of the groupby levels |
| 768 | if other is not None and not all( |
| 769 | len(group) == len(other) for group in self._grouper.indices.values() |
| 770 | ): |
| 771 | # GH 42915 |
| 772 | # len(other) != len(any group), so must reindex (expand) the result |
| 773 | # from flex_binary_moment to a "transform"-like result |
| 774 | # per groupby combination |
| 775 | old_result_len = len(result) |
| 776 | result = concat( |
| 777 | [ |
| 778 | result.take(gb_indices).reindex(result.index) |
| 779 | for gb_indices in self._grouper.indices.values() |
| 780 | ] |
| 781 | ) |
| 782 | |
| 783 | gb_pairs = ( |
| 784 | com.maybe_make_list(pair) for pair in self._grouper.indices.keys() |
| 785 | ) |
| 786 | groupby_codes = [] |
| 787 | groupby_levels = [] |
| 788 | # e.g. [[1, 2], [4, 5]] as [[1, 4], [2, 5]] |
| 789 | for gb_level_pair in map(list, zip(*gb_pairs, strict=True)): |
| 790 | labels = np.repeat(np.array(gb_level_pair), old_result_len) |
| 791 | codes, levels = factorize(labels) |
| 792 | groupby_codes.append(codes) |
| 793 | groupby_levels.append(levels) |
| 794 | else: |
| 795 | # pairwise=True or len(other) == len(each group), so repeat |
| 796 | # the groupby labels by the number of columns in the original object |
| 797 | groupby_codes = self._grouper.codes |
| 798 | # error: Incompatible types in assignment (expression has type |
| 799 | # "List[Index]", variable has type "List[Union[ndarray, Index]]") |
| 800 | groupby_levels = self._grouper.levels # type: ignore[assignment] |
| 801 | |
| 802 | group_indices = self._grouper.indices.values() |
| 803 | if group_indices: |
| 804 | indexer = np.concatenate(list(group_indices)) |
| 805 | else: |
| 806 | indexer = np.array([], dtype=np.intp) |
| 807 | |
| 808 | if target.ndim == 1: |
| 809 | repeat_by = 1 |
| 810 | else: |
nothing calls this directly
no test coverage detected