Skip to content

Commit c67da63

Browse files
ramvikramsDr-Irv
andauthored
GH: 624 - Added dtype_backend to all read_* functions (#655)
* Added `dtype_backend` to all functions * updated * Update test_io.py * Update test_io.py * Update test_io.py * Update test_io.py * Update test_io.py * Update test_io.py * Update test_io.py * Update test_io.py * Update tests/test_io.py Co-authored-by: Irv Lustig <[email protected]> * corrected the tests * Update series.pyi * added the comment from `test_read_swl_table` --------- Co-authored-by: Irv Lustig <[email protected]>
1 parent a28df91 commit c67da63

16 files changed

+171
-0
lines changed

pandas-stubs/core/generic.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from pandas._typing import (
2929
CSVQuoting,
3030
Dtype,
3131
DtypeArg,
32+
DtypeBackend,
3233
FilePath,
3334
FileWriteMode,
3435
FillnaOptions,
@@ -373,6 +374,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
373374
convert_string: _bool = ...,
374375
convert_integer: _bool = ...,
375376
convert_boolean: _bool = ...,
377+
dtype_backend: DtypeBackend = ...,
376378
) -> NDFrameT: ...
377379
def fillna(
378380
self,

pandas-stubs/core/series.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ from pandas._typing import (
9696
CategoryDtypeArg,
9797
ComplexDtypeArg,
9898
CompressionOptions,
99+
DtypeBackend,
99100
DtypeObj,
100101
FilePath,
101102
FillnaOptions,
@@ -1133,6 +1134,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
11331134
convert_string: _bool = ...,
11341135
convert_integer: _bool = ...,
11351136
convert_boolean: _bool = ...,
1137+
dtype_backend: DtypeBackend = ...,
11361138
) -> Series[S1]: ...
11371139
@overload
11381140
def ffill(

pandas-stubs/core/tools/numeric.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import numpy as np
77
import pandas as pd
88
from typing_extensions import TypeAlias
99

10+
from pandas._libs.lib import NoDefault
1011
from pandas._typing import (
12+
DtypeBackend,
1113
IgnoreRaiseCoerce,
1214
Scalar,
1315
npt,
@@ -20,22 +22,26 @@ def to_numeric(
2022
arg: Scalar,
2123
errors: Literal["raise", "coerce"] = ...,
2224
downcast: _Downcast = ...,
25+
dtype_backend: DtypeBackend | NoDefault = ...,
2326
) -> float: ...
2427
@overload
2528
def to_numeric(
2629
arg: Scalar,
2730
errors: Literal["ignore"],
2831
downcast: _Downcast = ...,
32+
dtype_backend: DtypeBackend | NoDefault = ...,
2933
) -> Scalar: ...
3034
@overload
3135
def to_numeric(
3236
arg: list | tuple | np.ndarray,
3337
errors: IgnoreRaiseCoerce = ...,
3438
downcast: _Downcast = ...,
39+
dtype_backend: DtypeBackend | NoDefault = ...,
3540
) -> npt.NDArray: ...
3641
@overload
3742
def to_numeric(
3843
arg: pd.Series,
3944
errors: IgnoreRaiseCoerce = ...,
4045
downcast: _Downcast = ...,
46+
dtype_backend: DtypeBackend | NoDefault = ...,
4147
) -> pd.Series: ...

pandas-stubs/io/clipboards.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ from typing import (
1212

1313
from pandas.core.frame import DataFrame
1414

15+
from pandas._libs.lib import NoDefault
1516
from pandas._typing import (
1617
CompressionOptions,
1718
CSVEngine,
1819
CSVQuoting,
1920
DtypeArg,
21+
DtypeBackend,
2022
ListLikeHashable,
2123
StorageOptions,
2224
UsecolsArgType,
@@ -28,6 +30,7 @@ from pandas.io.parsers import TextFileReader
2830
def read_clipboard(
2931
sep: str | None = ...,
3032
*,
33+
dtype_backend: DtypeBackend | NoDefault = ...,
3134
delimiter: str | None = ...,
3235
header: int | Sequence[int] | Literal["infer"] | None = ...,
3336
names: ListLikeHashable | None = ...,
@@ -85,6 +88,7 @@ def read_clipboard(
8588
def read_clipboard(
8689
sep: str | None = ...,
8790
*,
91+
dtype_backend: DtypeBackend | NoDefault = ...,
8892
delimiter: str | None = ...,
8993
header: int | Sequence[int] | Literal["infer"] | None = ...,
9094
names: ListLikeHashable | None = ...,
@@ -142,6 +146,7 @@ def read_clipboard(
142146
def read_clipboard(
143147
sep: str | None = ...,
144148
*,
149+
dtype_backend: DtypeBackend | NoDefault = ...,
145150
delimiter: str | None = ...,
146151
header: int | Sequence[int] | Literal["infer"] | None = ...,
147152
names: ListLikeHashable | None = ...,

pandas-stubs/io/excel/_base.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import pyxlsb.workbook
1919
from typing_extensions import Self
2020
from xlrd.book import Book
2121

22+
from pandas._libs.lib import NoDefault
2223
from pandas._typing import (
2324
Dtype,
25+
DtypeBackend,
2426
FilePath,
2527
ListLikeHashable,
2628
ReadBuffer,
@@ -66,6 +68,7 @@ def read_excel(
6668
comment: str | None = ...,
6769
skipfooter: int = ...,
6870
storage_options: StorageOptions = ...,
71+
dtype_backend: DtypeBackend | NoDefault = ...,
6972
) -> dict[int | str, DataFrame]: ...
7073
@overload
7174
def read_excel(
@@ -104,6 +107,7 @@ def read_excel(
104107
comment: str | None = ...,
105108
skipfooter: int = ...,
106109
storage_options: StorageOptions = ...,
110+
dtype_backend: DtypeBackend | NoDefault = ...,
107111
) -> DataFrame: ...
108112

109113
class ExcelWriter:

pandas-stubs/io/feather_format.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from pandas import DataFrame
22

3+
from pandas._libs.lib import NoDefault
34
from pandas._typing import (
5+
DtypeBackend,
46
FilePath,
57
HashableT,
68
ReadBuffer,
@@ -12,4 +14,5 @@ def read_feather(
1214
columns: list[HashableT] | None = ...,
1315
use_threads: bool = ...,
1416
storage_options: StorageOptions = ...,
17+
dtype_backend: DtypeBackend | NoDefault = ...,
1518
) -> DataFrame: ...

pandas-stubs/io/html.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ from typing import (
1212

1313
from pandas.core.frame import DataFrame
1414

15+
from pandas._libs.lib import NoDefault
1516
from pandas._typing import (
17+
DtypeBackend,
1618
FilePath,
1719
HashableT1,
1820
HashableT2,
@@ -49,4 +51,5 @@ def read_html(
4951
keep_default_na: bool = ...,
5052
displayed_only: bool = ...,
5153
extract_links: Literal["header", "footer", "body", "all"] | None = ...,
54+
dtype_backend: DtypeBackend | NoDefault = ...,
5255
) -> list[DataFrame]: ...

pandas-stubs/io/json/_json.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ from typing import (
99
from pandas.core.frame import DataFrame
1010
from pandas.core.series import Series
1111

12+
from pandas._libs.lib import NoDefault
1213
from pandas._typing import (
1314
CompressionOptions,
1415
DtypeArg,
16+
DtypeBackend,
1517
FilePath,
1618
HashableT,
1719
JsonFrameOrient,
@@ -43,6 +45,7 @@ def read_json(
4345
compression: CompressionOptions = ...,
4446
nrows: int | None = ...,
4547
storage_options: StorageOptions = ...,
48+
dtype_backend: DtypeBackend | NoDefault = ...,
4649
) -> JsonReader[Series]: ...
4750
@overload
4851
def read_json(
@@ -66,6 +69,7 @@ def read_json(
6669
compression: CompressionOptions = ...,
6770
nrows: int | None = ...,
6871
storage_options: StorageOptions = ...,
72+
dtype_backend: DtypeBackend | NoDefault = ...,
6973
) -> JsonReader[DataFrame]: ...
7074
@overload
7175
def read_json(
@@ -89,6 +93,7 @@ def read_json(
8993
compression: CompressionOptions = ...,
9094
nrows: int | None = ...,
9195
storage_options: StorageOptions = ...,
96+
dtype_backend: DtypeBackend | NoDefault = ...,
9297
) -> Series: ...
9398
@overload
9499
def read_json(
@@ -112,6 +117,7 @@ def read_json(
112117
compression: CompressionOptions = ...,
113118
nrows: int | None = ...,
114119
storage_options: StorageOptions = ...,
120+
dtype_backend: DtypeBackend | NoDefault = ...,
115121
) -> DataFrame: ...
116122

117123
class JsonReader(abc.Iterator, Generic[NDFrameT]):

pandas-stubs/io/orc.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ from typing import Any
22

33
from pandas import DataFrame
44

5+
from pandas._libs.lib import NoDefault
56
from pandas._typing import (
7+
DtypeBackend,
68
FilePath,
79
HashableT,
810
ReadBuffer,
@@ -11,5 +13,6 @@ from pandas._typing import (
1113
def read_orc(
1214
path: FilePath | ReadBuffer[bytes],
1315
columns: list[HashableT] | None = ...,
16+
dtype_backend: DtypeBackend | NoDefault = ...,
1417
**kwargs: Any,
1518
) -> DataFrame: ...

pandas-stubs/io/parsers/readers.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ from typing import (
1818
from pandas.core.frame import DataFrame
1919
from typing_extensions import Self
2020

21+
from pandas._libs.lib import NoDefault
2122
from pandas._typing import (
2223
CompressionOptions,
2324
CSVEngine,
2425
CSVQuoting,
2526
DtypeArg,
27+
DtypeBackend,
2628
FilePath,
2729
ListLikeHashable,
2830
ReadCsvBuffer,
@@ -91,6 +93,7 @@ def read_csv(
9193
memory_map: bool = ...,
9294
float_precision: Literal["high", "legacy", "round_trip"] | None = ...,
9395
storage_options: StorageOptions | None = ...,
96+
dtype_backend: DtypeBackend | NoDefault = ...,
9497
) -> TextFileReader: ...
9598
@overload
9699
def read_csv(
@@ -151,6 +154,7 @@ def read_csv(
151154
memory_map: bool = ...,
152155
float_precision: Literal["high", "legacy", "round_trip"] | None = ...,
153156
storage_options: StorageOptions | None = ...,
157+
dtype_backend: DtypeBackend | NoDefault = ...,
154158
) -> TextFileReader: ...
155159
@overload
156160
def read_csv(
@@ -211,6 +215,7 @@ def read_csv(
211215
memory_map: bool = ...,
212216
float_precision: Literal["high", "legacy", "round_trip"] | None = ...,
213217
storage_options: StorageOptions | None = ...,
218+
dtype_backend: DtypeBackend | NoDefault = ...,
214219
) -> DataFrame: ...
215220
@overload
216221
def read_table(
@@ -396,6 +401,7 @@ def read_fwf(
396401
colspecs: Sequence[tuple[int, int]] | Literal["infer"] | None = ...,
397402
widths: Sequence[int] | None = ...,
398403
infer_nrows: int = ...,
404+
dtype_backend: DtypeBackend | NoDefault = ...,
399405
iterator: Literal[True],
400406
chunksize: int | None = ...,
401407
**kwds: Any,
@@ -407,6 +413,7 @@ def read_fwf(
407413
colspecs: Sequence[tuple[int, int]] | Literal["infer"] | None = ...,
408414
widths: Sequence[int] | None = ...,
409415
infer_nrows: int = ...,
416+
dtype_backend: DtypeBackend | NoDefault = ...,
410417
iterator: bool = ...,
411418
chunksize: int,
412419
**kwds: Any,
@@ -418,6 +425,7 @@ def read_fwf(
418425
colspecs: Sequence[tuple[int, int]] | Literal["infer"] | None = ...,
419426
widths: Sequence[int] | None = ...,
420427
infer_nrows: int = ...,
428+
dtype_backend: DtypeBackend | NoDefault = ...,
421429
iterator: Literal[False] = ...,
422430
chunksize: None = ...,
423431
**kwds: Any,

pandas-stubs/io/spss.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from pandas.core.frame import DataFrame
22

3+
from pandas._libs.lib import NoDefault
34
from pandas._typing import (
5+
DtypeBackend,
46
FilePath,
57
HashableT,
68
)
@@ -9,4 +11,5 @@ def read_spss(
911
path: FilePath,
1012
usecols: list[HashableT] | None = ...,
1113
convert_categoricals: bool = ...,
14+
dtype_backend: DtypeBackend | NoDefault = ...,
1215
) -> DataFrame: ...

pandas-stubs/io/sql.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def read_sql_table(
4040
columns: list[str] | None = ...,
4141
*,
4242
chunksize: int,
43+
dtype_backend: DtypeBackend | NoDefault = ...,
4344
) -> Generator[DataFrame, None, None]: ...
4445
@overload
4546
def read_sql_table(
@@ -51,6 +52,7 @@ def read_sql_table(
5152
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
5253
columns: list[str] | None = ...,
5354
chunksize: None = ...,
55+
dtype_backend: DtypeBackend | NoDefault = ...,
5456
) -> DataFrame: ...
5557
@overload
5658
def read_sql_query(
@@ -63,6 +65,7 @@ def read_sql_query(
6365
*,
6466
chunksize: int,
6567
dtype: DtypeArg | None = ...,
68+
dtype_backend: DtypeBackend | NoDefault = ...,
6669
) -> Generator[DataFrame, None, None]: ...
6770
@overload
6871
def read_sql_query(
@@ -74,6 +77,7 @@ def read_sql_query(
7477
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
7578
chunksize: None = ...,
7679
dtype: DtypeArg | None = ...,
80+
dtype_backend: DtypeBackend | NoDefault = ...,
7781
) -> DataFrame: ...
7882
@overload
7983
def read_sql(

pandas-stubs/io/xml.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ from collections.abc import Sequence
22

33
from pandas.core.frame import DataFrame
44

5+
from pandas._libs.lib import NoDefault
56
from pandas._typing import (
67
CompressionOptions,
78
ConvertersArg,
89
DtypeArg,
10+
DtypeBackend,
911
FilePath,
1012
ParseDatesArg,
1113
ReadBuffer,
@@ -31,4 +33,5 @@ def read_xml(
3133
iterparse: dict[str, list[str]] | None = ...,
3234
compression: CompressionOptions = ...,
3335
storage_options: StorageOptions = ...,
36+
dtype_backend: DtypeBackend | NoDefault = ...,
3437
) -> DataFrame: ...

tests/test_frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,3 +2597,9 @@ def test_suffix_prefix_index() -> None:
25972597
check(
25982598
assert_type(df.add_prefix("_col", axis="columns"), pd.DataFrame), pd.DataFrame
25992599
)
2600+
2601+
2602+
def test_convert_dtypes_dtype_backend() -> None:
2603+
df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [3, 4, 5, 6]})
2604+
dfn = df.convert_dtypes(dtype_backend="numpy_nullable")
2605+
check(assert_type(dfn, pd.DataFrame), pd.DataFrame)

0 commit comments

Comments
 (0)