Skip to content

To string with encoding #28951

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 32 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ae76e46
Added code in core/frame.py to include encoding in to_string
Oct 13, 2019
3985040
Modified io/formats/format.py to include encoding parameter in to_strin
Oct 13, 2019
8eea18e
Added encoding to pandas/core/format.py for to_string
Oct 13, 2019
d2c70ee
Changed formatting with black
mohitanand001 Oct 13, 2019
835b22d
Added test for to_string encoding
mohitanand001 Oct 13, 2019
4e30d7c
Removed spaces from test_to_string.py file
mohitanand001 Oct 13, 2019
78ba34b
Added whatsnew for to_string with encoding param.
mohitanand001 Oct 13, 2019
56fdad9
Modified whatsnew with to_string encoding note in Others section.
mohitanand001 Oct 13, 2019
83ccffd
Modified func to meth in for to_string note added in whatsnew.
mohitanand001 Oct 13, 2019
19801bb
Added full stop at end of line in to_string docstring.
mohitanand001 Oct 13, 2019
d44afa7
Moved whatsnew note from Other to Other enhancements section.
mohitanand001 Oct 14, 2019
b4b983b
Added annotation to encoding parameter in to_string in pandas/core/fr…
mohitanand001 Oct 14, 2019
acb2c6e
Merge branch 'master' into to_string_with_encoding
mohitanand001 Oct 16, 2019
236db38
Added encoding in test_format.py and removed test_to_string.py
mohitanand001 Oct 19, 2019
93e8b11
Merge branch 'to_string_with_encoding' of https://github.com/farzieng…
mohitanand001 Oct 19, 2019
1f3e55d
Added encoding in pytest parameter in test_format.py
mohitanand001 Oct 19, 2019
b0364d2
Fixed encoding paramter placement in test_format.py
mohitanand001 Oct 19, 2019
12ddc8f
Added test to check if encoding is present and filepath is not string…
mohitanand001 Oct 19, 2019
f82fe78
Added pytest.raises instead of assert.
mohitanand001 Oct 19, 2019
5ef9cec
Fix pytest.raise for test_format.py
mohitanand001 Oct 19, 2019
df58c1f
If encoding is specified and buf is not file, raise ValueError.
mohitanand001 Oct 19, 2019
496f68f
Fix pytest.raise in test_filepath_or_buffer_arg.
mohitanand001 Oct 19, 2019
725b2ec
Reformatted with black.
mohitanand001 Oct 19, 2019
698d3b7
Raise ValueError in to_html, to_latex, and to_string if buf is a buff…
mohitanand001 Oct 19, 2019
96cc810
ValueError placement fixed in formats.py
mohitanand001 Oct 20, 2019
1a35eeb
Removed commented code from format.py
mohitanand001 Oct 20, 2019
f865ac3
Removed commented code from format.py
mohitanand001 Oct 20, 2019
757a1f5
Merge branch 'to_string_with_encoding' of https://github.com/farzieng…
mohitanand001 Oct 20, 2019
835cdb8
Added encoding in assert_filepath_or_buffer_equals fixture.
mohitanand001 Oct 21, 2019
648fa55
Modified test_filepath_or_buffer_arg to have custom data(removed floa…
mohitanand001 Oct 22, 2019
3698a2a
Modified test_filepath_or_buffer_arg to have custom data(removed floa…
mohitanand001 Oct 22, 2019
f8917de
Merge branch 'to_string_with_encoding' of https://github.com/farzieng…
mohitanand001 Oct 22, 2019
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Other enhancements
(:issue:`28368`)
- :meth:`DataFrame.to_json` now accepts an ``indent`` integer argument to enable pretty printing of JSON output (:issue:`12004`)
- :meth:`read_stata` can read Stata 119 dta files. (:issue:`28250`)
- Added ``encoding`` argument to :meth:`DataFrame.to_string` for non-ascii text (:issue:`28766`)

Build Changes
^^^^^^^^^^^^^
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ def to_string(
decimal: str = ".",
line_width: Optional[int] = None,
max_colwidth: Optional[int] = None,
encoding=None,
) -> Optional[str]:
"""
Render a DataFrame to a console-friendly tabular output.
Expand All @@ -754,6 +755,10 @@ def to_string(
Max width to truncate each column in characters. By default, no limit.

.. versionadded:: 1.0.0
encoding : str, default "utf-8"
Set character encoding.

.. versionadded:: 1.0
%(returns)s
See Also
--------
Expand Down Expand Up @@ -792,7 +797,7 @@ def to_string(
decimal=decimal,
line_width=line_width,
)
return formatter.to_string(buf=buf)
return formatter.to_string(buf=buf, encoding=encoding)

# ----------------------------------------------------------------------

Expand Down
8 changes: 6 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,12 @@ def _join_multiline(self, *args) -> str:
st = ed
return "\n\n".join(str_lst)

def to_string(self, buf: Optional[FilePathOrBuffer[str]] = None) -> Optional[str]:
return self.get_result(buf=buf)
def to_string(
self,
buf: Optional[FilePathOrBuffer[str]] = None,
encoding: Optional[str] = None,
) -> Optional[str]:
return self.get_result(buf=buf, encoding=encoding)

def to_latex(
self,
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_to_string_encoding(float_frame,):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are all test_to_format.py, pls move there

would take a followup to split up test_to_format into 2 parts though (e.g. move the to_string out to a separate file)

# GH 28766
path = "test_to_string_file"
float_frame.to_string(path, encoding="gbk")
with open(str(path), "r", encoding="gbk") as f:
assert float_frame.to_string() == f.read()