Skip to content

Commit

Permalink
REF: move get_filepath_buffer into get_handle
Browse files Browse the repository at this point in the history
  • Loading branch information
twoertwein committed Nov 6, 2020
1 parent 8b05fe3 commit ed51677
Show file tree
Hide file tree
Showing 26 changed files with 327 additions and 435 deletions.
6 changes: 3 additions & 3 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,8 @@ 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``
``df.to_csv(..., mode="wb")`` allows writing a CSV to a file object
opened binary mode. In most cases, it is not necessary that ``mode``
contains a "b":

.. ipython:: python
Expand All @@ -1034,7 +1034,7 @@ contains a "b":
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
4 changes: 2 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,7 @@ 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.
Most binary handles are supported without having a ``b`` in ``mode``.

For example:

Expand All @@ -94,7 +94,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
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 @@ -158,7 +158,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 @@ -2297,10 +2297,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()
handles = get_handle(buf, mode, storage_options=storage_options)
assert not isinstance(handles.handle, (str, mmap.mmap))
handles.handle.writelines(result)
handles.close()
return None

@deprecate_kwarg(old_arg_name="fname", new_arg_name="path")
Expand Down

0 comments on commit ed51677

Please sign in to comment.