From e12fd56401d5e14372fbeed8b4a17e98e6507905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Mon, 1 Aug 2022 14:22:20 -0400 Subject: [PATCH] TYP: pandas.core.generic from pandas-stubs (#47912) * TYP: pandas.core.generic from pandas-stubs * black * overload for to_latex * bool_t * whatsnew --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/generic.py | 197 +++++++++++++++++++++++---------- pandas/io/formats/excel.py | 4 +- pandas/io/sql.py | 3 +- 4 files changed, 144 insertions(+), 61 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index d138ebb9c02a3..3f2d37e6b52a9 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -774,6 +774,7 @@ Other Deprecations - Deprecated :meth:`Series.rank` returning an empty result when the dtype is non-numeric and ``numeric_only=True`` is provided; this will raise a ``TypeError`` in a future version (:issue:`47500`) - Deprecated argument ``errors`` for :meth:`Series.mask`, :meth:`Series.where`, :meth:`DataFrame.mask`, and :meth:`DataFrame.where` as ``errors`` had no effect on this methods (:issue:`47728`) - Deprecated arguments ``*args`` and ``**kwargs`` in :class:`Rolling`, :class:`Expanding`, and :class:`ExponentialMovingWindow` ops. (:issue:`47836`) +- Deprecated unused arguments ``encoding`` and ``verbose`` in :meth:`Series.to_excel` and :meth:`DataFrame.to_excel` (:issue:`47912`) .. --------------------------------------------------------------------------- .. _whatsnew_150.performance: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a033b7a3f83d7..7352ad2a4985d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -17,6 +17,7 @@ Hashable, Literal, Mapping, + NoReturn, Sequence, Type, cast, @@ -41,11 +42,14 @@ AnyArrayLike, ArrayLike, Axis, + ColspaceArgType, CompressionOptions, Dtype, DtypeArg, DtypeObj, FilePath, + FloatFormatType, + FormattersType, IgnoreRaise, IndexKeyFunc, IndexLabel, @@ -183,6 +187,9 @@ from pandas.core.resample import Resampler from pandas.core.series import Series + from pandas.io.pytables import HDFStore + + # goal is to be able to define the docs close to function, while still being # able to share _shared_docs = {**_shared_docs} @@ -765,7 +772,9 @@ def _set_axis(self, axis: int, labels: AnyArrayLike | Sequence) -> None: self._clear_item_cache() @final - def swapaxes(self: NDFrameT, axis1, axis2, copy=True) -> NDFrameT: + def swapaxes( + self: NDFrameT, axis1: Axis, axis2: Axis, copy: bool_t = True + ) -> NDFrameT: """ Interchange axes and swap values axes appropriately. @@ -795,7 +804,7 @@ def swapaxes(self: NDFrameT, axis1, axis2, copy=True) -> NDFrameT: @final @doc(klass=_shared_doc_kwargs["klass"]) - def droplevel(self: NDFrameT, level, axis=0) -> NDFrameT: + def droplevel(self: NDFrameT, level: IndexLabel, axis: Axis = 0) -> NDFrameT: """ Return {klass} with requested index / column level(s) removed. @@ -1458,7 +1467,7 @@ def __invert__(self: NDFrameT) -> NDFrameT: return self._constructor(new_data).__finalize__(self, method="__invert__") @final - def __nonzero__(self): + def __nonzero__(self) -> NoReturn: raise ValueError( f"The truth value of a {type(self).__name__} is ambiguous. " "Use a.empty, a.bool(), a.item(), a.any() or a.all()." @@ -1998,7 +2007,7 @@ def empty(self) -> bool_t: # This is also set in IndexOpsMixin # GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented - __array_priority__ = 1000 + __array_priority__: int = 1000 def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray: return np.asarray(self._values, dtype=dtype) @@ -2134,6 +2143,8 @@ def _repr_data_resource_(self): # I/O Methods @final + @deprecate_kwarg(old_arg_name="verbose", new_arg_name=None) + @deprecate_kwarg(old_arg_name="encoding", new_arg_name=None) @doc( klass="object", storage_options=_shared_docs["storage_options"], @@ -2145,18 +2156,18 @@ def to_excel( sheet_name: str = "Sheet1", na_rep: str = "", float_format: str | None = None, - columns=None, - header=True, - index=True, - index_label=None, - startrow=0, - startcol=0, - engine=None, - merge_cells=True, - encoding=None, - inf_rep="inf", - verbose=True, - freeze_panes=None, + columns: Sequence[Hashable] | None = None, + header: Sequence[Hashable] | bool_t = True, + index: bool_t = True, + index_label: IndexLabel = None, + startrow: int = 0, + startcol: int = 0, + engine: str | None = None, + merge_cells: bool_t = True, + encoding: lib.NoDefault = lib.no_default, + inf_rep: str = "inf", + verbose: lib.NoDefault = lib.no_default, + freeze_panes: tuple[int, int] | None = None, storage_options: StorageOptions = None, ) -> None: """ @@ -2214,11 +2225,21 @@ def to_excel( encoding : str, optional Encoding of the resulting excel file. Only necessary for xlwt, other writers support unicode natively. + + .. deprecated:: 1.5.0 + + This keyword was not used. + inf_rep : str, default 'inf' Representation for infinity (there is no native representation for infinity in Excel). verbose : bool, default True Display more information in the error logs. + + .. deprecated:: 1.5.0 + + This keyword was not used. + freeze_panes : tuple of int (length 2), optional Specifies the one-based bottommost row and rightmost column that is to be frozen. @@ -2587,7 +2608,7 @@ def to_json( @final def to_hdf( self, - path_or_buf, + path_or_buf: FilePath | HDFStore, key: str, mode: str = "a", complevel: int | None = None, @@ -2740,13 +2761,13 @@ def to_sql( self, name: str, con, - schema=None, + schema: str | None = None, if_exists: str = "fail", index: bool_t = True, - index_label=None, - chunksize=None, + index_label: IndexLabel = None, + chunksize: int | None = None, dtype: DtypeArg | None = None, - method=None, + method: str | None = None, ) -> int | None: """ Write records stored in a DataFrame to a SQL database. @@ -3147,33 +3168,89 @@ class (index) object 'bird' 'bird' 'mammal' 'mammal' else: return xarray.Dataset.from_dataframe(self) + @overload + def to_latex( + self, + buf: None = ..., + columns: Sequence[Hashable] | None = ..., + col_space: ColspaceArgType | None = ..., + header: bool_t | Sequence[str] = ..., + index: bool_t = ..., + na_rep: str = ..., + formatters: FormattersType | None = ..., + float_format: FloatFormatType | None = ..., + sparsify: bool_t | None = ..., + index_names: bool_t = ..., + bold_rows: bool_t = ..., + column_format: str | None = ..., + longtable: bool_t | None = ..., + escape: bool_t | None = ..., + encoding: str | None = ..., + decimal: str = ..., + multicolumn: bool_t | None = ..., + multicolumn_format: str | None = ..., + multirow: bool_t | None = ..., + caption: str | None = ..., + label: str | None = ..., + position: str | None = ..., + ) -> str: + ... + + @overload + def to_latex( + self, + buf: FilePath | WriteBuffer[str], + columns: Sequence[Hashable] | None = ..., + col_space: ColspaceArgType | None = ..., + header: bool_t | Sequence[str] = ..., + index: bool_t = ..., + na_rep: str = ..., + formatters: FormattersType | None = ..., + float_format: FloatFormatType | None = ..., + sparsify: bool_t | None = ..., + index_names: bool_t = ..., + bold_rows: bool_t = ..., + column_format: str | None = ..., + longtable: bool_t | None = ..., + escape: bool_t | None = ..., + encoding: str | None = ..., + decimal: str = ..., + multicolumn: bool_t | None = ..., + multicolumn_format: str | None = ..., + multirow: bool_t | None = ..., + caption: str | None = ..., + label: str | None = ..., + position: str | None = ..., + ) -> None: + ... + @final @doc(returns=fmt.return_docstring) def to_latex( self, - buf=None, - columns=None, - col_space=None, - header=True, - index=True, - na_rep="NaN", - formatters=None, - float_format=None, - sparsify=None, - index_names=True, - bold_rows=False, - column_format=None, - longtable=None, - escape=None, - encoding=None, - decimal=".", - multicolumn=None, - multicolumn_format=None, - multirow=None, - caption=None, - label=None, - position=None, - ): + buf: FilePath | WriteBuffer[str] | None = None, + columns: Sequence[Hashable] | None = None, + col_space: ColspaceArgType | None = None, + header: bool_t | Sequence[str] = True, + index: bool_t = True, + na_rep: str = "NaN", + formatters: FormattersType | None = None, + float_format: FloatFormatType | None = None, + sparsify: bool_t | None = None, + index_names: bool_t = True, + bold_rows: bool_t = False, + column_format: str | None = None, + longtable: bool_t | None = None, + escape: bool_t | None = None, + encoding: str | None = None, + decimal: str = ".", + multicolumn: bool_t | None = None, + multicolumn_format: str | None = None, + multirow: bool_t | None = None, + caption: str | None = None, + label: str | None = None, + position: str | None = None, + ) -> str | None: r""" Render object to a LaTeX tabular, longtable, or nested table. @@ -3771,7 +3848,11 @@ def _take_with_is_copy(self: NDFrameT, indices, axis=0) -> NDFrameT: @final def xs( - self: NDFrameT, key, axis=0, level=None, drop_level: bool_t = True + self: NDFrameT, + key: IndexLabel, + axis: Axis = 0, + level: IndexLabel = None, + drop_level: bool_t = True, ) -> NDFrameT: """ Return cross-section from the Series/DataFrame. @@ -6822,7 +6903,7 @@ def replace( *, inplace: Literal[False] = ..., limit: int | None = ..., - regex=..., + regex: bool_t = ..., method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ..., ) -> NDFrameT: ... @@ -6835,7 +6916,7 @@ def replace( *, inplace: Literal[True], limit: int | None = ..., - regex=..., + regex: bool_t = ..., method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ..., ) -> None: ... @@ -6848,7 +6929,7 @@ def replace( *, inplace: bool_t = ..., limit: int | None = ..., - regex=..., + regex: bool_t = ..., method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ..., ) -> NDFrameT | None: ... @@ -6868,7 +6949,7 @@ def replace( value=lib.no_default, inplace: bool_t = False, limit: int | None = None, - regex=False, + regex: bool_t = False, method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = lib.no_default, ) -> NDFrameT | None: if not ( @@ -9521,7 +9602,7 @@ def where( axis=..., level=..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT: ... @@ -9535,7 +9616,7 @@ def where( axis=..., level=..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool_t | lib.NoDefault = ..., ) -> None: ... @@ -9549,7 +9630,7 @@ def where( axis=..., level=..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT | None: ... @@ -9572,7 +9653,7 @@ def where( axis=None, level=None, errors: IgnoreRaise | lib.NoDefault = "raise", - try_cast=lib.no_default, + try_cast: bool_t | lib.NoDefault = lib.no_default, ) -> NDFrameT | None: """ Replace values where the condition is {cond_rev}. @@ -9742,7 +9823,7 @@ def mask( axis=..., level=..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT: ... @@ -9756,7 +9837,7 @@ def mask( axis=..., level=..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool_t | lib.NoDefault = ..., ) -> None: ... @@ -9770,7 +9851,7 @@ def mask( axis=..., level=..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT | None: ... @@ -9794,7 +9875,7 @@ def mask( axis=None, level=None, errors: IgnoreRaise | lib.NoDefault = "raise", - try_cast=lib.no_default, + try_cast: bool_t | lib.NoDefault = lib.no_default, ) -> NDFrameT | None: inplace = validate_bool_kwarg(inplace, "inplace") @@ -10456,7 +10537,7 @@ def describe( percentiles=None, include=None, exclude=None, - datetime_is_numeric=False, + datetime_is_numeric: bool_t = False, ) -> NDFrameT: """ Generate descriptive statistics. diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 4279a9707e692..d8ef709dc89ea 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -856,8 +856,8 @@ def write( sheet_name: str = "Sheet1", startrow: int = 0, startcol: int = 0, - freeze_panes=None, - engine=None, + freeze_panes: tuple[int, int] | None = None, + engine: str | None = None, storage_options: StorageOptions = None, ) -> None: """ diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 086a60774ac4e..71fecba4340ac 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -28,6 +28,7 @@ from pandas._typing import ( DateTimeErrorChoices, DtypeArg, + IndexLabel, ) from pandas.compat._optional import import_optional_dependency from pandas.errors import ( @@ -603,7 +604,7 @@ def to_sql( schema: str | None = None, if_exists: str = "fail", index: bool = True, - index_label=None, + index_label: IndexLabel = None, chunksize: int | None = None, dtype: DtypeArg | None = None, method: str | None = None,