Skip to content

Commit 1293443

Browse files
bashtageKevin Sheppard
and
Kevin Sheppard
authored
ENH: Improve to_csv and from_{fwf,csv,table} (#248)
* ENH: Improve read_{csv/fwf/table} * MAINT: Move parsers->readers * ENH: Move complex type to _typing * MAINT: Correct typing issues * TYP: Remove None from quoting * CLN: Fix merge issues Co-authored-by: Kevin Sheppard <[email protected]>
1 parent 36b2ea5 commit 1293443

File tree

10 files changed

+672
-754
lines changed

10 files changed

+672
-754
lines changed

pandas-stubs/_typing.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from typing import (
1414
AnyStr,
1515
Callable,
1616
Hashable,
17+
Iterator,
1718
Literal,
1819
Mapping,
1920
Optional,
@@ -66,6 +67,12 @@ class BaseBuffer(Protocol): ...
6667
class ReadBuffer(BaseBuffer, Protocol[AnyStr_cov]): ...
6768
class WriteBuffer(BaseBuffer, Protocol[AnyStr_cov]): ...
6869

70+
class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol[AnyStr_cov]):
71+
def __iter__(self) -> Iterator[AnyStr_cov]: ...
72+
def readline(self) -> AnyStr_cov: ...
73+
@property
74+
def closed(self) -> bool: ...
75+
6976
class WriteExcelBuffer(WriteBuffer[bytes], Protocol):
7077
def truncate(self, size: Union[int, None] = ...) -> int: ...
7178

@@ -242,6 +249,7 @@ JsonSeriesOrient = Literal["split", "records", "index"]
242249
TimestampConvention = Literal["start", "end", "s", "e"]
243250

244251
CSVEngine = Literal["c", "python", "pyarrow", "python-fwf"]
252+
CSVQuoting = Literal[0, 1, 2, 3]
245253

246254
HDFCompLib = Literal["zlib", "lzo", "bzip2", "blosc"]
247255
ParquetEngine = Literal["auto", "pyarrow", "fastparquet"]

pandas-stubs/core/frame.pyi

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,55 +1917,6 @@ class DataFrame(NDFrame, OpsMixin):
19171917
self, excel: _bool = ..., sep: _str | None = ..., **kwargs
19181918
) -> None: ...
19191919
@overload
1920-
def to_csv(
1921-
self,
1922-
path_or_buf: FilePathOrBuffer | None,
1923-
sep: _str = ...,
1924-
na_rep: _str = ...,
1925-
float_format: _str | None = ...,
1926-
columns: Sequence[Hashable] | None = ...,
1927-
header: _bool | list[_str] = ...,
1928-
index: _bool = ...,
1929-
index_label: _bool | _str | Sequence[Hashable] | None = ...,
1930-
mode: _str = ...,
1931-
encoding: _str | None = ...,
1932-
compression: _str | Mapping[_str, _str] = ...,
1933-
quoting: Literal[0, 1, 2, 3] | None = ...,
1934-
quotechar: _str = ...,
1935-
line_terminator: _str | None = ...,
1936-
chunksize: int | None = ...,
1937-
date_format: _str | None = ...,
1938-
doublequote: _bool = ...,
1939-
escapechar: _str | None = ...,
1940-
decimal: _str = ...,
1941-
errors: _str = ...,
1942-
storage_options: dict[_str, Any] | None = ...,
1943-
) -> None: ...
1944-
@overload
1945-
def to_csv(
1946-
self,
1947-
sep: _str = ...,
1948-
na_rep: _str = ...,
1949-
float_format: _str | None = ...,
1950-
columns: Sequence[Hashable] | None = ...,
1951-
header: _bool | list[_str] = ...,
1952-
index: _bool = ...,
1953-
index_label: _bool | _str | Sequence[Hashable] | None = ...,
1954-
mode: _str = ...,
1955-
encoding: _str | None = ...,
1956-
compression: _str | Mapping[_str, _str] = ...,
1957-
quoting: Literal[0, 1, 2, 3] | None = ...,
1958-
quotechar: _str = ...,
1959-
line_terminator: _str | None = ...,
1960-
chunksize: int | None = ...,
1961-
date_format: _str | None = ...,
1962-
doublequote: _bool = ...,
1963-
escapechar: _str | None = ...,
1964-
decimal: _str = ...,
1965-
errors: _str = ...,
1966-
storage_options: dict[_str, Any] | None = ...,
1967-
) -> _str: ...
1968-
@overload
19691920
def to_json(
19701921
self,
19711922
path_or_buf: FilePathOrBuffer | None,

pandas-stubs/core/generic.pyi

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ from pandas._typing import (
2222
ArrayLike,
2323
Axis,
2424
CompressionOptions,
25+
CSVQuoting,
2526
Dtype,
2627
DtypeArg,
2728
FilePath,
@@ -231,18 +232,18 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
231232
@overload
232233
def to_csv(
233234
self,
234-
path_or_buf: FilePathOrBuffer | None,
235+
path_or_buf: FilePathOrBuffer,
235236
sep: _str = ...,
236237
na_rep: _str = ...,
237-
float_format: _str | None = ...,
238-
columns: Sequence[Hashable] | None = ...,
238+
float_format: _str | Callable[[object], _str] | None = ...,
239+
columns: list[HashableT] | None = ...,
239240
header: _bool | list[_str] = ...,
240241
index: _bool = ...,
241-
index_label: _bool | _str | Sequence[Hashable] | None = ...,
242-
mode: _str = ...,
242+
index_label: Literal[False] | _str | list[HashableT] | None = ...,
243+
mode: FileWriteMode = ...,
243244
encoding: _str | None = ...,
244-
compression: _str | Mapping[_str, _str] = ...,
245-
quoting: Literal[0, 1, 2, 3] | None = ...,
245+
compression: CompressionOptions = ...,
246+
quoting: CSVQuoting = ...,
246247
quotechar: _str = ...,
247248
line_terminator: _str | None = ...,
248249
chunksize: int | None = ...,
@@ -251,22 +252,23 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
251252
escapechar: _str | None = ...,
252253
decimal: _str = ...,
253254
errors: _str = ...,
254-
storage_options: dict[_str, Any] | None = ...,
255+
storage_options: StorageOptions = ...,
255256
) -> None: ...
256257
@overload
257258
def to_csv(
258259
self,
260+
path_or_buf: None = ...,
259261
sep: _str = ...,
260262
na_rep: _str = ...,
261-
float_format: _str | None = ...,
262-
columns: Sequence[Hashable] | None = ...,
263+
float_format: _str | Callable[[object], _str] | None = ...,
264+
columns: list[HashableT] | None = ...,
263265
header: _bool | list[_str] = ...,
264266
index: _bool = ...,
265-
index_label: _bool | _str | Sequence[Hashable] | None = ...,
266-
mode: _str = ...,
267+
index_label: Literal[False] | _str | list[HashableT] | None = ...,
268+
mode: FileWriteMode = ...,
267269
encoding: _str | None = ...,
268-
compression: _str | Mapping[_str, _str] = ...,
269-
quoting: Literal[0, 1, 2, 3] | None = ...,
270+
compression: CompressionOptions = ...,
271+
quoting: CSVQuoting = ...,
270272
quotechar: _str = ...,
271273
line_terminator: _str | None = ...,
272274
chunksize: int | None = ...,
@@ -275,7 +277,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
275277
escapechar: _str | None = ...,
276278
decimal: _str = ...,
277279
errors: _str = ...,
278-
storage_options: dict[_str, Any] | None = ...,
280+
storage_options: StorageOptions = ...,
279281
) -> _str: ...
280282
def take(
281283
self, indices, axis=..., is_copy: _bool | None = ..., **kwargs

pandas-stubs/io/clipboards.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from pandas.core.series import Series
1515
from pandas._typing import (
1616
CompressionOptions,
1717
CSVEngine,
18+
CSVQuoting,
1819
DtypeArg,
1920
StorageOptions,
2021
npt,
@@ -71,7 +72,7 @@ def read_clipboard(
7172
decimal: str = ...,
7273
lineterminator: str | None = ...,
7374
quotechar: str = ...,
74-
quoting: Literal[0, 1, 2, 3] | None = ...,
75+
quoting: CSVQuoting = ...,
7576
doublequote: bool = ...,
7677
escapechar: str | None = ...,
7778
comment: str | None = ...,
@@ -137,7 +138,7 @@ def read_clipboard(
137138
decimal: str = ...,
138139
lineterminator: str | None = ...,
139140
quotechar: str = ...,
140-
quoting: Literal[0, 1, 2, 3] | None = ...,
141+
quoting: CSVQuoting = ...,
141142
doublequote: bool = ...,
142143
escapechar: str | None = ...,
143144
comment: str | None = ...,
@@ -203,7 +204,7 @@ def read_clipboard(
203204
decimal: str = ...,
204205
lineterminator: str | None = ...,
205206
quotechar: str = ...,
206-
quoting: Literal[0, 1, 2, 3] | None = ...,
207+
quoting: CSVQuoting = ...,
207208
doublequote: bool = ...,
208209
escapechar: str | None = ...,
209210
comment: str | None = ...,

pandas-stubs/io/common.pyi

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
from typing import (
2-
IO,
3-
AnyStr,
4-
Generic,
5-
)
6-
7-
from pandas._typing import CompressionDict
8-
9-
class IOHandles(Generic[AnyStr]):
10-
handle: IO[AnyStr]
11-
compression: CompressionDict
12-
created_handles: list[IO[AnyStr]]
13-
is_wrapped: bool
14-
def close(self) -> None: ...
15-
def __enter__(self) -> IOHandles[AnyStr]: ...
16-
def __exit__(self, *args: object) -> None: ...
17-
def __init__(self, handle, compression, created_handles, is_wrapped) -> None: ...
1+
from typing import (
2+
IO,
3+
AnyStr,
4+
Generic,
5+
)
6+
7+
from pandas._typing import CompressionDict
8+
9+
class IOHandles(Generic[AnyStr]):
10+
handle: IO[AnyStr]
11+
compression: CompressionDict
12+
created_handles: list[IO[AnyStr]]
13+
is_wrapped: bool
14+
def close(self) -> None: ...
15+
def __enter__(self) -> IOHandles[AnyStr]: ...
16+
def __exit__(self, *args: object) -> None: ...
17+
def __init__(self, handle, compression, created_handles, is_wrapped) -> None: ...

0 commit comments

Comments
 (0)