Skip to content

Commit a23f901

Browse files
thoojreback
authored andcommitted
Fix order parameters and add decimal to to_string (#23614)
1 parent 8af7637 commit a23f901

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

doc/source/whatsnew/v0.24.0.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ New features
2424
the user to override the engine's default behavior to include or omit the
2525
dataframe's indexes from the resulting Parquet file. (:issue:`20768`)
2626
- :meth:`DataFrame.corr` and :meth:`Series.corr` now accept a callable for generic calculation methods of correlation, e.g. histogram intersection (:issue:`22684`)
27-
27+
- :func:`DataFrame.to_string` now accepts ``decimal`` as an argument, allowing
28+
the user to specify which decimal separator should be used in the output. (:issue:`23614`)
2829

2930
.. _whatsnew_0240.enhancements.extension_array_operators:
3031

@@ -1002,6 +1003,7 @@ Other API Changes
10021003
- :class:`DateOffset` attribute `_cacheable` and method `_should_cache` have been removed (:issue:`23118`)
10031004
- Comparing :class:`Timedelta` to be less or greater than unknown types now raises a ``TypeError`` instead of returning ``False`` (:issue:`20829`)
10041005
- :meth:`Index.hasnans` and :meth:`Series.hasnans` now always return a python boolean. Previously, a python or a numpy boolean could be returned, depending on circumstances (:issue:`23294`).
1006+
- The order of the arguments of :func:`DataFrame.to_html` and :func:`DataFrame.to_string` is rearranged to be consistent with each other. (:issue:`23614`)
10051007

10061008
.. _whatsnew_0240.deprecations:
10071009

pandas/core/frame.py

+15-25
Original file line numberDiff line numberDiff line change
@@ -2035,24 +2035,21 @@ def to_parquet(self, fname, engine='auto', compression='snappy',
20352035
def to_string(self, buf=None, columns=None, col_space=None, header=True,
20362036
index=True, na_rep='NaN', formatters=None, float_format=None,
20372037
sparsify=None, index_names=True, justify=None,
2038-
line_width=None, max_rows=None, max_cols=None,
2039-
show_dimensions=False):
2038+
max_rows=None, max_cols=None, show_dimensions=False,
2039+
decimal='.', line_width=None):
20402040
"""
20412041
Render a DataFrame to a console-friendly tabular output.
2042-
20432042
%(shared_params)s
20442043
line_width : int, optional
20452044
Width to wrap a line in characters.
2046-
20472045
%(returns)s
2048-
20492046
See Also
20502047
--------
20512048
to_html : Convert DataFrame to HTML.
20522049
20532050
Examples
20542051
--------
2055-
>>> d = {'col1' : [1, 2, 3], 'col2' : [4, 5, 6]}
2052+
>>> d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]}
20562053
>>> df = pd.DataFrame(d)
20572054
>>> print(df.to_string())
20582055
col1 col2
@@ -2068,42 +2065,37 @@ def to_string(self, buf=None, columns=None, col_space=None, header=True,
20682065
sparsify=sparsify, justify=justify,
20692066
index_names=index_names,
20702067
header=header, index=index,
2071-
line_width=line_width,
20722068
max_rows=max_rows,
20732069
max_cols=max_cols,
2074-
show_dimensions=show_dimensions)
2070+
show_dimensions=show_dimensions,
2071+
decimal=decimal,
2072+
line_width=line_width)
20752073
formatter.to_string()
20762074

20772075
if buf is None:
20782076
result = formatter.buf.getvalue()
20792077
return result
20802078

2081-
@Substitution(header='whether to print column labels, default True')
2079+
@Substitution(header='Whether to print column labels, default True')
20822080
@Substitution(shared_params=fmt.common_docstring,
20832081
returns=fmt.return_docstring)
20842082
def to_html(self, buf=None, columns=None, col_space=None, header=True,
20852083
index=True, na_rep='NaN', formatters=None, float_format=None,
2086-
sparsify=None, index_names=True, justify=None, bold_rows=True,
2087-
classes=None, escape=True, max_rows=None, max_cols=None,
2088-
show_dimensions=False, notebook=False, decimal='.',
2089-
border=None, table_id=None):
2084+
sparsify=None, index_names=True, justify=None, max_rows=None,
2085+
max_cols=None, show_dimensions=False, decimal='.',
2086+
bold_rows=True, classes=None, escape=True,
2087+
notebook=False, border=None, table_id=None):
20902088
"""
20912089
Render a DataFrame as an HTML table.
2092-
20932090
%(shared_params)s
2094-
bold_rows : boolean, default True
2095-
Make the row labels bold in the output
2091+
bold_rows : bool, default True
2092+
Make the row labels bold in the output.
20962093
classes : str or list or tuple, default None
2097-
CSS class(es) to apply to the resulting html table
2098-
escape : boolean, default True
2094+
CSS class(es) to apply to the resulting html table.
2095+
escape : bool, default True
20992096
Convert the characters <, >, and & to HTML-safe sequences.
21002097
notebook : {True, False}, default False
21012098
Whether the generated HTML is for IPython Notebook.
2102-
decimal : string, default '.'
2103-
Character recognized as decimal separator, e.g. ',' in Europe
2104-
2105-
.. versionadded:: 0.18.0
2106-
21072099
border : int
21082100
A ``border=border`` attribute is included in the opening
21092101
`<table>` tag. Default ``pd.options.html.border``.
@@ -2114,9 +2106,7 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
21142106
A css id is included in the opening `<table>` tag if specified.
21152107
21162108
.. versionadded:: 0.23.0
2117-
21182109
%(returns)s
2119-
21202110
See Also
21212111
--------
21222112
to_string : Convert DataFrame to a string.

pandas/io/formats/format.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@
8888
Maximum number of columns to display in the console.
8989
show_dimensions : bool, default False
9090
Display DataFrame dimensions (number of rows by number of columns).
91+
decimal : str, default '.'
92+
Character recognized as decimal separator, e.g. ',' in Europe.
93+
94+
.. versionadded:: 0.18.0
9195
"""
9296

9397
_VALID_JUSTIFY_PARAMETERS = ("left", "right", "center", "justify",
@@ -101,8 +105,6 @@
101105
String representation of the dataframe.
102106
"""
103107

104-
docstring_to_string = common_docstring + return_docstring
105-
106108

107109
class CategoricalFormatter(object):
108110

pandas/tests/io/formats/test_format.py

+6
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,12 @@ def test_to_string_format_na(self):
14581458
'4 4.0 bar')
14591459
assert result == expected
14601460

1461+
def test_to_string_decimal(self):
1462+
# Issue #23614
1463+
df = DataFrame({'A': [6.0, 3.1, 2.2]})
1464+
expected = ' A\n0 6,0\n1 3,1\n2 2,2'
1465+
assert df.to_string(decimal=',') == expected
1466+
14611467
def test_to_string_line_width(self):
14621468
df = DataFrame(123, lrange(10, 15), lrange(30))
14631469
s = df.to_string(line_width=80)

0 commit comments

Comments
 (0)