REF: move get_filepath_buffer into get_handle (#37639) · pandas-dev/pandas@6d1541e (original) (raw)
35 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1024,17 +1024,18 @@ Writing CSVs to binary file objects | ||
1024 | 1024 | |
1025 | 1025 | .. versionadded:: 1.2.0 |
1026 | 1026 | |
1027 | -``df.to_csv(..., mode="w+b")`` allows writing a CSV to a file object | |
1028 | -opened binary mode. For this to work, it is necessary that ``mode`` | |
1029 | -contains a "b": | |
1027 | +``df.to_csv(..., mode="wb")`` allows writing a CSV to a file object | |
1028 | +opened binary mode. In most cases, it is not necessary to specify | |
1029 | +``mode`` as Pandas will auto-detect whether the file object is | |
1030 | +opened in text or binary mode. | |
1030 | 1031 | |
1031 | 1032 | .. ipython:: python |
1032 | 1033 | |
1033 | 1034 | import io |
1034 | 1035 | |
1035 | 1036 | data = pd.DataFrame([0, 1, 2]) |
1036 | 1037 | buffer = io.BytesIO() |
1037 | - data.to_csv(buffer, mode="w+b", encoding="utf-8", compression="gzip") | |
1038 | + data.to_csv(buffer, encoding="utf-8", compression="gzip") | |
1038 | 1039 | |
1039 | 1040 | .. _io.float_precision: |
1040 | 1041 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -84,7 +84,8 @@ Support for binary file handles in ``to_csv`` | ||
84 | 84 | |
85 | 85 | :meth:`to_csv` supports file handles in binary mode (:issue:`19827` and :issue:`35058`) |
86 | 86 | with ``encoding`` (:issue:`13068` and :issue:`23854`) and ``compression`` (:issue:`22555`). |
87 | -``mode`` has to contain a ``b`` for binary handles to be supported. | |
87 | +If Pandas does not automatically detect whether the file handle is opened in binary or text mode, | |
88 | +it is necessary to provide ``mode="wb"``. | |
88 | 89 | |
89 | 90 | For example: |
90 | 91 | |
@@ -94,7 +95,7 @@ For example: | ||
94 | 95 | |
95 | 96 | data = pd.DataFrame([0, 1, 2]) |
96 | 97 | buffer = io.BytesIO() |
97 | - data.to_csv(buffer, mode="w+b", encoding="utf-8", compression="gzip") | |
98 | + data.to_csv(buffer, encoding="utf-8", compression="gzip") | |
98 | 99 | |
99 | 100 | Support for short caption and table position in ``to_latex`` |
100 | 101 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
@@ -515,6 +516,7 @@ I/O | ||
515 | 516 | - :func:`read_csv` was closing user-provided binary file handles when ``engine="c"`` and an ``encoding`` was requested (:issue:`36980`) |
516 | 517 | - Bug in :meth:`DataFrame.to_hdf` was not dropping missing rows with ``dropna=True`` (:issue:`35719`) |
517 | 518 | - Bug in :func:`read_html` was raising a ``TypeError`` when supplying a ``pathlib.Path`` argument to the ``io`` parameter (:issue:`37705`) |
519 | +- :meth:`to_excel` and :meth:`to_markdown` support writing to fsspec URLs such as S3 and Google Cloud Storage (:issue:`33987`) | |
518 | 520 | |
519 | 521 | Plotting |
520 | 522 | ^^^^^^^^ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -146,10 +146,5 @@ | ||
146 | 146 | CompressionOptions = Optional[Union[str, CompressionDict]] |
147 | 147 | |
148 | 148 | |
149 | -# let's bind types | |
150 | -ModeVar = TypeVar("ModeVar", str, None, Optional[str]) | |
151 | -EncodingVar = TypeVar("EncodingVar", str, None, Optional[str]) | |
152 | - | |
153 | - | |
154 | 149 | # type of float formatter in DataFrameFormatter |
155 | 150 | FloatFormatType = Union[str, Callable, "EngFormatter"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -157,7 +157,7 @@ | ||
157 | 157 | from pandas.core.series import Series |
158 | 158 | from pandas.core.sorting import get_group_index, lexsort_indexer, nargsort |
159 | 159 | |
160 | -from pandas.io.common import get_filepath_or_buffer | |
160 | +from pandas.io.common import get_handle | |
161 | 161 | from pandas.io.formats import console, format as fmt |
162 | 162 | from pandas.io.formats.info import DataFrameInfo |
163 | 163 | import pandas.plotting |
@@ -2301,10 +2301,10 @@ def to_markdown( | ||
2301 | 2301 | result = tabulate.tabulate(self, **kwargs) |
2302 | 2302 | if buf is None: |
2303 | 2303 | return result |
2304 | - ioargs = get_filepath_or_buffer(buf, mode=mode, storage_options=storage_options) | |
2305 | -assert not isinstance(ioargs.filepath_or_buffer, (str, mmap.mmap)) | |
2306 | -ioargs.filepath_or_buffer.writelines(result) | |
2307 | -ioargs.close() | |
2304 | + | |
2305 | +with get_handle(buf, mode, storage_options=storage_options) as handles: | |
2306 | + assert not isinstance(handles.handle, (str, mmap.mmap)) | |
2307 | + handles.handle.writelines(result) | |
2308 | 2308 | return None |
2309 | 2309 | |
2310 | 2310 | @deprecate_kwarg(old_arg_name="fname", new_arg_name="path") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3221,7 +3221,7 @@ def to_csv( | ||
3221 | 3221 | File path or object, if None is provided the result is returned as |
3222 | 3222 | a string. If a non-binary file object is passed, it should be opened |
3223 | 3223 | with `newline=''`, disabling universal newlines. If a binary |
3224 | - file object is passed, `mode` needs to contain a `'b'`. | |
3224 | + file object is passed, `mode` might need to contain a `'b'`. | |
3225 | 3225 | |
3226 | 3226 | .. versionchanged:: 0.24.0 |
3227 | 3227 |