16
16
import functools
17
17
from io import StringIO
18
18
import itertools
19
- import mmap
20
19
from textwrap import dedent
21
20
from typing import (
22
21
IO ,
55
54
CompressionOptions ,
56
55
Dtype ,
57
56
DtypeObj ,
58
- FilePathOrBuffer ,
57
+ FilePath ,
59
58
FillnaOptions ,
60
59
FloatFormatType ,
61
60
FormattersType ,
71
70
TimedeltaConvertibleTypes ,
72
71
TimestampConvertibleTypes ,
73
72
ValueKeyFunc ,
73
+ WriteBuffer ,
74
74
npt ,
75
75
)
76
76
from pandas .compat ._optional import import_optional_dependency
@@ -1056,7 +1056,7 @@ def _repr_html_(self) -> str | None:
1056
1056
@Substitution (shared_params = fmt .common_docstring , returns = fmt .return_docstring )
1057
1057
def to_string (
1058
1058
self ,
1059
- buf : FilePathOrBuffer [str ] | None = None ,
1059
+ buf : FilePath | WriteBuffer [str ] | None = None ,
1060
1060
columns : Sequence [str ] | None = None ,
1061
1061
col_space : int | None = None ,
1062
1062
header : bool | Sequence [str ] = True ,
@@ -2432,7 +2432,7 @@ def _from_arrays(
2432
2432
@deprecate_kwarg (old_arg_name = "fname" , new_arg_name = "path" )
2433
2433
def to_stata (
2434
2434
self ,
2435
- path : FilePathOrBuffer ,
2435
+ path : FilePath | WriteBuffer [ bytes ] ,
2436
2436
convert_dates : dict [Hashable , str ] | None = None ,
2437
2437
write_index : bool = True ,
2438
2438
byteorder : str | None = None ,
@@ -2454,11 +2454,9 @@ def to_stata(
2454
2454
2455
2455
Parameters
2456
2456
----------
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.
2462
2460
2463
2461
.. versionchanged:: 1.0.0
2464
2462
@@ -2600,14 +2598,16 @@ def to_stata(
2600
2598
writer .write_file ()
2601
2599
2602
2600
@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 :
2604
2602
"""
2605
2603
Write a DataFrame to the binary Feather format.
2606
2604
2607
2605
Parameters
2608
2606
----------
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.
2611
2611
**kwargs :
2612
2612
Additional keywords passed to :func:`pyarrow.feather.write_feather`.
2613
2613
Starting with pyarrow 0.17, this includes the `compression`,
@@ -2677,15 +2677,14 @@ def to_markdown(
2677
2677
return result
2678
2678
2679
2679
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 )
2682
2681
return None
2683
2682
2684
2683
@doc (storage_options = generic ._shared_docs ["storage_options" ])
2685
2684
@deprecate_kwarg (old_arg_name = "fname" , new_arg_name = "path" )
2686
2685
def to_parquet (
2687
2686
self ,
2688
- path : FilePathOrBuffer | None = None ,
2687
+ path : FilePath | WriteBuffer [ bytes ] | None = None ,
2689
2688
engine : str = "auto" ,
2690
2689
compression : str | None = "snappy" ,
2691
2690
index : bool | None = None ,
@@ -2703,13 +2702,11 @@ def to_parquet(
2703
2702
2704
2703
Parameters
2705
2704
----------
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.
2713
2710
2714
2711
.. versionchanged:: 1.2.0
2715
2712
@@ -2804,7 +2801,7 @@ def to_parquet(
2804
2801
@Substitution (shared_params = fmt .common_docstring , returns = fmt .return_docstring )
2805
2802
def to_html (
2806
2803
self ,
2807
- buf : FilePathOrBuffer [str ] | None = None ,
2804
+ buf : FilePath | WriteBuffer [str ] | None = None ,
2808
2805
columns : Sequence [str ] | None = None ,
2809
2806
col_space : ColspaceArgType | None = None ,
2810
2807
header : bool | Sequence [str ] = True ,
@@ -2891,7 +2888,7 @@ def to_html(
2891
2888
@doc (storage_options = generic ._shared_docs ["storage_options" ])
2892
2889
def to_xml (
2893
2890
self ,
2894
- path_or_buffer : FilePathOrBuffer | None = None ,
2891
+ path_or_buffer : FilePath | WriteBuffer [ bytes ] | WriteBuffer [ str ] | None = None ,
2895
2892
index : bool = True ,
2896
2893
root_name : str | None = "data" ,
2897
2894
row_name : str | None = "row" ,
@@ -2904,7 +2901,7 @@ def to_xml(
2904
2901
xml_declaration : bool | None = True ,
2905
2902
pretty_print : bool | None = True ,
2906
2903
parser : str | None = "lxml" ,
2907
- stylesheet : FilePathOrBuffer | None = None ,
2904
+ stylesheet : FilePath | WriteBuffer [ bytes ] | WriteBuffer [ str ] | None = None ,
2908
2905
compression : CompressionOptions = "infer" ,
2909
2906
storage_options : StorageOptions = None ,
2910
2907
) -> str | None :
@@ -2915,9 +2912,10 @@ def to_xml(
2915
2912
2916
2913
Parameters
2917
2914
----------
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.
2921
2919
index : bool, default True
2922
2920
Whether to include index in XML document.
2923
2921
root_name : str, default 'data'
@@ -3211,7 +3209,7 @@ def to_xml(
3211
3209
def info (
3212
3210
self ,
3213
3211
verbose : bool | None = None ,
3214
- buf : IO [str ] | None = None ,
3212
+ buf : WriteBuffer [str ] | None = None ,
3215
3213
max_cols : int | None = None ,
3216
3214
memory_usage : bool | str | None = None ,
3217
3215
show_counts : bool | None = None ,
0 commit comments