diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 11101e5a832a9..a4dfb085c766f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -80,6 +80,7 @@ TimestampConvertibleTypes, ValueKeyFunc, WriteBuffer, + WriteExcelBuffer, npt, ) from pandas.compat._optional import import_optional_dependency @@ -188,12 +189,14 @@ if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset - from pandas.core.frame import DataFrame + from pandas import ( + DataFrame, + ExcelWriter, + HDFStore, + Series, + ) from pandas.core.indexers.objects import BaseIndexer 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 @@ -2091,7 +2094,7 @@ def _repr_data_resource_(self): ) def to_excel( self, - excel_writer, + excel_writer: FilePath | WriteExcelBuffer | ExcelWriter, sheet_name: str = "Sheet1", na_rep: str = "", float_format: str | None = None, diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 806496d5a7108..98f7b64d2cda0 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -52,10 +52,14 @@ if TYPE_CHECKING: from pandas._typing import ( + FilePath, IndexLabel, StorageOptions, + WriteExcelBuffer, ) + from pandas import ExcelWriter + class ExcelCell: __fields__ = ("row", "col", "val", "style", "mergestart", "mergeend") @@ -890,7 +894,7 @@ def get_formatted_cells(self) -> Iterable[ExcelCell]: @doc(storage_options=_shared_docs["storage_options"]) def write( self, - writer, + writer: FilePath | WriteExcelBuffer | ExcelWriter, sheet_name: str = "Sheet1", startrow: int = 0, startcol: int = 0, diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index d989a9d199078..573e2b72e81f9 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -70,8 +70,11 @@ Scalar, StorageOptions, WriteBuffer, + WriteExcelBuffer, ) + from pandas import ExcelWriter + try: import matplotlib as mpl import matplotlib.pyplot as plt @@ -494,7 +497,7 @@ def set_tooltips( ) def to_excel( self, - excel_writer, + excel_writer: FilePath | WriteExcelBuffer | ExcelWriter, sheet_name: str = "Sheet1", na_rep: str = "", float_format: str | None = None, @@ -1327,7 +1330,7 @@ def to_string( self, buf: FilePath | WriteBuffer[str], *, - encoding=..., + encoding: str | None = ..., sparse_index: bool | None = ..., sparse_columns: bool | None = ..., max_rows: int | None = ..., @@ -1341,7 +1344,7 @@ def to_string( self, buf: None = ..., *, - encoding=..., + encoding: str | None = ..., sparse_index: bool | None = ..., sparse_columns: bool | None = ..., max_rows: int | None = ..., @@ -1355,7 +1358,7 @@ def to_string( self, buf: FilePath | WriteBuffer[str] | None = None, *, - encoding=None, + encoding: str | None = None, sparse_index: bool | None = None, sparse_columns: bool | None = None, max_rows: int | None = None, @@ -3385,8 +3388,11 @@ def highlight_quantile( @classmethod def from_custom_template( - cls, searchpath, html_table: str | None = None, html_style: str | None = None - ): + cls, + searchpath: Sequence[str], + html_table: str | None = None, + html_style: str | None = None, + ) -> type[Styler]: """ Factory function for creating a subclass of ``Styler``. diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 28138249ec30b..48ec7dd24d067 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -395,7 +395,7 @@ def read_json( orient: str | None = ..., typ: Literal["frame"] = ..., dtype: DtypeArg | None = ..., - convert_axes=..., + convert_axes: bool | None = ..., convert_dates: bool | list[str] = ..., keep_default_dates: bool = ..., precise_float: bool = ..., @@ -420,7 +420,7 @@ def read_json( orient: str | None = ..., typ: Literal["series"], dtype: DtypeArg | None = ..., - convert_axes=..., + convert_axes: bool | None = ..., convert_dates: bool | list[str] = ..., keep_default_dates: bool = ..., precise_float: bool = ..., @@ -445,7 +445,7 @@ def read_json( orient: str | None = ..., typ: Literal["series"], dtype: DtypeArg | None = ..., - convert_axes=..., + convert_axes: bool | None = ..., convert_dates: bool | list[str] = ..., keep_default_dates: bool = ..., precise_float: bool = ..., @@ -470,7 +470,7 @@ def read_json( orient: str | None = ..., typ: Literal["frame"] = ..., dtype: DtypeArg | None = ..., - convert_axes=..., + convert_axes: bool | None = ..., convert_dates: bool | list[str] = ..., keep_default_dates: bool = ..., precise_float: bool = ..., @@ -498,7 +498,7 @@ def read_json( orient: str | None = None, typ: Literal["frame", "series"] = "frame", dtype: DtypeArg | None = None, - convert_axes=None, + convert_axes: bool | None = None, convert_dates: bool | list[str] = True, keep_default_dates: bool = True, precise_float: bool = False, @@ -814,7 +814,7 @@ def __init__( orient, typ: FrameSeriesStrT, dtype, - convert_axes, + convert_axes: bool | None, convert_dates, keep_default_dates: bool, precise_float: bool, diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index af0d34745a317..e06cc57a9ea75 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -16,8 +16,10 @@ Callable, Hashable, Literal, + Mapping, NamedTuple, Sequence, + TypedDict, overload, ) import warnings @@ -72,6 +74,7 @@ CSVEngine, DtypeArg, FilePath, + HashableT, IndexLabel, ReadCsvBuffer, StorageOptions, @@ -437,7 +440,15 @@ ) -_c_parser_defaults = { +class _C_Parser_Defaults(TypedDict): + delim_whitespace: Literal[False] + na_filter: Literal[True] + low_memory: Literal[True] + memory_map: Literal[False] + float_precision: None + + +_c_parser_defaults: _C_Parser_Defaults = { "delim_whitespace": False, "na_filter": True, "low_memory": True, @@ -445,8 +456,14 @@ "float_precision": None, } -_fwf_defaults = {"colspecs": "infer", "infer_nrows": 100, "widths": None} +class _Fwf_Defaults(TypedDict): + colspecs: Literal["infer"] + infer_nrows: Literal[100] + widths: None + + +_fwf_defaults: _Fwf_Defaults = {"colspecs": "infer", "infer_nrows": 100, "widths": None} _c_unsupported = {"skipfooter"} _python_unsupported = {"low_memory", "float_precision"} _pyarrow_unsupported = { @@ -602,17 +619,17 @@ def read_csv( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -620,7 +637,7 @@ def read_csv( parse_dates: bool | Sequence[Hashable] | None = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -640,7 +657,7 @@ def read_csv( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: Literal["high", "legacy"] | None = ..., storage_options: StorageOptions = ..., @@ -659,17 +676,17 @@ def read_csv( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -677,7 +694,7 @@ def read_csv( parse_dates: bool | Sequence[Hashable] | None = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -697,7 +714,7 @@ def read_csv( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: Literal["high", "legacy"] | None = ..., storage_options: StorageOptions = ..., @@ -716,17 +733,17 @@ def read_csv( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -734,7 +751,7 @@ def read_csv( parse_dates: bool | Sequence[Hashable] | None = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -754,7 +771,7 @@ def read_csv( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: Literal["high", "legacy"] | None = ..., storage_options: StorageOptions = ..., @@ -773,17 +790,17 @@ def read_csv( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -791,7 +808,7 @@ def read_csv( parse_dates: bool | Sequence[Hashable] | None = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -811,7 +828,7 @@ def read_csv( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: Literal["high", "legacy"] | None = ..., storage_options: StorageOptions = ..., @@ -839,19 +856,19 @@ def read_csv( header: int | Sequence[int] | None | Literal["infer"] = "infer", names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default, index_col: IndexLabel | Literal[False] | None = None, - usecols=None, + usecols: list[HashableT] | Callable[[Hashable], bool] | None = None, # General Parsing Configuration dtype: DtypeArg | None = None, engine: CSVEngine | None = None, - converters=None, - true_values=None, - false_values=None, + converters: Mapping[Hashable, Callable] | None = None, + true_values: list | None = None, + false_values: list | None = None, skipinitialspace: bool = False, - skiprows=None, + skiprows: list[int] | int | Callable[[Hashable], bool] | None = None, skipfooter: int = 0, nrows: int | None = None, # NA and Missing Data Handling - na_values=None, + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = None, keep_default_na: bool = True, na_filter: bool = True, verbose: bool = False, @@ -860,7 +877,7 @@ def read_csv( parse_dates: bool | Sequence[Hashable] | None = None, infer_datetime_format: bool | lib.NoDefault = lib.no_default, keep_date_col: bool = False, - date_parser=lib.no_default, + date_parser: Callable | lib.NoDefault = lib.no_default, date_format: str | None = None, dayfirst: bool = False, cache_dates: bool = True, @@ -884,7 +901,7 @@ def read_csv( on_bad_lines: str = "error", # Internal delim_whitespace: bool = False, - low_memory=_c_parser_defaults["low_memory"], + low_memory: bool = _c_parser_defaults["low_memory"], memory_map: bool = False, float_precision: Literal["high", "legacy"] | None = None, storage_options: StorageOptions = None, @@ -931,17 +948,17 @@ def read_table( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -949,7 +966,7 @@ def read_table( parse_dates: bool | Sequence[Hashable] = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -969,7 +986,7 @@ def read_table( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: str | None = ..., storage_options: StorageOptions = ..., @@ -988,17 +1005,17 @@ def read_table( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -1006,7 +1023,7 @@ def read_table( parse_dates: bool | Sequence[Hashable] = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -1026,7 +1043,7 @@ def read_table( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: str | None = ..., storage_options: StorageOptions = ..., @@ -1045,17 +1062,17 @@ def read_table( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -1063,7 +1080,7 @@ def read_table( parse_dates: bool | Sequence[Hashable] = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -1083,7 +1100,7 @@ def read_table( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: str | None = ..., storage_options: StorageOptions = ..., @@ -1102,17 +1119,17 @@ def read_table( header: int | Sequence[int] | None | Literal["infer"] = ..., names: Sequence[Hashable] | None | lib.NoDefault = ..., index_col: IndexLabel | Literal[False] | None = ..., - usecols=..., + usecols: list[HashableT] | Callable[[Hashable], bool] | None = ..., dtype: DtypeArg | None = ..., engine: CSVEngine | None = ..., - converters=..., - true_values=..., - false_values=..., + converters: Mapping[Hashable, Callable] | None = ..., + true_values: list | None = ..., + false_values: list | None = ..., skipinitialspace: bool = ..., - skiprows=..., + skiprows: list[int] | int | Callable[[Hashable], bool] | None = ..., skipfooter: int = ..., nrows: int | None = ..., - na_values=..., + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = ..., keep_default_na: bool = ..., na_filter: bool = ..., verbose: bool = ..., @@ -1120,7 +1137,7 @@ def read_table( parse_dates: bool | Sequence[Hashable] = ..., infer_datetime_format: bool | lib.NoDefault = ..., keep_date_col: bool = ..., - date_parser=..., + date_parser: Callable | lib.NoDefault = ..., date_format: str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., @@ -1140,7 +1157,7 @@ def read_table( dialect: str | csv.Dialect | None = ..., on_bad_lines=..., delim_whitespace: bool = ..., - low_memory=..., + low_memory: bool = ..., memory_map: bool = ..., float_precision: str | None = ..., storage_options: StorageOptions = ..., @@ -1168,19 +1185,19 @@ def read_table( header: int | Sequence[int] | None | Literal["infer"] = "infer", names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default, index_col: IndexLabel | Literal[False] | None = None, - usecols=None, + usecols: list[HashableT] | Callable[[Hashable], bool] | None = None, # General Parsing Configuration dtype: DtypeArg | None = None, engine: CSVEngine | None = None, - converters=None, - true_values=None, - false_values=None, + converters: Mapping[Hashable, Callable] | None = None, + true_values: list | None = None, + false_values: list | None = None, skipinitialspace: bool = False, - skiprows=None, + skiprows: list[int] | int | Callable[[Hashable], bool] | None = None, skipfooter: int = 0, nrows: int | None = None, # NA and Missing Data Handling - na_values=None, + na_values: Sequence[str] | Mapping[str, Sequence[str]] | None = None, keep_default_na: bool = True, na_filter: bool = True, verbose: bool = False, @@ -1189,7 +1206,7 @@ def read_table( parse_dates: bool | Sequence[Hashable] = False, infer_datetime_format: bool | lib.NoDefault = lib.no_default, keep_date_col: bool = False, - date_parser=lib.no_default, + date_parser: Callable | lib.NoDefault = lib.no_default, date_format: str | None = None, dayfirst: bool = False, cache_dates: bool = True, @@ -1213,7 +1230,7 @@ def read_table( on_bad_lines: str = "error", # Internal delim_whitespace: bool = False, - low_memory=_c_parser_defaults["low_memory"], + low_memory: bool = _c_parser_defaults["low_memory"], memory_map: bool = False, float_precision: str | None = None, storage_options: StorageOptions = None, @@ -1563,7 +1580,7 @@ def _clean_options( if "python" in engine: for arg in _python_unsupported: - if fallback_reason and result[arg] != _c_parser_defaults[arg]: + if fallback_reason and result[arg] != _c_parser_defaults.get(arg): raise ValueError( "Falling back to the 'python' engine because " f"{fallback_reason}, but this causes {repr(arg)} to be " diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0fbd587c1971e..119a71ddc6943 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -3,6 +3,9 @@ import importlib from typing import ( TYPE_CHECKING, + Callable, + Hashable, + Literal, Sequence, ) @@ -28,6 +31,7 @@ import types from matplotlib.axes import Axes + import numpy as np from pandas._typing import IndexLabel @@ -1035,7 +1039,7 @@ def __call__(self, *args, **kwargs): ) @Substitution(kind="line") @Appender(_bar_or_line_doc) - def line(self, x=None, y=None, **kwargs) -> PlotAccessor: + def line(self, x: Hashable = None, y: Hashable = None, **kwargs) -> PlotAccessor: """ Plot Series or DataFrame as lines. @@ -1123,7 +1127,7 @@ def line(self, x=None, y=None, **kwargs) -> PlotAccessor: @Substitution(kind="bar") @Appender(_bar_or_line_doc) def bar( # pylint: disable=disallowed-name - self, x=None, y=None, **kwargs + self, x: Hashable = None, y: Hashable = None, **kwargs ) -> PlotAccessor: """ Vertical bar plot. @@ -1210,7 +1214,7 @@ def bar( # pylint: disable=disallowed-name ) @Substitution(kind="bar") @Appender(_bar_or_line_doc) - def barh(self, x=None, y=None, **kwargs) -> PlotAccessor: + def barh(self, x: Hashable = None, y: Hashable = None, **kwargs) -> PlotAccessor: """ Make a horizontal bar plot. @@ -1222,7 +1226,7 @@ def barh(self, x=None, y=None, **kwargs) -> PlotAccessor: """ return self(kind="barh", x=x, y=y, **kwargs) - def box(self, by=None, **kwargs) -> PlotAccessor: + def box(self, by: IndexLabel = None, **kwargs) -> PlotAccessor: r""" Make a box plot of the DataFrame columns. @@ -1289,7 +1293,7 @@ def box(self, by=None, **kwargs) -> PlotAccessor: """ return self(kind="box", by=by, **kwargs) - def hist(self, by=None, bins: int = 10, **kwargs) -> PlotAccessor: + def hist(self, by: IndexLabel = None, bins: int = 10, **kwargs) -> PlotAccessor: """ Draw one histogram of the DataFrame's columns. @@ -1351,7 +1355,12 @@ def hist(self, by=None, bins: int = 10, **kwargs) -> PlotAccessor: """ return self(kind="hist", by=by, bins=bins, **kwargs) - def kde(self, bw_method=None, ind=None, **kwargs) -> PlotAccessor: + def kde( + self, + bw_method: Literal["scott", "silverman"] | float | Callable | None = None, + ind: np.ndarray | int | None = None, + **kwargs, + ) -> PlotAccessor: """ Generate Kernel Density Estimate plot using Gaussian kernels. @@ -1461,7 +1470,9 @@ def kde(self, bw_method=None, ind=None, **kwargs) -> PlotAccessor: density = kde - def area(self, x=None, y=None, stacked: bool = True, **kwargs) -> PlotAccessor: + def area( + self, x: Hashable = None, y: Hashable = None, stacked: bool = True, **kwargs + ) -> PlotAccessor: """ Draw a stacked area plot. @@ -1589,7 +1600,14 @@ def pie(self, **kwargs) -> PlotAccessor: raise ValueError("pie requires either y column or 'subplots=True'") return self(kind="pie", **kwargs) - def scatter(self, x, y, s=None, c=None, **kwargs) -> PlotAccessor: + def scatter( + self, + x: Hashable, + y: Hashable, + s: Hashable | Sequence[Hashable] = None, + c: Hashable | Sequence[Hashable] = None, + **kwargs, + ) -> PlotAccessor: """ Create a scatter plot with varying marker point size and color. @@ -1675,7 +1693,13 @@ def scatter(self, x, y, s=None, c=None, **kwargs) -> PlotAccessor: return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs) def hexbin( - self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs + self, + x: Hashable, + y: Hashable, + C: Hashable = None, + reduce_C_function: Callable | None = None, + gridsize: int | tuple[int, int] | None = None, + **kwargs, ) -> PlotAccessor: """ Generate a hexagonal binning plot. diff --git a/pandas/plotting/_misc.py b/pandas/plotting/_misc.py index 903dce9f040c3..cbfdbc10ad71c 100644 --- a/pandas/plotting/_misc.py +++ b/pandas/plotting/_misc.py @@ -3,14 +3,18 @@ from contextlib import contextmanager from typing import ( TYPE_CHECKING, + Any, Generator, + Mapping, ) from pandas.plotting._core import _get_plot_backend if TYPE_CHECKING: from matplotlib.axes import Axes + from matplotlib.colors import Colormap from matplotlib.figure import Figure + from matplotlib.table import Table import numpy as np from pandas import ( @@ -19,7 +23,7 @@ ) -def table(ax, data, **kwargs): +def table(ax: Axes, data: DataFrame | Series, **kwargs) -> Table: """ Helper function to convert DataFrame and Series to matplotlib.table. @@ -93,8 +97,8 @@ def scatter_matrix( grid: bool = False, diagonal: str = "hist", marker: str = ".", - density_kwds=None, - hist_kwds=None, + density_kwds: Mapping[str, Any] | None = None, + hist_kwds: Mapping[str, Any] | None = None, range_padding: float = 0.05, **kwargs, ) -> np.ndarray: @@ -177,7 +181,7 @@ def radviz( class_column: str, ax: Axes | None = None, color: list[str] | tuple[str, ...] | None = None, - colormap=None, + colormap: Colormap | str | None = None, **kwds, ) -> Axes: """ @@ -265,7 +269,7 @@ def andrews_curves( ax: Axes | None = None, samples: int = 200, color: list[str] | tuple[str, ...] | None = None, - colormap=None, + colormap: Colormap | str | None = None, **kwargs, ) -> Axes: """ @@ -396,9 +400,9 @@ def parallel_coordinates( color: list[str] | tuple[str, ...] | None = None, use_columns: bool = False, xticks: list | tuple | None = None, - colormap=None, + colormap: Colormap | str | None = None, axvlines: bool = True, - axvlines_kwds=None, + axvlines_kwds: Mapping[str, Any] | None = None, sort_labels: bool = False, **kwargs, ) -> Axes: diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 9bd88c4c905c3..23e8de69a21ed 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -45,6 +45,13 @@ if TYPE_CHECKING: from pandas._typing import npt + + from pandas import ( + DatetimeIndex, + Series, + TimedeltaIndex, + ) + from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin # --------------------------------------------------------------------- # Offset names ("time rules") and related functions @@ -105,7 +112,9 @@ def get_period_alias(offset_str: str) -> str | None: # Period codes -def infer_freq(index) -> str | None: +def infer_freq( + index: DatetimeIndex | TimedeltaIndex | Series | DatetimeLikeArrayMixin, +) -> str | None: """ Infer the most likely frequency given the input index. @@ -169,7 +178,10 @@ def infer_freq(index) -> str | None: raise TypeError( f"cannot infer freq from a non-convertible index of dtype {index.dtype}" ) - index = index._values + # error: Incompatible types in assignment (expression has type + # "Union[ExtensionArray, ndarray[Any, Any]]", variable has type + # "Union[DatetimeIndex, TimedeltaIndex, Series, DatetimeLikeArrayMixin]") + index = index._values # type: ignore[assignment] if not isinstance(index, DatetimeIndex): index = DatetimeIndex(index)