Skip to content

REF: move get_filepath_buffer into get_handle #37639

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 2 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1024,17 +1024,18 @@ Writing CSVs to binary file objects

.. versionadded:: 1.2.0

``df.to_csv(..., mode="w+b")`` allows writing a CSV to a file object
opened binary mode. For this to work, it is necessary that ``mode``
contains a "b":
``df.to_csv(..., mode="wb")`` allows writing a CSV to a file object
opened binary mode. In most cases, it is not necessary to specify
``mode`` as Pandas will auto-detect whether the file object is
Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't yet found an object that needs an explicit mode

opened in text or binary mode.

.. 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")
data.to_csv(buffer, encoding="utf-8", compression="gzip")

.. _io.float_precision:

Expand Down
6 changes: 4 additions & 2 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Support for binary file handles in ``to_csv``

: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.
If Pandas does not automatically detect whether the file handle is opened in binary or text mode,
it is necessary to provide ``mode="wb"``.

For example:

Expand All @@ -94,7 +95,7 @@ For example:

data = pd.DataFrame([0, 1, 2])
buffer = io.BytesIO()
data.to_csv(buffer, mode="w+b", encoding="utf-8", compression="gzip")
data.to_csv(buffer, encoding="utf-8", compression="gzip")

Support for short caption and table position in ``to_latex``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -514,6 +515,7 @@ I/O
- :func:`read_csv` was closing user-provided binary file handles when ``engine="c"`` and an ``encoding`` was requested (:issue:`36980`)
- Bug in :meth:`DataFrame.to_hdf` was not dropping missing rows with ``dropna=True`` (:issue:`35719`)
- Bug in :func:`read_html` was raising a ``TypeError`` when supplying a ``pathlib.Path`` argument to the ``io`` parameter (:issue:`37705`)
- :meth:`to_excel` and :meth:`to_markdown` support writing to fsspec URLs such as S3 and Google Cloud Storage (:issue:`33987`)

Plotting
^^^^^^^^
Expand Down
5 changes: 0 additions & 5 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,5 @@
CompressionOptions = Optional[Union[str, CompressionDict]]


# let's bind types
ModeVar = TypeVar("ModeVar", str, None, Optional[str])
EncodingVar = TypeVar("EncodingVar", str, None, Optional[str])


# type of float formatter in DataFrameFormatter
FloatFormatType = Union[str, Callable, "EngFormatter"]
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
from pandas.core.series import Series
from pandas.core.sorting import get_group_index, lexsort_indexer, nargsort

from pandas.io.common import get_filepath_or_buffer
from pandas.io.common import get_handle
from pandas.io.formats import console, format as fmt
from pandas.io.formats.info import DataFrameInfo
import pandas.plotting
Expand Down Expand Up @@ -2301,10 +2301,10 @@ def to_markdown(
result = tabulate.tabulate(self, **kwargs)
if buf is None:
return result
ioargs = get_filepath_or_buffer(buf, mode=mode, storage_options=storage_options)
assert not isinstance(ioargs.filepath_or_buffer, (str, mmap.mmap))
ioargs.filepath_or_buffer.writelines(result)
ioargs.close()

with get_handle(buf, mode, storage_options=storage_options) as handles:
assert not isinstance(handles.handle, (str, mmap.mmap))
handles.handle.writelines(result)
return None

@deprecate_kwarg(old_arg_name="fname", new_arg_name="path")
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3221,7 +3221,7 @@ def to_csv(
File path or object, if None is provided the result is returned as
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'`.
file object is passed, `mode` might need to contain a `'b'`.

.. versionchanged:: 0.24.0

Expand Down
Loading