1616import functools
1717from io import StringIO
1818import itertools
19- import mmap
2019from textwrap import dedent
2120from typing import (
2221 IO ,
5554 CompressionOptions ,
5655 Dtype ,
5756 DtypeObj ,
58- FilePathOrBuffer ,
57+ FilePath ,
5958 FillnaOptions ,
6059 FloatFormatType ,
6160 FormattersType ,
7170 TimedeltaConvertibleTypes ,
7271 TimestampConvertibleTypes ,
7372 ValueKeyFunc ,
73+ WriteBuffer ,
7474 npt ,
7575)
7676from pandas .compat ._optional import import_optional_dependency
@@ -1056,7 +1056,7 @@ def _repr_html_(self) -> str | None:
10561056 @Substitution (shared_params = fmt .common_docstring , returns = fmt .return_docstring )
10571057 def to_string (
10581058 self ,
1059- buf : FilePathOrBuffer [str ] | None = None ,
1059+ buf : FilePath | WriteBuffer [str ] | None = None ,
10601060 columns : Sequence [str ] | None = None ,
10611061 col_space : int | None = None ,
10621062 header : bool | Sequence [str ] = True ,
@@ -2432,7 +2432,7 @@ def _from_arrays(
24322432 @deprecate_kwarg (old_arg_name = "fname" , new_arg_name = "path" )
24332433 def to_stata (
24342434 self ,
2435- path : FilePathOrBuffer ,
2435+ path : FilePath | WriteBuffer [ bytes ] ,
24362436 convert_dates : dict [Hashable , str ] | None = None ,
24372437 write_index : bool = True ,
24382438 byteorder : str | None = None ,
@@ -2454,11 +2454,9 @@ def to_stata(
24542454
24552455 Parameters
24562456 ----------
2457- path : str, buffer or path object
2458- String, path object (pathlib.Path or py._path.local.LocalPath) or
2459- object implementing a binary write() function. If using a buffer
2460- then the buffer will not be automatically closed after the file
2461- data has been written.
2457+ path : str, path object, or buffer
2458+ String, path object (implementing ``os.PathLike[str]``), or file-like
2459+ object implementing a binary ``write()`` function.
24622460
24632461 .. versionchanged:: 1.0.0
24642462
@@ -2600,14 +2598,16 @@ def to_stata(
26002598 writer .write_file ()
26012599
26022600 @deprecate_kwarg (old_arg_name = "fname" , new_arg_name = "path" )
2603- def to_feather (self , path : FilePathOrBuffer [bytes ], ** kwargs ) -> None :
2601+ def to_feather (self , path : FilePath | WriteBuffer [bytes ], ** kwargs ) -> None :
26042602 """
26052603 Write a DataFrame to the binary Feather format.
26062604
26072605 Parameters
26082606 ----------
2609- path : str or file-like object
2610- If a string, it will be used as Root Directory path.
2607+ path : str, path object, file-like object
2608+ String, path object (implementing ``os.PathLike[str]``), or file-like
2609+ object implementing a binary ``write()`` function. If a string or a path,
2610+ it will be used as Root Directory path when writing a partitioned dataset.
26112611 **kwargs :
26122612 Additional keywords passed to :func:`pyarrow.feather.write_feather`.
26132613 Starting with pyarrow 0.17, this includes the `compression`,
@@ -2677,15 +2677,14 @@ def to_markdown(
26772677 return result
26782678
26792679 with get_handle (buf , mode , storage_options = storage_options ) as handles :
2680- assert not isinstance (handles .handle , (str , mmap .mmap ))
2681- handles .handle .writelines (result )
2680+ handles .handle .write (result )
26822681 return None
26832682
26842683 @doc (storage_options = generic ._shared_docs ["storage_options" ])
26852684 @deprecate_kwarg (old_arg_name = "fname" , new_arg_name = "path" )
26862685 def to_parquet (
26872686 self ,
2688- path : FilePathOrBuffer | None = None ,
2687+ path : FilePath | WriteBuffer [ bytes ] | None = None ,
26892688 engine : str = "auto" ,
26902689 compression : str | None = "snappy" ,
26912690 index : bool | None = None ,
@@ -2703,13 +2702,11 @@ def to_parquet(
27032702
27042703 Parameters
27052704 ----------
2706- path : str or file-like object, default None
2707- If a string, it will be used as Root Directory path
2708- when writing a partitioned dataset. By file-like object,
2709- we refer to objects with a write() method, such as a file handle
2710- (e.g. via builtin open function) or io.BytesIO. The engine
2711- fastparquet does not accept file-like objects. If path is None,
2712- a bytes object is returned.
2705+ path : str, path object, file-like object, or None, default None
2706+ String, path object (implementing ``os.PathLike[str]``), or file-like
2707+ object implementing a binary ``write()`` function. If None, the result is
2708+ returned as bytes. If a string or path, it will be used as Root Directory
2709+ path when writing a partitioned dataset.
27132710
27142711 .. versionchanged:: 1.2.0
27152712
@@ -2804,7 +2801,7 @@ def to_parquet(
28042801 @Substitution (shared_params = fmt .common_docstring , returns = fmt .return_docstring )
28052802 def to_html (
28062803 self ,
2807- buf : FilePathOrBuffer [str ] | None = None ,
2804+ buf : FilePath | WriteBuffer [str ] | None = None ,
28082805 columns : Sequence [str ] | None = None ,
28092806 col_space : ColspaceArgType | None = None ,
28102807 header : bool | Sequence [str ] = True ,
@@ -2891,7 +2888,7 @@ def to_html(
28912888 @doc (storage_options = generic ._shared_docs ["storage_options" ])
28922889 def to_xml (
28932890 self ,
2894- path_or_buffer : FilePathOrBuffer | None = None ,
2891+ path_or_buffer : FilePath | WriteBuffer [ bytes ] | WriteBuffer [ str ] | None = None ,
28952892 index : bool = True ,
28962893 root_name : str | None = "data" ,
28972894 row_name : str | None = "row" ,
@@ -2904,7 +2901,7 @@ def to_xml(
29042901 xml_declaration : bool | None = True ,
29052902 pretty_print : bool | None = True ,
29062903 parser : str | None = "lxml" ,
2907- stylesheet : FilePathOrBuffer | None = None ,
2904+ stylesheet : FilePath | WriteBuffer [ bytes ] | WriteBuffer [ str ] | None = None ,
29082905 compression : CompressionOptions = "infer" ,
29092906 storage_options : StorageOptions = None ,
29102907 ) -> str | None :
@@ -2915,9 +2912,10 @@ def to_xml(
29152912
29162913 Parameters
29172914 ----------
2918- path_or_buffer : str, path object or file-like object, optional
2919- File to write output to. If None, the output is returned as a
2920- string.
2915+ path_or_buffer : str, path object, file-like object, or None, default None
2916+ String, path object (implementing ``os.PathLike[str]``), or file-like
2917+ object implementing a ``write()`` function. If None, the result is returned
2918+ as a string.
29212919 index : bool, default True
29222920 Whether to include index in XML document.
29232921 root_name : str, default 'data'
@@ -3211,7 +3209,7 @@ def to_xml(
32113209 def info (
32123210 self ,
32133211 verbose : bool | None = None ,
3214- buf : IO [str ] | None = None ,
3212+ buf : WriteBuffer [str ] | None = None ,
32153213 max_cols : int | None = None ,
32163214 memory_usage : bool | str | None = None ,
32173215 show_counts : bool | None = None ,
0 commit comments