ronantakizawa/codereview-bench huggingface dataset:
Repository: pandas-dev/pandas
File: pandas/io/excel/_xlsxwriter.py (Python)

Code before:
        storage_options: StorageOptions | None = None,
        if_sheet_exists: ExcelWriterIfSheetExists | None = None,
        engine_kwargs: dict[str, Any] | None = None,
        **kwargs,
    ) -> None:
        # Use the xlsxwriter module as the Excel writer.
        from xlsxwriter import Workbook

        engine_kwargs = combine_kwargs(engine_kwargs, kwargs)

        if mode == "a":
            raise ValueError("Append mode is not supported with xlsxwriter!")

        super().__init__(
            path,
            engine=engine,
            date_format=date_format,
            datetime_format=datetime_format,
            mode=mode,
            storage_options=storage_options,
            if_sheet_exists=if_sheet_exists,
            engine_kwargs=engine_kwargs,
        )

        try:
            self._book = Workbook(self._handles.handle, **engine_kwargs)  # type: ignore[arg-type]
        except TypeError:
            self._handles.handle.close()
            raise

    @property
    def book(self):
        """
        Book instance of class xlsxwriter.Workbook.

        This attribute can be used to access engine-specific features.
        """
        return self._book

    @property
    def sheets(self) -> dict[str, Any]:
        result = self.book.sheetnames
        return result

    def _save(self) -> None:
        """
        Save workbook to disk.
        """
        self.book.close()

    def _write_cells(

Review comment (bug):
i removed `type: ignore` as it was showing ci check fail.

Code after:
        storage_options: StorageOptions | None = None,
        if_sheet_exists: ExcelWriterIfSheetExists | None = None,
        engine_kwargs: dict[str, Any] | None = None,
        **kwargs,
    ) -> None:
        # Use the xlsxwriter module as the Excel writer.
        from xlsxwriter import Workbook

        engine_kwargs = combine_kwargs(engine_kwargs, kwargs)

        if mode == "a":
            raise ValueError("Append mode is not supported with xlsxwriter!")

        super().__init__(
            path,
            engine=engine,
            date_format=date_format,
            datetime_format=datetime_format,
            mode=mode,
            storage_options=storage_options,
            if_sheet_exists=if_sheet_exists,
            engine_kwargs=engine_kwargs,
        )

        try:
            self._book = Workbook(self._handles.handle, **engine_kwargs)
        except TypeError:
            self._handles.handle.close()
            raise

    @property
    def book(self):
        """
        Book instance of class xlsxwriter.Workbook.

        This attribute can be used to access engine-specific features.
        """
        return self._book

    @property
    def sheets(self) -> dict[str, Any]:
        result = self.book.sheetnames
        return result

    def _save(self) -> None:
        """
        Save workbook to disk.
        """
        self.book.close()

    def _write_cells(
Repository: pandas-dev/pandas
File: pandas/core/base.py (Python)

Code before:
        a    category
        b      str
        c    category
        d    category
        dtype: object

        >>> df.dtypes.value_counts()
        category    2
        category    1
        str         1
        Name: count, dtype: int64
        """
        return algorithms.value_counts_internal(
            self,
            sort=sort,
            ascending=ascending,
            normalize=normalize,
            bins=bins,
            dropna=dropna,
        )

    def unique(self):
        values = self._values
        if not isinstance(values, np.ndarray):
            # i.e. ExtensionArray
            result: np.ndarray | ExtensionArray = values.unique()
        else:
            result = algorithms.unique1d(values)
        return result

    @final
    def nunique(self, dropna: bool = True) -> int:
        """
        Return number of unique elements in the object.

        Excludes NA values by default.

        Parameters
        ----------
        dropna : bool, default True
            Don't include NaN in the count.

        Returns
        -------
        int
            A integer indicating the number of unique elements in the object.

        See Also
        --------
        DataFrame.nunique: Method nunique for DataFrame.
        Series.count: Count non-NA/null observations in the Series.

Review comment (suggestion):
If you run mypy with `--local-partial-types --allow-redefinition-new` this wouldn't be needed anymore.

https://mypy.readthedocs.io/en/latest/command_line.html#cmdoption-mypy-allow-redefinition-new

Code after:
        a    category
        b      str
        c    category
        d    category
        dtype: object

        >>> df.dtypes.value_counts()
        category    2
        category    1
        str         1
        Name: count, dtype: int64
        """
        return algorithms.value_counts_internal(
            self,
            sort=sort,
            ascending=ascending,
            normalize=normalize,
            bins=bins,
            dropna=dropna,
        )

    def unique(self):
        values = self._values
        if not isinstance(values, np.ndarray):
            # i.e. ExtensionArray
            result = values.unique()
        else:
            result = algorithms.unique1d(values)  # type: ignore[assignment]
        return result

    @final
    def nunique(self, dropna: bool = True) -> int:
        """
        Return number of unique elements in the object.

        Excludes NA values by default.

        Parameters
        ----------
        dropna : bool, default True
            Don't include NaN in the count.

        Returns
        -------
        int
            A integer indicating the number of unique elements in the object.

        See Also
        --------
        DataFrame.nunique: Method nunique for DataFrame.
        Series.count: Count non-NA/null observations in the Series.
Repository: pandas-dev/pandas
File: pandas/tests/frame/test_reductions.py (Python)

Code before:

    kwargs = {}
    if method not in ("count", "nunique", "quantile"):
        kwargs["skipna"] = skipna
    if method in ("prod", "product", "sum"):
        kwargs["min_count"] = min_count

    if not skipna and method in ("idxmax", "idxmin"):
        with pytest.raises(ValueError, match="encountered an NA value"):
            getattr(df, method)(axis=1, **kwargs)
        with pytest.raises(ValueError, match="Encountered an NA value"):
            getattr(expected_df, method)(axis=1, **kwargs)
        return
    result = getattr(df, method)(axis=1, **kwargs)
    expected = getattr(expected_df, method)(axis=1, **kwargs)
    if method not in ("idxmax", "idxmin"):
        expected = expected.astype(expected_dtype)
    tm.assert_series_equal(result, expected)


def test_mean_nullable_int_axis_1():
    """
    Test DataFrame.mean(axis=1) with nullable Int64 dtype.

    Ensures correct results with skipna=True and skipna=False.
    """
    df = DataFrame(
        {"a": [1, 2, 3, 4], "b": Series([1, 2, 4, None], dtype=pd.Int64Dtype())}
    )

    result = df.mean(axis=1, skipna=True)
    expected = Series([1.0, 2.0, 3.5, 4.0], dtype="Float64")
    tm.assert_series_equal(result, expected)

    result = df.mean(axis=1, skipna=False)
    expected = Series([1.0, 2.0, 3.5, pd.NA], dtype="Float64")
    tm.assert_series_equal(result, expected)


Review comment (suggestion):
we generally use comments instead of docstrings in tests.  can you just do `# GH#???` referring back to the original issue

Code after:

    kwargs = {}
    if method not in ("count", "nunique", "quantile"):
        kwargs["skipna"] = skipna
    if method in ("prod", "product", "sum"):
        kwargs["min_count"] = min_count

    if not skipna and method in ("idxmax", "idxmin"):
        with pytest.raises(ValueError, match="encountered an NA value"):
            getattr(df, method)(axis=1, **kwargs)
        with pytest.raises(ValueError, match="Encountered an NA value"):
            getattr(expected_df, method)(axis=1, **kwargs)
        return
    result = getattr(df, method)(axis=1, **kwargs)
    expected = getattr(expected_df, method)(axis=1, **kwargs)
    if method not in ("idxmax", "idxmin"):
        expected = expected.astype(expected_dtype)
    tm.assert_series_equal(result, expected)


def test_mean_nullable_int_axis_1():
    # GH##36585
    df = DataFrame(
        {"a": [1, 2, 3, 4], "b": Series([1, 2, 4, None], dtype=pd.Int64Dtype())}
    )

    result = df.mean(axis=1, skipna=True)
    expected = Series([1.0, 2.0, 3.5, 4.0], dtype="Float64")
    tm.assert_series_equal(result, expected)

    result = df.mean(axis=1, skipna=False)
    expected = Series([1.0, 2.0, 3.5, pd.NA], dtype="Float64")
    tm.assert_series_equal(result, expected)
Repository: pandas-dev/pandas
File: pandas/tests/indexing/multiindex/test_multiindex.py (Python)

Code before:
    @pytest.mark.parametrize("operation", ["div", "mul", "add", "sub"])
    def test_groupyby_rename_categories_operation_with_multiindex(self, operation):
        # GH#51500
        data = DataFrame(
            [["C", "B", "B"], ["B", "A", "A"], ["B", "A", "B"]], columns=["0", "1", "2"]
        )
        data["0"] = data["0"].astype("category")
        data["0"] = data["0"].cat.rename_categories({"C": "B", "B": "C"})

        a = data.groupby(by=["0", "1"])["2"].value_counts()
        b = data.groupby(by=["0", "1"]).size()

        result = getattr(a, operation)(b)
        expected = getattr(a, operation)(b.sort_index(ascending=False))

        tm.assert_series_equal(result, expected)

    def test_multiindex_assign_aligns_as_implicit_tuple(self):
        # GH 61841
        cols = MultiIndex.from_tuples([("A", "B")])
        df1 = DataFrame([[i] for i in range(3)], columns=cols)
        df2 = DataFrame([[i] for i in range(3)], columns=cols)
        df3 = DataFrame([[i] for i in range(3)], columns=cols)
        s1 = df1["A"].rolling(2).mean()
        s2 = df2["A"].rolling(2).mean()
        s3 = df3["A"].rolling(2).mean()

        df2["C"] = s2
        df3[("C", "")] = s3
        tm.assert_frame_equal(df2, df3)

        df1["C"] = s1
        tm.assert_frame_equal(df1, df2)
        tm.assert_frame_equal(df1, df3)

        df1["C"] = s1
        tm.assert_frame_equal(df1, df2)
        tm.assert_frame_equal(df1, df3)


Review comment (refactor):
Could you just create these duplicates with `.copy()`?

Code after:
    @pytest.mark.parametrize("operation", ["div", "mul", "add", "sub"])
    def test_groupyby_rename_categories_operation_with_multiindex(self, operation):
        # GH#51500
        data = DataFrame(
            [["C", "B", "B"], ["B", "A", "A"], ["B", "A", "B"]], columns=["0", "1", "2"]
        )
        data["0"] = data["0"].astype("category")
        data["0"] = data["0"].cat.rename_categories({"C": "B", "B": "C"})

        a = data.groupby(by=["0", "1"])["2"].value_counts()
        b = data.groupby(by=["0", "1"]).size()

        result = getattr(a, operation)(b)
        expected = getattr(a, operation)(b.sort_index(ascending=False))

        tm.assert_series_equal(result, expected)

    def test_multiindex_assign_aligns_as_implicit_tuple(self):
        # GH 61841
        cols = MultiIndex.from_tuples([("A", "B")])
        df1 = DataFrame([[i] for i in range(3)], columns=cols)
        df2 = df1.copy()
        df3 = df1.copy()
        s1 = df1["A"].rolling(2).mean()
        s2 = s1.copy()
        s3 = s1.copy()

        df2["C"] = s2
        df3[("C", "")] = s3
        tm.assert_frame_equal(df2, df3)

        df1["C"] = s1
        tm.assert_frame_equal(df1, df2)
        tm.assert_frame_equal(df1, df3)

        df1["C"] = s1
        tm.assert_frame_equal(df1, df2)
        tm.assert_frame_equal(df1, df3)
Repository: pandas-dev/pandas
File: pandas/tests/io/parser/common/test_chunksize.py (Python)

Code before:
        )
        with pytest.raises(ValueError, match=msg):
            if iterator:
                with parser.read_csv(data, chunksize=nrows) as reader:
                    next(iter(reader))
            else:
                parser.read_csv(data, nrows=nrows)
        return

    if iterator:
        with parser.read_csv(data, chunksize=nrows) as reader:
            result = next(iter(reader))
    else:
        result = parser.read_csv(data, nrows=nrows)

    tm.assert_frame_equal(result, expected)


