diff --git a/pandas/_typing.py b/pandas/_typing.py index 87979aba9ada4..6059bced4a7d4 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -94,6 +94,13 @@ AnyArrayLike = Union[ArrayLike, "Index", "Series"] TimeArrayLike = Union["DatetimeArray", "TimedeltaArray"] +# list-like + +# Cannot use `Sequence` because a string is a sequence, and we don't want to +# accept that. Could refine if https://github.com/python/typing/issues/256 is +# resolved to differentiate between Sequence[str] and str +ListLike = Union[AnyArrayLike, List, range] + # scalars PythonScalar = Union[str, float, bool] @@ -130,7 +137,7 @@ Ordered = Optional[bool] JSONSerializable = Optional[Union[PythonScalar, List, Dict]] Frequency = Union[str, "BaseOffset"] -Axes = Union[AnyArrayLike, List, range] +Axes = ListLike RandomState = Union[ int, diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 10e64f7fbd52c..7318a2d2a5ab0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1134,9 +1134,9 @@ def _repr_html_(self) -> str | None: def to_string( self, buf: None = ..., - columns: Sequence[str] | None = ..., + columns: Axes | None = ..., col_space: int | list[int] | dict[Hashable, int] | None = ..., - header: bool | Sequence[str] = ..., + header: bool | list[str] = ..., index: bool = ..., na_rep: str = ..., formatters: fmt.FormattersType | None = ..., @@ -1159,9 +1159,9 @@ def to_string( def to_string( self, buf: FilePath | WriteBuffer[str], - columns: Sequence[str] | None = ..., + columns: Axes | None = ..., col_space: int | list[int] | dict[Hashable, int] | None = ..., - header: bool | Sequence[str] = ..., + header: bool | list[str] = ..., index: bool = ..., na_rep: str = ..., formatters: fmt.FormattersType | None = ..., @@ -1181,8 +1181,8 @@ def to_string( ... @Substitution( - header_type="bool or sequence of str", - header="Write out the column names. If a list of strings " + header_type="bool or list of str", + header="Write out the column names. If a list of columns " "is given, it is assumed to be aliases for the " "column names", col_space_type="int, list or dict of int", @@ -1194,9 +1194,9 @@ def to_string( def to_string( self, buf: FilePath | WriteBuffer[str] | None = None, - columns: Sequence[str] | None = None, + columns: Axes | None = None, col_space: int | list[int] | dict[Hashable, int] | None = None, - header: bool | Sequence[str] = True, + header: bool | list[str] = True, index: bool = True, na_rep: str = "NaN", formatters: fmt.FormattersType | None = None, @@ -2977,9 +2977,9 @@ def to_orc( def to_html( self, buf: FilePath | WriteBuffer[str], - columns: Sequence[Level] | None = ..., + columns: Axes | None = ..., col_space: ColspaceArgType | None = ..., - header: bool | Sequence[str] = ..., + header: bool = ..., index: bool = ..., na_rep: str = ..., formatters: FormattersType | None = ..., @@ -3006,9 +3006,9 @@ def to_html( def to_html( self, buf: None = ..., - columns: Sequence[Level] | None = ..., + columns: Axes | None = ..., col_space: ColspaceArgType | None = ..., - header: bool | Sequence[str] = ..., + header: bool = ..., index: bool = ..., na_rep: str = ..., formatters: FormattersType | None = ..., @@ -3042,9 +3042,9 @@ def to_html( def to_html( self, buf: FilePath | WriteBuffer[str] | None = None, - columns: Sequence[Level] | None = None, + columns: Axes | None = None, col_space: ColspaceArgType | None = None, - header: bool | Sequence[str] = True, + header: bool = True, index: bool = True, na_rep: str = "NaN", formatters: FormattersType | None = None, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 33dc62564d34e..519338d0354a9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3103,7 +3103,7 @@ def to_latex( self, buf: None = ..., columns: Sequence[Hashable] | None = ..., - header: bool_t | Sequence[str] = ..., + header: bool_t | list[str] = ..., index: bool_t = ..., na_rep: str = ..., formatters: FormattersType | None = ..., @@ -3130,7 +3130,7 @@ def to_latex( self, buf: FilePath | WriteBuffer[str], columns: Sequence[Hashable] | None = ..., - header: bool_t | Sequence[str] = ..., + header: bool_t | list[str] = ..., index: bool_t = ..., na_rep: str = ..., formatters: FormattersType | None = ..., @@ -3157,7 +3157,7 @@ def to_latex( self, buf: FilePath | WriteBuffer[str] | None = None, columns: Sequence[Hashable] | None = None, - header: bool_t | Sequence[str] = True, + header: bool_t | list[str] = True, index: bool_t = True, na_rep: str = "NaN", formatters: FormattersType | None = None, diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index bda8de2de8897..baf264353fca7 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -102,7 +102,7 @@ def decimal(self) -> str: return self.fmt.decimal @property - def header(self) -> bool | Sequence[str]: + def header(self) -> bool | list[str]: return self.fmt.header @property diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index ccae3887c248e..f9b081afcd045 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -120,7 +120,7 @@ ---------- buf : str, Path or StringIO-like, optional, default None Buffer to write to. If None, the output is returned as a string. - columns : sequence, optional, default None + columns : array-like, optional, default None The subset of columns to write. Writes all columns by default. col_space : %(col_space_type)s, optional %(col_space)s. @@ -564,9 +564,9 @@ class DataFrameFormatter: def __init__( self, frame: DataFrame, - columns: Sequence[Hashable] | None = None, + columns: Axes | None = None, col_space: ColspaceArgType | None = None, - header: bool | Sequence[str] = True, + header: bool | list[str] = True, index: bool = True, na_rep: str = "NaN", formatters: FormattersType | None = None, @@ -686,11 +686,9 @@ def _initialize_justify(self, justify: str | None) -> str: else: return justify - def _initialize_columns(self, columns: Sequence[Hashable] | None) -> Index: + def _initialize_columns(self, columns: Axes | None) -> Index: if columns is not None: - # GH 47231 - columns doesn't have to be `Sequence[str]` - # Will fix in later PR - cols = ensure_index(cast(Axes, columns)) + cols = ensure_index(columns) self.frame = self.frame[cols] return cols else: