-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
support binary file handles in to_csv #35129
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
Changes from all commits
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,6 +13,25 @@ including other versions of pandas. | |
Enhancements | ||
~~~~~~~~~~~~ | ||
|
||
.. _whatsnew_120.binary_handle_to_csv: | ||
|
||
Support for binary file handles in ``to_csv`` | ||
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. can you add this example to the io.rst section as well 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. I added a new subsection in "Data Handling". Is that an appropriate place? |
||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
:meth:`to_csv` supports file handles in binary mode (:issue:`19827` and :issue:`35058`) | ||
with ``encoding`` (:issue:`13068` and :issue:`23854`) and ``compression`` (:issue:`22555`). | ||
``mode`` has to contain a ``b`` for binary handles to be supported. | ||
|
||
For example: | ||
|
||
.. ipython:: python | ||
|
||
import io | ||
|
||
data = pd.DataFrame([0, 1, 2]) | ||
buffer = io.BytesIO() | ||
data.to_csv(buffer, mode="w+b", encoding="utf-8", compression="gzip") | ||
|
||
.. _whatsnew_120.enhancements.other: | ||
|
||
Other enhancements | ||
|
@@ -121,7 +140,7 @@ MultiIndex | |
I/O | ||
^^^ | ||
|
||
- | ||
- Bug in :meth:`to_csv` caused a ``ValueError`` when it was called with a filename in combination with ``mode`` containing a ``b`` (:issue:`35058`) | ||
- | ||
|
||
Plotting | ||
|
@@ -167,4 +186,4 @@ Other | |
.. _whatsnew_120.contributors: | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
~~~~~~~~~~~~ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3021,13 +3021,18 @@ def to_csv( | |
---------- | ||
path_or_buf : str or file handle, default None | ||
File path or object, if None is provided the result is returned as | ||
a string. If a file object is passed it should be opened with | ||
`newline=''`, disabling universal newlines. | ||
a string. If a non-binary file object is passed, it should be opened | ||
with `newline=''`, disabling universal newlines. If a binary | ||
file object is passed, `mode` needs to contain a `'b'`. | ||
|
||
.. versionchanged:: 0.24.0 | ||
|
||
Was previously named "path" for Series. | ||
|
||
.. versionchanged:: 1.2.0 | ||
|
||
Support for binary file objects was introduced. | ||
|
||
sep : str, default ',' | ||
String of length 1. Field delimiter for the output file. | ||
na_rep : str, default '' | ||
|
@@ -3056,7 +3061,8 @@ def to_csv( | |
Python write mode, default 'w'. | ||
encoding : str, optional | ||
A string representing the encoding to use in the output file, | ||
defaults to 'utf-8'. | ||
defaults to 'utf-8'. `encoding` is not supported if `path_or_buf` | ||
is a non-binary file object. | ||
compression : str or dict, default 'infer' | ||
If str, represents compression mode. If dict, value at 'method' is | ||
the compression mode. Compression mode may be any of the following | ||
|
@@ -3080,6 +3086,10 @@ def to_csv( | |
supported for compression modes 'gzip' and 'bz2' | ||
as well as 'zip'. | ||
|
||
.. versionchanged:: 1.2.0 | ||
|
||
Compression is supported for non-binary file objects. | ||
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. should have been "Compression is supported for binary file objects."! @jreback Should I create a one-line PR for this? 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. I opened a PR: #35615 |
||
|
||
quoting : optional constant from csv module | ||
Defaults to csv.QUOTE_MINIMAL. If you have set a `float_format` | ||
then floats are converted to strings and thus csv.QUOTE_NONNUMERIC | ||
|
Uh oh!
There was an error while loading. Please reload this page.