def test_read_csv_memory_growth_chunksize(temp_file, all_parsers):
    # see gh-24805
    #
    # Let's just make sure that we don't crash
    # as we iteratively process all chunks.
    parser = all_parsers

    path = str(temp_file)
    with open(path, "w", encoding="utf-8") as f:
        for i in range(1000):
            f.write(str(i) + "\n")

    if parser.engine == "pyarrow":
        msg = "The 'chunksize' option is not supported with the 'pyarrow' engine"
        with pytest.raises(ValueError, match=msg):
            with parser.read_csv(path, chunksize=20) as result:
                for _ in result:
                    pass
        return

    with parser.read_csv(path, chunksize=20) as result:
        for _ in result:
            pass


def test_chunksize_with_usecols_second_block_shorter(all_parsers):
    # GH#21211
    parser = all_parsers
    data = """1,2,3,4
5,6,7,8
9,10,11
"""


Review comment (suggestion):
Can you just use `temp_file` directly, instead of converting to a `str`? (Unless the test appears to explicitly testing a string path) 

Code after:
        )
        with pytest.raises(ValueError, match=msg):
            if iterator:
                with parser.read_csv(data, chunksize=nrows) as reader:
                    next(iter(reader))
            else:
                parser.read_csv(data, nrows=nrows)
        return

    if iterator:
        with parser.read_csv(data, chunksize=nrows) as reader:
            result = next(iter(reader))
    else:
        result = parser.read_csv(data, nrows=nrows)

    tm.assert_frame_equal(result, expected)


