#37551 REF: simplify Index.take, MultiIndex.take

Merged jbrockmendel
Coverage Reach
core/arrays/sparse/array.py core/arrays/sparse/dtype.py core/arrays/sparse/accessor.py core/arrays/sparse/scipy_sparse.py core/arrays/sparse/__init__.py core/arrays/categorical.py core/arrays/datetimelike.py core/arrays/datetimes.py core/arrays/timedeltas.py core/arrays/interval.py core/arrays/period.py core/arrays/integer.py core/arrays/boolean.py core/arrays/floating.py core/arrays/base.py core/arrays/numpy_.py core/arrays/string_.py core/arrays/masked.py core/arrays/_mixins.py core/arrays/_arrow_utils.py core/arrays/_ranges.py core/arrays/__init__.py core/indexes/base.py core/indexes/multi.py core/indexes/interval.py core/indexes/datetimelike.py core/indexes/range.py core/indexes/datetimes.py core/indexes/period.py core/indexes/category.py core/indexes/numeric.py core/indexes/extension.py core/indexes/accessors.py core/indexes/api.py core/indexes/timedeltas.py core/indexes/frozen.py core/internals/blocks.py core/internals/managers.py core/internals/construction.py core/internals/concat.py core/internals/ops.py core/internals/__init__.py core/groupby/groupby.py core/groupby/generic.py core/groupby/ops.py core/groupby/grouper.py core/groupby/numba_.py core/groupby/base.py core/groupby/categorical.py core/groupby/__init__.py core/reshape/merge.py core/reshape/reshape.py core/reshape/pivot.py core/reshape/concat.py core/reshape/tile.py core/reshape/melt.py core/reshape/util.py core/reshape/api.py core/generic.py core/dtypes/cast.py core/dtypes/dtypes.py core/dtypes/common.py core/dtypes/missing.py core/dtypes/concat.py core/dtypes/base.py core/dtypes/inference.py core/dtypes/generic.py core/dtypes/api.py core/frame.py core/computation/expr.py core/computation/pytables.py core/computation/ops.py core/computation/expressions.py core/computation/eval.py core/computation/scope.py core/computation/align.py core/computation/engines.py core/computation/parsing.py core/computation/common.py core/computation/check.py core/computation/api.py core/window/rolling.py core/window/ewm.py core/window/indexers.py core/window/expanding.py core/window/common.py core/window/numba_.py core/window/__init__.py core/strings/accessor.py core/strings/object_array.py core/strings/base.py core/strings/__init__.py core/series.py core/indexing.py core/algorithms.py core/ops/array_ops.py core/ops/__init__.py core/ops/docstrings.py core/ops/missing.py core/ops/mask_ops.py core/ops/common.py core/ops/methods.py core/ops/roperator.py core/ops/invalid.py core/ops/dispatch.py core/nanops.py core/resample.py core/tools/datetimes.py core/tools/times.py core/tools/numeric.py core/tools/timedeltas.py core/base.py core/missing.py core/aggregation.py core/apply.py core/sorting.py core/common.py core/construction.py core/config_init.py core/util/hashing.py core/util/numba_.py core/indexers.py core/array_algos/masked_reductions.py core/array_algos/replace.py core/array_algos/transforms.py core/accessor.py core/arraylike.py core/flags.py core/api.py core/shared_docs.py core/index.py core/sparse/api.py io/formats/format.py io/formats/style.py io/formats/excel.py io/formats/html.py io/formats/latex.py io/formats/info.py io/formats/printing.py io/formats/csvs.py io/formats/string.py io/formats/css.py io/formats/console.py io/pytables.py io/excel/_base.py io/excel/_openpyxl.py io/excel/_odfreader.py io/excel/_odswriter.py io/excel/_xlsxwriter.py io/excel/_xlwt.py io/excel/_util.py io/excel/_xlrd.py io/excel/_pyxlsb.py io/sas/sas7bdat.py io/sas/sas_xport.py io/sas/sas_constants.py io/sas/sasreader.py io/json/_json.py io/json/_table_schema.py io/json/_normalize.py io/html.py io/clipboard/__init__.py io/common.py io/parquet.py io/date_converters.py io/feather_format.py io/gbq.py io/api.py io/spss.py io/orc.py io/clipboards.py plotting/_matplotlib/core.py plotting/_matplotlib/converter.py plotting/_matplotlib/misc.py plotting/_matplotlib/boxplot.py plotting/_matplotlib/tools.py plotting/_matplotlib/hist.py plotting/_matplotlib/timeseries.py plotting/_matplotlib/style.py plotting/_matplotlib/__init__.py plotting/_matplotlib/compat.py plotting/_core.py plotting/_misc.py _testing.py util/_decorators.py util/_validators.py util/_doctools.py util/_test_decorators.py util/_print_versions.py util/_depr_module.py util/_tester.py util/_exceptions.py util/testing.py tseries/frequencies.py tseries/holiday.py tseries/api.py tseries/offsets.py _config/config.py _config/localization.py _config/display.py _config/dates.py _config/__init__.py compat/numpy/function.py compat/numpy/__init__.py compat/pickle_compat.py compat/_optional.py compat/__init__.py compat/chainmap.py errors/__init__.py _libs/tslibs/__init__.py _libs/__init__.py api/extensions/__init__.py api/types/__init__.py api/indexers/__init__.py

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.


