Skip to content

Remove Sequence[str] as type used in DataFrame.to_string() #47233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...,
Expand All @@ -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 = ...,
Expand All @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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 = ...,
Expand All @@ -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 = ...,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...,
Expand All @@ -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 = ...,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down