def test_read_csv_memory_growth_chunksize(temp_file, all_parsers):
    # see gh-24805
    #
    # Let's just make sure that we don't crash
    # as we iteratively process all chunks.
    parser = all_parsers

    with open(temp_file, "w", encoding="utf-8") as f:
        for i in range(1000):
            f.write(str(i) + "\n")

    if parser.engine == "pyarrow":
        msg = "The 'chunksize' option is not supported with the 'pyarrow' engine"
        with pytest.raises(ValueError, match=msg):
            with parser.read_csv(temp_file, chunksize=20) as result:
                for _ in result:
                    pass
        return

    with parser.read_csv(temp_file, chunksize=20) as result:
        for _ in result:
            pass


def test_chunksize_with_usecols_second_block_shorter(all_parsers):
    # GH#21211
    parser = all_parsers
    data = """1,2,3,4
5,6,7,8
9,10,11
"""

    if parser.engine == "pyarrow":
Repository: pandas-dev/pandas
File: pandas/core/nanops.py (Python)

Code before:
    """
    dtype = values.dtype
    values, mask = _get_values(values, skipna, fill_value=0, mask=mask)
    dtype_sum = _get_dtype_max(dtype)
    if dtype.kind == "f":
        dtype_sum = dtype
    elif dtype.kind == "m":
        dtype_sum = np.dtype(np.float64)

    the_sum = values.sum(axis, dtype=dtype_sum)
    the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count)

    return the_sum


def _mask_datetimelike_result(
    result: np.ndarray | np.datetime64 | np.timedelta64,
    axis: AxisInt | None,
    mask: npt.NDArray[np.bool_],
    orig_values: np.ndarray,
) -> np.ndarray | np.datetime64 | np.timedelta64 | NaTType:
    if isinstance(result, np.ndarray):
        # we need to apply the mask
        result = result.astype("i8").view(orig_values.dtype)
        axis_mask = mask.any(axis=axis)
        # error: Unsupported target for indexed assignment ("Union[ndarray[Any, Any],
        # datetime64, timedelta64]")
        result[axis_mask] = iNaT
    else:
        if mask.any():
            return np.int64(iNaT).view(orig_values.dtype)
    return result


@bottleneck_switch()
@_datetimelike_compat
def nanmean(
    values: np.ndarray,
    *,
    axis: AxisInt | None = None,
    skipna: bool = True,
    mask: npt.NDArray[np.bool_] | None = None,
) -> float:
    """
    Compute the mean of the element along an axis ignoring NaNs

    Parameters
    ----------
    values : ndarray
    axis : int, optional
    skipna : bool, default True

