Skip to content
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

float_format in .to_string can be str #730

Open
zmoon opened this issue Jun 13, 2023 · 7 comments
Open

float_format in .to_string can be str #730

zmoon opened this issue Jun 13, 2023 · 7 comments
Assignees

Comments

@zmoon
Copy link
Contributor

zmoon commented Jun 13, 2023

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?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jun 13, 2023

@twoertwein can you take a look at this?

@twoertwein
Copy link
Member

And perhaps the pandas docs should be updated? Unless support for passing format string was supposed to have been removed?

In the past, it apparently did not work. Since it works now, it probably is intended to pass formatting strings. Do you mind opening an issue at pandas to clarify the intend/update the docs?

@zmoon
Copy link
Contributor Author

zmoon commented Jun 13, 2023

In the past, it apparently did not work.

Issue that seems related: pandas-dev/pandas#9448

@zmoon
Copy link
Contributor Author

zmoon commented Jun 14, 2023

It still doesn't work for extension dtypes.

pd.Series([1.1, 2.2, 3.3]).to_string(float_format="%.3e")  # works
pd.Series([1.1, 2.2, 3.3]).convert_dtypes().to_string(float_format="%.3e")  # `TypeError: 'str' object is not callable`

@twoertwein
Copy link
Member

Thank you, @zmoon ! In that case, I would be inclined to not add str to the annotations.

@zmoon
Copy link
Contributor Author

zmoon commented Jun 14, 2023

That seems fair until the docs are clarified and it works for extension dtypes too. Maybe we could leave this open though?

@twoertwein
Copy link
Member

twoertwein commented Jun 14, 2023

Feel free to open also a new pandas issue to highlight the discrepancy with the extension types (and xref the existing issue about str/no str).

edit: should we use the label "upstream issue" for this (and similar issues which first need to be confirmed/fixed in pandas)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants