From 2e27204c67a096221252648168a075a90810027a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 9 Jun 2021 13:09:52 -0400 Subject: [PATCH 1/2] TYP: to_csv accepts IO[bytes] and fix FilePathOrBuffer --- pandas/_typing.py | 4 ++-- pandas/core/generic.py | 2 +- pandas/io/formats/csvs.py | 2 +- pandas/io/formats/format.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 7763b0ceb610a..12d23786c3387 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -170,8 +170,8 @@ # filenames and file-like-objects Buffer = Union[IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap] -FileOrBuffer = Union[str, Buffer[T]] -FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[T]] +FileOrBuffer = Union[str, Buffer[AnyStr]] +FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[AnyStr]] # for arbitrary kwargs passed during reading/writing files StorageOptions = Optional[Dict[str, Any]] diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1de19fceff258..93f2e0da0c5c5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3295,7 +3295,7 @@ def to_latex( @doc(storage_options=_shared_docs["storage_options"]) def to_csv( self, - path_or_buf: FilePathOrBuffer | None = None, + path_or_buf: FilePathOrBuffer[str] | FilePathOrBuffer[bytes] | None = None, sep: str = ",", na_rep: str = "", float_format: str | None = None, diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 915a17fc702c3..f078975e4b85a 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -48,7 +48,7 @@ class CSVFormatter: def __init__( self, formatter: DataFrameFormatter, - path_or_buf: FilePathOrBuffer[str] = "", + path_or_buf: FilePathOrBuffer[str] | FilePathOrBuffer[bytes] = "", sep: str = ",", cols: Sequence[Hashable] | None = None, index_label: IndexLabel | None = None, diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 32a5cda4f4083..d24f47eafa95e 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1054,7 +1054,7 @@ def to_string( def to_csv( self, - path_or_buf: FilePathOrBuffer[str] | None = None, + path_or_buf: FilePathOrBuffer[str] | FilePathOrBuffer[bytes] | None = None, encoding: str | None = None, sep: str = ",", columns: Sequence[Hashable] | None = None, From 8324994a4ad7f06c733a268853c815d27b04f194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 10 Jun 2021 15:14:44 -0400 Subject: [PATCH 2/2] use AnyStr --- pandas/core/generic.py | 3 ++- pandas/io/formats/csvs.py | 3 ++- pandas/io/formats/format.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 93f2e0da0c5c5..99e4888d08be6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -11,6 +11,7 @@ from typing import ( TYPE_CHECKING, Any, + AnyStr, Callable, Hashable, Mapping, @@ -3295,7 +3296,7 @@ def to_latex( @doc(storage_options=_shared_docs["storage_options"]) def to_csv( self, - path_or_buf: FilePathOrBuffer[str] | FilePathOrBuffer[bytes] | None = None, + path_or_buf: FilePathOrBuffer[AnyStr] | None = None, sep: str = ",", na_rep: str = "", float_format: str | None = None, diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index f078975e4b85a..e9af2f6676092 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -9,6 +9,7 @@ from typing import ( TYPE_CHECKING, Any, + AnyStr, Hashable, Iterator, Sequence, @@ -48,7 +49,7 @@ class CSVFormatter: def __init__( self, formatter: DataFrameFormatter, - path_or_buf: FilePathOrBuffer[str] | FilePathOrBuffer[bytes] = "", + path_or_buf: FilePathOrBuffer[AnyStr] = "", sep: str = ",", cols: Sequence[Hashable] | None = None, index_label: IndexLabel | None = None, diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index d24f47eafa95e..d1c19f348f901 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -19,6 +19,7 @@ IO, TYPE_CHECKING, Any, + AnyStr, Callable, Hashable, Iterable, @@ -1054,7 +1055,7 @@ def to_string( def to_csv( self, - path_or_buf: FilePathOrBuffer[str] | FilePathOrBuffer[bytes] | None = None, + path_or_buf: FilePathOrBuffer[AnyStr] | None = None, encoding: str | None = None, sep: str = ",", columns: Sequence[Hashable] | None = None,