Repository: pandas-dev/pandas
File: pandas/io/formats/excel.py (Python)

Code before:

        {storage_options}

        engine_kwargs: dict, optional
            Arbitrary keyword arguments passed to excel engine.
        """
        from pandas.io.excel import ExcelWriter

        num_rows, num_cols = self.df.shape
        if num_rows > self.max_rows or num_cols > self.max_cols:
            raise ValueError(
                f"This sheet is too large! Your sheet size is: {num_rows}, {num_cols} "
                f"Max sheet size is: {self.max_rows}, {self.max_cols}"
            )

        if self.autofilter:
            if num_cols == 0:
                indexoffset = 0
            elif self.index:
                if isinstance(self.df.index, MultiIndex):
                    indexoffset = self.df.index.nlevels - 1
                    if self.merge_cells:
                        warnings.warn(
                            "Excel filters merged cells by showing only the first row."
                            "'autofiler' and 'merge_cells' should not "
                            "be used simultaneously.",
                            UserWarning,
                            stacklevel=find_stack_level(),
                        )
                else:
                    indexoffset = 0
            else:
                indexoffset = -1
            start = f"{self._num2excel(startcol)}{startrow + 1}"
            autofilter_end_column = self._num2excel(startcol + num_cols + indexoffset)
            end = f"{autofilter_end_column}{startrow + num_rows + 1}"
            autofilter_range = f"{start}:{end}"
        else:
            autofilter_range = None

        if engine_kwargs is None:
            engine_kwargs = {}

        formatted_cells = self.get_formatted_cells()
        if isinstance(writer, ExcelWriter):
            need_save = False
        else:
            writer = ExcelWriter(
                writer,
                engine=engine,
                storage_options=storage_options,

Review comment (suggestion):
Because of blanks, the autofilter here will produce what I consider invalid results. As such, I think we should raise rather than warn.

Code after:

        {storage_options}

        engine_kwargs: dict, optional
            Arbitrary keyword arguments passed to excel engine.
        """
        from pandas.io.excel import ExcelWriter

        num_rows, num_cols = self.df.shape
        if num_rows > self.max_rows or num_cols > self.max_cols:
            raise ValueError(
                f"This sheet is too large! Your sheet size is: {num_rows}, {num_cols} "
                f"Max sheet size is: {self.max_rows}, {self.max_cols}"
            )

        if self.autofilter:
            # default offset for header row
            startrowsoffset = 1
            endrowsoffset = 1

            if num_cols == 0:
                indexoffset = 0
            elif self.index:
                indexoffset = 0
                if isinstance(self.df.index, MultiIndex):
                    if self.merge_cells:
                        raise ValueError(
                            "Excel filters merged cells by showing only the first row. "
                            "'autofilter' and 'merge_cells' cannot "
                            "be used simultaneously."
                        )
                    else:
                        indexoffset = self.df.index.nlevels - 1

                if isinstance(self.columns, MultiIndex):
                    if self.merge_cells:
                        raise ValueError(
                            "Excel filters merged cells by showing only the first row. "
                            "'autofilter' and 'merge_cells' cannot "
                            "be used simultaneously."
                        )
                    else:
                        startrowsoffset = self.columns.nlevels
                        # multindex columns add a blank row between header and data
                        endrowsoffset = self.columns.nlevels + 1
            else:
                # no index column
                indexoffset = -1
            start = f"{self._num2excel(startcol)}{startrow + startrowsoffset}"
            autofilter_end_column = self._num2excel(startcol + num_cols + indexoffset)
            end = f"{autofilter_end_column}{startrow + num_rows + endrowsoffset}"