Skip to content

BUG/CLN: Clean float / complex string formatting #36799

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 26 commits into from
Oct 14, 2020
Merged
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
406ed4d
CLN: Clean float / complex string formatting
dsaxton Oct 2, 2020
a1228c9
Fix
dsaxton Oct 2, 2020
eabef62
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 2, 2020
72be97f
Fix and test
dsaxton Oct 2, 2020
e1d1ba3
Fix
dsaxton Oct 2, 2020
d38839c
Fix
dsaxton Oct 2, 2020
0da1c46
Nit
dsaxton Oct 2, 2020
4ea7dcf
Nit
dsaxton Oct 2, 2020
376b06e
Change doc
dsaxton Oct 2, 2020
473d674
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 2, 2020
e564fee
Escape
dsaxton Oct 2, 2020
6cec317
Add failing test
dsaxton Oct 2, 2020
50ccbc0
Edit
dsaxton Oct 2, 2020
e725cb2
Maybe
dsaxton Oct 2, 2020
3a94daa
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 2, 2020
03e8cab
Comment
dsaxton Oct 2, 2020
46949fd
Remove
dsaxton Oct 2, 2020
11f20d0
Compile and comment
dsaxton Oct 3, 2020
44f0792
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 3, 2020
229b5fc
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 3, 2020
eec9d89
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 8, 2020
0e0dd92
Move into other function
dsaxton Oct 8, 2020
956ecf3
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 8, 2020
25dd8f1
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 10, 2020
eca0413
Note
dsaxton Oct 11, 2020
00b71b2
Merge remote-tracking branch 'upstream/master' into fix-is-numeric-he…
dsaxton Oct 11, 2020
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
21 changes: 4 additions & 17 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,9 +1473,9 @@ def format_values_with(float_format):

if self.fixed_width:
if is_complex:
result = _trim_zeros_complex(values, self.decimal, na_rep)
result = values
else:
result = _trim_zeros_float(values, self.decimal, na_rep)
result = _trim_zeros_float(values, self.decimal)
return np.asarray(result, dtype="object")

return values
Expand Down Expand Up @@ -1855,29 +1855,16 @@ def just(x):
return result


def _trim_zeros_complex(
str_complexes: np.ndarray, decimal: str = ".", na_rep: str = "NaN"
) -> List[str]:
"""
Separates the real and imaginary parts from the complex number, and
executes the _trim_zeros_float method on each of those.
"""
return [
"".join(_trim_zeros_float(re.split(r"([j+-])", x), decimal, na_rep))
for x in str_complexes
]


def _trim_zeros_float(
str_floats: Union[np.ndarray, List[str]], decimal: str = ".", na_rep: str = "NaN"
str_floats: Union[np.ndarray, List[str]], decimal: str = "."
) -> List[str]:
"""
Trims zeros, leaving just one before the decimal points if need be.
"""
trimmed = str_floats

def _is_number(x):
return x != na_rep and not x.endswith("inf")
return re.match(r"\s*-?[0-9]+(\.[0-9]*)?", x) is not None

def _cond(values):
finite = [x for x in values if _is_number(x)]
Expand Down