-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 2 commits
08fb709
3e49016
a9e2849
d65511c
d7d82f5
ca9b7ac
6da7d4f
bc33a68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -627,6 +627,12 @@ def __init__( | |
|
||
manager = get_option("mode.data_manager") | ||
|
||
# GH47215 | ||
if index is not None and isinstance(index, set): | ||
raise ValueError("index cannot be a set") | ||
if columns is not None and isinstance(columns, set): | ||
raise ValueError("columns cannot be a set") | ||
|
||
if copy is None: | ||
if isinstance(data, dict): | ||
# retain pre-GH#38939 default behavior | ||
|
@@ -730,10 +736,7 @@ def __init__( | |
if not isinstance(data, np.ndarray) and treat_as_nested(data): | ||
# exclude ndarray as we may have cast it a few lines above | ||
if columns is not None: | ||
# error: Argument 1 to "ensure_index" has incompatible type | ||
# "Collection[Any]"; expected "Union[Union[Union[ExtensionArray, | ||
# ndarray], Index, Series], Sequence[Any]]" | ||
columns = ensure_index(columns) # type: ignore[arg-type] | ||
columns = ensure_index(columns) | ||
arrays, columns, index = nested_data_to_arrays( | ||
# error: Argument 3 to "nested_data_to_arrays" has incompatible | ||
# type "Optional[Collection[Any]]"; expected "Optional[Index]" | ||
|
@@ -771,14 +774,8 @@ def __init__( | |
if index is None or columns is None: | ||
raise ValueError("DataFrame constructor not properly called!") | ||
|
||
# Argument 1 to "ensure_index" has incompatible type "Collection[Any]"; | ||
# expected "Union[Union[Union[ExtensionArray, ndarray], | ||
# Index, Series], Sequence[Any]]" | ||
index = ensure_index(index) # type: ignore[arg-type] | ||
# Argument 1 to "ensure_index" has incompatible type "Collection[Any]"; | ||
# expected "Union[Union[Union[ExtensionArray, ndarray], | ||
# Index, Series], Sequence[Any]]" | ||
columns = ensure_index(columns) # type: ignore[arg-type] | ||
index = ensure_index(index) | ||
columns = ensure_index(columns) | ||
|
||
if not dtype: | ||
dtype, _ = infer_dtype_from_scalar(data, pandas_dtype=True) | ||
|
@@ -1108,9 +1105,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 = ..., | ||
index: bool = ..., | ||
na_rep: str = ..., | ||
formatters: fmt.FormattersType | None = ..., | ||
|
@@ -1133,9 +1130,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 = ..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid invariant collection types (list, dict) in argument positions, in favor of covariant types like Mapping or Sequence; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing it to |
||
index: bool = ..., | ||
na_rep: str = ..., | ||
formatters: fmt.FormattersType | None = ..., | ||
|
@@ -1155,8 +1152,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 array-like of column names", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my comment #47215 (comment) about the (imo) vague definition of array-like. what about list-like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we do accept a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Turns out we don't accept a |
||
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", | ||
|
@@ -1168,9 +1165,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 = True, | ||
index: bool = True, | ||
na_rep: str = "NaN", | ||
formatters: fmt.FormattersType | None = None, | ||
|
@@ -2914,9 +2911,9 @@ def to_parquet( | |
def to_html( | ||
self, | ||
buf: FilePath | WriteBuffer[str] | None = None, | ||
columns: Sequence[str] | 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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List and Dict are probably too restrictive in general? If we use this in places where we accept list-like or dict-like.
Also from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions
This also helps us develop robust code since mypy will report if we try to write into the passed argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only getting applied to the
index
andcolumns
argument ofpd.DataFrame
. We can't useSequence
, as that would allow a string. We document the arguments asIndex
orarray-like
. I don't thinkList
is too restrictive - the problem is thatSequence
is too broad because of the issue with strings. I could see removingDict
since we don't document that it is acceptable.