-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix string formatting #52883
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
Fix string formatting #52883
Changes from all commits
25be5b1
e929f2d
4a80f38
2f65b79
5041d23
4ca3af9
6231f1a
0044b61
113af37
1a17b68
81212cd
1f0e4d8
b7c6900
0a0d0fc
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 |
---|---|---|
|
@@ -13,7 +13,8 @@ including other versions of pandas. | |
|
||
Fixed regressions | ||
~~~~~~~~~~~~~~~~~ | ||
- | ||
- Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) | ||
|
||
|
||
.. --------------------------------------------------------------------------- | ||
.. _whatsnew_202.bug_fixes: | ||
|
@@ -33,6 +34,7 @@ Other | |
~~~~~ | ||
- Raised a better error message when calling :func:`Series.dt.to_pydatetime` with :class:`ArrowDtype` with ``pyarrow.date32`` or ``pyarrow.date64`` type (:issue:`52812`) | ||
|
||
|
||
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. Could you undo these new whitespaces? |
||
.. --------------------------------------------------------------------------- | ||
.. _whatsnew_202.contributors: | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1392,7 +1392,10 @@ def _format(x): | |||||||||
fmt_values = [] | ||||||||||
for i, v in enumerate(vals): | ||||||||||
if not is_float_type[i] and leading_space or self.formatter is not None: | ||||||||||
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.
Suggested change
|
||||||||||
fmt_values.append(f" {_format(v)}") | ||||||||||
if leading_space: | ||||||||||
fmt_values.append(f" {_format(v)}") | ||||||||||
else: | ||||||||||
fmt_values.append(f"{_format(v)}") | ||||||||||
Comment on lines
+1395
to
+1398
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.
Suggested change
It seems this if/else is already handled by the else block below. I think a smaller change is sufficient. WDYT? @reddyrg1 @mroeschke @phofl |
||||||||||
elif is_float_type[i]: | ||||||||||
fmt_values.append(float_format(v)) | ||||||||||
else: | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2099,6 +2099,19 @@ def test_max_rows_fitted(self, length, min_rows, max_rows, expected): | |
result = formatter.max_rows_fitted | ||
assert result == expected | ||
|
||
def test_no_extra_space(self): | ||
# GH 52690: Check that no extra space is given | ||
# Expected Output | ||
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. Could you remove these comments? |
||
col1 = "TEST" | ||
col2 = "PANDAS" | ||
col3 = "to_string" | ||
expected = f"{col1:<6s} {col2:<7s} {col3:<10s}" | ||
# Testing | ||
df = DataFrame([{"col1": "TEST", "col2": "PANDAS", "col3": "to_string"}]) | ||
d = {"col1": "{:<6s}".format, "col2": "{:<7s}".format, "col3": "{:<10s}".format} | ||
result = df.to_string(index=False, header=False, formatters=d) | ||
assert result == expected | ||
|
||
|
||
def gen_series_formatting(): | ||
s1 = Series(["a"] * 100) | ||
|
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.
Could you merge in main and add this to the existing
v2.0.2.rst
file instead?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.
ok, should have done this