@@ -2018,29 +2018,13 @@
Loading
2018 2018
    def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
2019 2019
        nv.validate_take(tuple(), kwargs)
2020 2020
        indices = ensure_platform_int(indices)
2021 -
        taken = self._assert_take_fillable(
2022 -
            self.codes,
2023 -
            indices,
2024 -
            allow_fill=allow_fill,
2025 -
            fill_value=fill_value,
2026 -
            na_value=-1,
2027 -
        )
2028 -
        return MultiIndex(
2029 -
            levels=self.levels, codes=taken, names=self.names, verify_integrity=False
2030 -
        )
2031 2021
2032 -
    def _assert_take_fillable(
2033 -
        self, values, indices, allow_fill=True, fill_value=None, na_value=None
2034 -
    ):
2035 -
        """ Internal method to handle NA filling of take """
2036 2022
        # only fill if we are passing a non-None fill_value
2037 -
        if allow_fill and fill_value is not None:
2038 -
            if (indices < -1).any():
2039 -
                msg = (
2040 -
                    "When allow_fill=True and fill_value is not None, "
2041 -
                    "all indices must be >= -1"
2042 -
                )
2043 -
                raise ValueError(msg)
2023 +
        allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices)
2024 +
2025 +
        na_value = -1
2026 +
2027 +
        if allow_fill:
2044 2028
            taken = [lab.take(indices) for lab in self.codes]
2045 2029
            mask = indices == -1
2046 2030
            if mask.any():
@@ -2052,7 +2036,10 @@
Loading
2052 2036
                taken = masked
2053 2037
        else:
2054 2038
            taken = [lab.take(indices) for lab in self.codes]
2055 -
        return taken
2039 +
2040 +
        return MultiIndex(
2041 +
            levels=self.levels, codes=taken, names=self.names, verify_integrity=False
2042 +
        )
2056 2043
2057 2044
    def append(self, other):
2058 2045
        """

@@ -733,44 +733,36 @@
Loading
733 733
        if kwargs:
734 734
            nv.validate_take(tuple(), kwargs)
735 735
        indices = ensure_platform_int(indices)
736 -
        if self._can_hold_na:
737 -
            taken = self._assert_take_fillable(
738 -
                self._values,
739 -
                indices,
740 -
                allow_fill=allow_fill,
741 -
                fill_value=fill_value,
742 -
                na_value=self._na_value,
743 -
            )
744 -
        else:
745 -
            if allow_fill and fill_value is not None:
746 -
                cls_name = type(self).__name__
747 -
                raise ValueError(
748 -
                    f"Unable to fill values because {cls_name} cannot contain NA"
749 -
                )
750 -
            taken = self._values.take(indices)
736 +
        allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices)
737 +
738 +
        # Note: we discard fill_value and use self._na_value, only relevant
739 +
        #  in the case where allow_fill is True and fill_value is not None
740 +
        taken = algos.take(
741 +
            self._values, indices, allow_fill=allow_fill, fill_value=self._na_value
742 +
        )
751 743
        return self._shallow_copy(taken)
752 744
753 -
    def _assert_take_fillable(
754 -
        self, values, indices, allow_fill=True, fill_value=None, na_value=np.nan
755 -
    ):
745 +
    def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
756 746
        """
757 -
        Internal method to handle NA filling of take.
747 +
        We only use pandas-style take when allow_fill is True _and_
748 +
        fill_value is not None.
758 749
        """
759 -
        indices = ensure_platform_int(indices)
760 -
761 -
        # only fill if we are passing a non-None fill_value
762 750
        if allow_fill and fill_value is not None:
763 -
            if (indices < -1).any():
751 +
            # only fill if we are passing a non-None fill_value
752 +
            if self._can_hold_na:
753 +
                if (indices < -1).any():
754 +
                    raise ValueError(
755 +
                        "When allow_fill=True and fill_value is not None, "
756 +
                        "all indices must be >= -1"
757 +
                    )
758 +
            else:
759 +
                cls_name = type(self).__name__
764 760
                raise ValueError(
765 -
                    "When allow_fill=True and fill_value is not None, "
766 -
                    "all indices must be >= -1"
761 +
                    f"Unable to fill values because {cls_name} cannot contain NA"
767 762
                )
768 -
            taken = algos.take(
769 -
                values, indices, allow_fill=allow_fill, fill_value=na_value
770 -
            )
771 763
        else:
772 -
            taken = algos.take(values, indices, allow_fill=False, fill_value=na_value)
773 -
        return taken
764 +
            allow_fill = False
765 +
        return allow_fill
774 766
775 767
    _index_shared_docs[
776 768
        "repeat"

Learn more Showing 2 files with coverage changes found.

Changes in pandas/io/gbq.py
-2
+2
Loading file...
Changes in pandas/core/frame.py
-2
+2
Loading file...
Files Coverage
pandas -0.01% 94.20%
Project Totals (214 files) 94.20%
Loading