Review comment (suggestion):
You can remove the comment here since you removed the `ignore`

Code after:
    """
    dtype = values.dtype
    values, mask = _get_values(values, skipna, fill_value=0, mask=mask)
    dtype_sum = _get_dtype_max(dtype)
    if dtype.kind == "f":
        dtype_sum = dtype
    elif dtype.kind == "m":
        dtype_sum = np.dtype(np.float64)

    the_sum = values.sum(axis, dtype=dtype_sum)
    the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count)

    return the_sum


def _mask_datetimelike_result(
    result: np.ndarray | np.datetime64 | np.timedelta64,
    axis: AxisInt | None,
    mask: npt.NDArray[np.bool_],
    orig_values: np.ndarray,
) -> np.ndarray | np.datetime64 | np.timedelta64 | NaTType:
    if isinstance(result, np.ndarray):
        # we need to apply the mask
        result = result.astype("i8").view(orig_values.dtype)
        axis_mask = mask.any(axis=axis)
        result[axis_mask] = iNaT
    else:
        if mask.any():
            return np.int64(iNaT).view(orig_values.dtype)
    return result


@bottleneck_switch()
@_datetimelike_compat
def nanmean(
    values: np.ndarray,
    *,
    axis: AxisInt | None = None,
    skipna: bool = True,
    mask: npt.NDArray[np.bool_] | None = None,
) -> float:
    """
    Compute the mean of the element along an axis ignoring NaNs

    Parameters
    ----------
    values : ndarray
    axis : int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known
Repository: pandas-dev/pandas
File: pandas/core/arrays/numpy_.py (Python)

