-
-
Notifications
You must be signed in to change notification settings - Fork 146
Closed
Labels
Output-Formatting__repr__ of pandas objects, to_string__repr__ of pandas objects, to_string
Description
Describe the bug
The change in #721 broke this for mypy. Although the current docs say "one-parameter function, optional, default None", you are allowed to pass a string that specifies the format like in .to_csv
.
To Reproduce
These usages all work with both frame and series.
# t.py
import pandas as pd
df = pd.DataFrame({"a": [0.1, 1.0, 3.14]})
print(df.to_string(float_format="%.3g"))
print(df.to_string(float_format="{:.3g}".format))
print(df.to_string(float_format=lambda x: f"{x:.3e}"))
print(df.to_string(float_format=pd.io.formats.format.EngFormatter(3, True)))
print(df.a.to_string(float_format="%.3g"))
print(df.a.to_string(float_format="{:.3g}".format))
print(df.a.to_string(float_format=lambda x: f"{x:.3e}"))
print(df.a.to_string(float_format=pd.io.formats.format.EngFormatter(3, True)))
> mypy t.py
t.py:5: error: No overload variant of "to_string" of "DataFrame" matches argument type "str" [call-overload]
t.py:5: note: Possible overload variants:
t.py:5: note: def [HashableT1 <: Hashable, HashableT2 <: Hashable] to_string(self, buf: Union[str, PathLike[str], WriteBuffer[str]], columns: Union[List[HashableT1], Index, Series[Any], None] = ..., col_space: Union[int, List[int], Dict[HashableT2, int], None] = ..., header: Union[bool, List[str], Tuple[str, ...]] = ..., index: bool = ..., na_rep: str = ..., formatters: Union[List[Callable[..., Any]], Tuple[Callable[..., Any], ...], Mapping[Union[str, int], Callable[..., Any]], None] = ..., float_format: Optional[Callable[[float], str]] = ..., sparsify: Optional[bool] = ..., index_names: bool = ..., justify: Optional[str] = ..., max_rows: Optional[int] = ..., max_cols: Optional[int] = ..., show_dimensions: bool = ..., decimal: str = ..., line_width: Optional[int] = ..., min_rows: Optional[int] = ..., max_colwidth: Optional[int] = ..., encoding: Optional[str] = ...) -> None
t.py:5: note: def [HashableT <: Hashable] to_string(self, buf: None = ..., columns: Union[List[HashableT], Index, Series[Any], None] = ..., col_space: Union[int, List[int], Dict[Hashable, int], None] = ..., header: Union[bool, Sequence[str]] = ..., index: bool = ..., na_rep: str = ..., formatters: Union[List[Callable[..., Any]], Tuple[Callable[..., Any], ...], Mapping[Union[str, int], Callable[..., Any]], None] = ..., float_format: Optional[Callable[[float], str]] = ..., sparsify: Optional[bool] = ..., index_names: bool = ..., justify: Optional[str] = ..., max_rows: Optional[int] = ..., max_cols: Optional[int] = ..., show_dimensions: bool = ..., decimal: str = ..., line_width: Optional[int] = ..., min_rows: Optional[int] = ..., max_colwidth: Optional[int] = ..., encoding: Optional[str] = ...) -> str
t.py:10: error: No overload variant of "to_string" of "Series" matches argument type "str" [call-overload]
t.py:10: note: Possible overload variants:
t.py:10: note: def to_string(self, buf: Union[str, PathLike[str], WriteBuffer[str]], na_rep: str = ..., float_format: Callable[[float], str] = ..., header: bool = ..., index: bool = ..., length: bool = ..., dtype: bool = ..., name: bool = ..., max_rows: Optional[int] = ..., min_rows: Optional[int] = ...) -> None
t.py:10: note: def to_string(self, buf: None = ..., na_rep: str = ..., float_format: Callable[[float], str] = ..., header: bool = ..., index: bool = ..., length: bool = ..., dtype: bool = ..., name: bool = ..., max_rows: Optional[int] = ..., min_rows: Optional[int] = ...) -> str
Found 2 errors in 1 file (checked 1 source file)
Please complete the following information:
- OS: [e.g. Windows, Linux, MacOS] Windows
- OS Version [e.g. 22] 10
- python version 3.10.11
- version of type checker mypy 1.3.0
- version of installed
pandas-stubs
2.0.2.230605 - pandas 2.0.2
Additional context
Maybe FloatFormatType
can be used (again) but could be updated to be more specific about the callable (Callable[[float], str]
).
And perhaps the pandas docs should be updated? Unless support for passing format string was supposed to have been removed?
Metadata
Metadata
Assignees
Labels
Output-Formatting__repr__ of pandas objects, to_string__repr__ of pandas objects, to_string