Skip to content

Commit 920c025

Browse files
Dr-Irvjreback
andauthored
Remove Sequence[str] as type used in DataFrame.to_string() (#47233)
* API: Disallow sets as index and columns argument in DataFrame constructor * TYP: Remove Sequence[str] from to_string, to_html * use list[str] for header argument. Remove dict as index and columns argument for DataFrame. Define ListLike * use Axes rather than Sequence[Hashable] --------- Co-authored-by: Jeff Reback <[email protected]>
1 parent f2a41fa commit 920c025

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

pandas/_typing.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
AnyArrayLike = Union[ArrayLike, "Index", "Series"]
9595
TimeArrayLike = Union["DatetimeArray", "TimedeltaArray"]
9696

97+
# list-like
98+
99+
# Cannot use `Sequence` because a string is a sequence, and we don't want to
100+
# accept that. Could refine if https://github.com/python/typing/issues/256 is
101+
# resolved to differentiate between Sequence[str] and str
102+
ListLike = Union[AnyArrayLike, List, range]
103+
97104
# scalars
98105

99106
PythonScalar = Union[str, float, bool]
@@ -130,7 +137,7 @@
130137
Ordered = Optional[bool]
131138
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
132139
Frequency = Union[str, "BaseOffset"]
133-
Axes = Union[AnyArrayLike, List, range]
140+
Axes = ListLike
134141

135142
RandomState = Union[
136143
int,

pandas/core/frame.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -1134,9 +1134,9 @@ def _repr_html_(self) -> str | None:
11341134
def to_string(
11351135
self,
11361136
buf: None = ...,
1137-
columns: Sequence[str] | None = ...,
1137+
columns: Axes | None = ...,
11381138
col_space: int | list[int] | dict[Hashable, int] | None = ...,
1139-
header: bool | Sequence[str] = ...,
1139+
header: bool | list[str] = ...,
11401140
index: bool = ...,
11411141
na_rep: str = ...,
11421142
formatters: fmt.FormattersType | None = ...,
@@ -1159,9 +1159,9 @@ def to_string(
11591159
def to_string(
11601160
self,
11611161
buf: FilePath | WriteBuffer[str],
1162-
columns: Sequence[str] | None = ...,
1162+
columns: Axes | None = ...,
11631163
col_space: int | list[int] | dict[Hashable, int] | None = ...,
1164-
header: bool | Sequence[str] = ...,
1164+
header: bool | list[str] = ...,
11651165
index: bool = ...,
11661166
na_rep: str = ...,
11671167
formatters: fmt.FormattersType | None = ...,
@@ -1181,8 +1181,8 @@ def to_string(
11811181
...
11821182

11831183
@Substitution(
1184-
header_type="bool or sequence of str",
1185-
header="Write out the column names. If a list of strings "
1184+
header_type="bool or list of str",
1185+
header="Write out the column names. If a list of columns "
11861186
"is given, it is assumed to be aliases for the "
11871187
"column names",
11881188
col_space_type="int, list or dict of int",
@@ -1194,9 +1194,9 @@ def to_string(
11941194
def to_string(
11951195
self,
11961196
buf: FilePath | WriteBuffer[str] | None = None,
1197-
columns: Sequence[str] | None = None,
1197+
columns: Axes | None = None,
11981198
col_space: int | list[int] | dict[Hashable, int] | None = None,
1199-
header: bool | Sequence[str] = True,
1199+
header: bool | list[str] = True,
12001200
index: bool = True,
12011201
na_rep: str = "NaN",
12021202
formatters: fmt.FormattersType | None = None,
@@ -2965,9 +2965,9 @@ def to_orc(
29652965
def to_html(
29662966
self,
29672967
buf: FilePath | WriteBuffer[str],
2968-
columns: Sequence[Level] | None = ...,
2968+
columns: Axes | None = ...,
29692969
col_space: ColspaceArgType | None = ...,
2970-
header: bool | Sequence[str] = ...,
2970+
header: bool = ...,
29712971
index: bool = ...,
29722972
na_rep: str = ...,
29732973
formatters: FormattersType | None = ...,
@@ -2994,9 +2994,9 @@ def to_html(
29942994
def to_html(
29952995
self,
29962996
buf: None = ...,
2997-
columns: Sequence[Level] | None = ...,
2997+
columns: Axes | None = ...,
29982998
col_space: ColspaceArgType | None = ...,
2999-
header: bool | Sequence[str] = ...,
2999+
header: bool = ...,
30003000
index: bool = ...,
30013001
na_rep: str = ...,
30023002
formatters: FormattersType | None = ...,
@@ -3030,9 +3030,9 @@ def to_html(
30303030
def to_html(
30313031
self,
30323032
buf: FilePath | WriteBuffer[str] | None = None,
3033-
columns: Sequence[Level] | None = None,
3033+
columns: Axes | None = None,
30343034
col_space: ColspaceArgType | None = None,
3035-
header: bool | Sequence[str] = True,
3035+
header: bool = True,
30363036
index: bool = True,
30373037
na_rep: str = "NaN",
30383038
formatters: FormattersType | None = None,

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3101,7 +3101,7 @@ def to_latex(
31013101
self,
31023102
buf: None = ...,
31033103
columns: Sequence[Hashable] | None = ...,
3104-
header: bool_t | Sequence[str] = ...,
3104+
header: bool_t | list[str] = ...,
31053105
index: bool_t = ...,
31063106
na_rep: str = ...,
31073107
formatters: FormattersType | None = ...,
@@ -3128,7 +3128,7 @@ def to_latex(
31283128
self,
31293129
buf: FilePath | WriteBuffer[str],
31303130
columns: Sequence[Hashable] | None = ...,
3131-
header: bool_t | Sequence[str] = ...,
3131+
header: bool_t | list[str] = ...,
31323132
index: bool_t = ...,
31333133
na_rep: str = ...,
31343134
formatters: FormattersType | None = ...,
@@ -3155,7 +3155,7 @@ def to_latex(
31553155
self,
31563156
buf: FilePath | WriteBuffer[str] | None = None,
31573157
columns: Sequence[Hashable] | None = None,
3158-
header: bool_t | Sequence[str] = True,
3158+
header: bool_t | list[str] = True,
31593159
index: bool_t = True,
31603160
na_rep: str = "NaN",
31613161
formatters: FormattersType | None = None,

pandas/io/formats/csvs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def decimal(self) -> str:
102102
return self.fmt.decimal
103103

104104
@property
105-
def header(self) -> bool | Sequence[str]:
105+
def header(self) -> bool | list[str]:
106106
return self.fmt.header
107107

108108
@property

pandas/io/formats/format.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
----------
121121
buf : str, Path or StringIO-like, optional, default None
122122
Buffer to write to. If None, the output is returned as a string.
123-
columns : sequence, optional, default None
123+
columns : array-like, optional, default None
124124
The subset of columns to write. Writes all columns by default.
125125
col_space : %(col_space_type)s, optional
126126
%(col_space)s.
@@ -564,9 +564,9 @@ class DataFrameFormatter:
564564
def __init__(
565565
self,
566566
frame: DataFrame,
567-
columns: Sequence[Hashable] | None = None,
567+
columns: Axes | None = None,
568568
col_space: ColspaceArgType | None = None,
569-
header: bool | Sequence[str] = True,
569+
header: bool | list[str] = True,
570570
index: bool = True,
571571
na_rep: str = "NaN",
572572
formatters: FormattersType | None = None,
@@ -686,11 +686,9 @@ def _initialize_justify(self, justify: str | None) -> str:
686686
else:
687687
return justify
688688

689-
def _initialize_columns(self, columns: Sequence[Hashable] | None) -> Index:
689+
def _initialize_columns(self, columns: Axes | None) -> Index:
690690
if columns is not None:
691-
# GH 47231 - columns doesn't have to be `Sequence[str]`
692-
# Will fix in later PR
693-
cols = ensure_index(cast(Axes, columns))
691+
cols = ensure_index(columns)
694692
self.frame = self.frame[cols]
695693
return cols
696694
else:

0 commit comments

Comments
 (0)