Code before:
            limit=limit,
            limit_direction=limit_direction,
            limit_area=limit_area,
            **kwargs,
        )
        if not copy:
            return self
        return type(self)._simple_new(out_data, dtype=self.dtype)

    def take(
        self,
        indices: TakeIndexer,
        *,
        allow_fill: bool = False,
        fill_value: Any = None,
        axis: AxisInt = 0,
    ) -> Self:
        """
        Take entries from this array at each index in a list of indices,
        producing an array containing only those entries.
        """
        result = super().take(
            indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
        )
        # See GH#62448.
        if self.dtype.numpy_dtype in [
            np.uint8,
            np.uint16,
            np.uint32,
            np.uint64,
            np.int8,
            np.int16,
            np.int32,
            np.int64,
        ]:
            return type(self)(result, copy=False)

        return result

    # ------------------------------------------------------------------------
    # Reductions

    def any(
        self,
        *,
        axis: AxisInt | None = None,
        out=None,
        keepdims: bool = False,
        skipna: bool = True,
    ):
        nv.validate_any((), {"out": out, "keepdims": keepdims})

Review comment (suggestion):
do this check before the super call, and just check `self.dtype.kind in "iub"`

Code after:
        # NB: we return type(self) even if copy=False
        if not self.dtype._is_numeric:
            raise TypeError(f"Cannot interpolate with {self.dtype} dtype")

        if not copy:
            out_data = self._ndarray
        else:
            out_data = self._ndarray.copy()

        # TODO: assert we have floating dtype?
        missing.interpolate_2d_inplace(
            out_data,
            method=method,
            axis=axis,
            index=index,
            limit=limit,
            limit_direction=limit_direction,
            limit_area=limit_area,
            **kwargs,
        )
        if not copy:
            return self
        return type(self)._simple_new(out_data, dtype=self.dtype)

    def take(
        self,
        indices: TakeIndexer,
        *,
        allow_fill: bool = False,
        fill_value: Any = None,
        axis: AxisInt = 0,
    ) -> Self:
        """
        Take entries from this array at each index in a list of indices,
        producing an array containing only those entries.
        """
        result = super().take(
            indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
        )
        # See GH#62448.
        if self.dtype.kind in "iub":
            return type(self)(result._ndarray, copy=False)

        return result

    # ------------------------------------------------------------------------
    # Reductions

    def any(
        self,
        *,
Repository: pandas-dev/pandas
File: pandas/core/arrays/numpy_.py (Python)

Code before:
            limit_direction=limit_direction,
            limit_area=limit_area,
            **kwargs,
        )
        if not copy:
            return self
        return type(self)._simple_new(out_data, dtype=self.dtype)

    def take(
        self,
        indices: TakeIndexer,
        *,
        allow_fill: bool = False,
        fill_value: Any = None,
        axis: AxisInt = 0,
    ) -> Self:
        """
        Take entries from this array at each index in a list of indices,
        producing an array containing only those entries.
        """
        result = super().take(
            indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
        )
        # See GH#62448.
        if self.dtype.kind in "iub":
            return type(self)(result, copy=False)

        return result

    # ------------------------------------------------------------------------
    # Reductions

    def any(
        self,
        *,
        axis: AxisInt | None = None,
        out=None,
        keepdims: bool = False,
        skipna: bool = True,
    ):
        nv.validate_any((), {"out": out, "keepdims": keepdims})
        result = nanops.nanany(self._ndarray, axis=axis, skipna=skipna)
        return self._wrap_reduction_result(axis, result)

    def all(
        self,
        *,
        axis: AxisInt | None = None,
        out=None,
        keepdims: bool = False,
        skipna: bool = True,

Review comment (suggestion):
i think this should use `result._ndarray` instead of `result`?  it doesn't actually make a difference, but feels wonky to be passing a mal-formed object here.

Code after:
            raise TypeError(f"Cannot interpolate with {self.dtype} dtype")

        if not copy:
            out_data = self._ndarray
        else:
            out_data = self._ndarray.copy()

        # TODO: assert we have floating dtype?
        missing.interpolate_2d_inplace(
            out_data,
            method=method,
            axis=axis,
            index=index,
            limit=limit,
            limit_direction=limit_direction,
            limit_area=limit_area,
            **kwargs,
        )
        if not copy:
            return self
        return type(self)._simple_new(out_data, dtype=self.dtype)

    def take(
        self,
        indices: TakeIndexer,
        *,
        allow_fill: bool = False,
        fill_value: Any = None,
        axis: AxisInt = 0,
    ) -> Self:
        """
        Take entries from this array at each index in a list of indices,
        producing an array containing only those entries.
        """
        result = super().take(
            indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
        )
        # See GH#62448.
        if self.dtype.kind in "iub":
            return type(self)(result._ndarray, copy=False)

        return result

    # ------------------------------------------------------------------------
    # Reductions

    def any(
        self,
        *,
        axis: AxisInt | None = None,
        out=None,