Skip to content

Commit cedd9c7

Browse files
authored
TYP: pandas/io annotations from pandas-stubs (#47831)
1 parent a7b4b81 commit cedd9c7

File tree

14 files changed

+270
-240
lines changed

14 files changed

+270
-240
lines changed

pandas/errors/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class AbstractMethodError(NotImplementedError):
189189
while keeping compatibility with Python 2 and Python 3.
190190
"""
191191

192-
def __init__(self, class_instance, methodtype="method") -> None:
192+
def __init__(self, class_instance, methodtype: str = "method") -> None:
193193
types = {"method", "classmethod", "staticmethod", "property"}
194194
if methodtype not in types:
195195
raise ValueError(

pandas/io/excel/_base.py

+34-19
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,18 @@ def read_excel(
359359
# sheet name is str or int -> DataFrame
360360
sheet_name: str | int,
361361
header: int | Sequence[int] | None = ...,
362-
names=...,
362+
names: list[str] | None = ...,
363363
index_col: int | Sequence[int] | None = ...,
364-
usecols=...,
364+
usecols: int
365+
| str
366+
| Sequence[int]
367+
| Sequence[str]
368+
| Callable[[str], bool]
369+
| None = ...,
365370
squeeze: bool | None = ...,
366371
dtype: DtypeArg | None = ...,
367372
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
368-
converters=...,
373+
converters: dict[str, Callable] | dict[int, Callable] | None = ...,
369374
true_values: Iterable[Hashable] | None = ...,
370375
false_values: Iterable[Hashable] | None = ...,
371376
skiprows: Sequence[int] | int | Callable[[int], object] | None = ...,
@@ -374,8 +379,8 @@ def read_excel(
374379
keep_default_na: bool = ...,
375380
na_filter: bool = ...,
376381
verbose: bool = ...,
377-
parse_dates=...,
378-
date_parser=...,
382+
parse_dates: list | dict | bool = ...,
383+
date_parser: Callable | None = ...,
379384
thousands: str | None = ...,
380385
decimal: str = ...,
381386
comment: str | None = ...,
@@ -393,13 +398,18 @@ def read_excel(
393398
# sheet name is list or None -> dict[IntStrT, DataFrame]
394399
sheet_name: list[IntStrT] | None,
395400
header: int | Sequence[int] | None = ...,
396-
names=...,
401+
names: list[str] | None = ...,
397402
index_col: int | Sequence[int] | None = ...,
398-
usecols=...,
403+
usecols: int
404+
| str
405+
| Sequence[int]
406+
| Sequence[str]
407+
| Callable[[str], bool]
408+
| None = ...,
399409
squeeze: bool | None = ...,
400410
dtype: DtypeArg | None = ...,
401411
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
402-
converters=...,
412+
converters: dict[str, Callable] | dict[int, Callable] | None = ...,
403413
true_values: Iterable[Hashable] | None = ...,
404414
false_values: Iterable[Hashable] | None = ...,
405415
skiprows: Sequence[int] | int | Callable[[int], object] | None = ...,
@@ -408,8 +418,8 @@ def read_excel(
408418
keep_default_na: bool = ...,
409419
na_filter: bool = ...,
410420
verbose: bool = ...,
411-
parse_dates=...,
412-
date_parser=...,
421+
parse_dates: list | dict | bool = ...,
422+
date_parser: Callable | None = ...,
413423
thousands: str | None = ...,
414424
decimal: str = ...,
415425
comment: str | None = ...,
@@ -428,13 +438,18 @@ def read_excel(
428438
io,
429439
sheet_name: str | int | list[IntStrT] | None = 0,
430440
header: int | Sequence[int] | None = 0,
431-
names=None,
441+
names: list[str] | None = None,
432442
index_col: int | Sequence[int] | None = None,
433-
usecols=None,
443+
usecols: int
444+
| str
445+
| Sequence[int]
446+
| Sequence[str]
447+
| Callable[[str], bool]
448+
| None = None,
434449
squeeze: bool | None = None,
435450
dtype: DtypeArg | None = None,
436451
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = None,
437-
converters=None,
452+
converters: dict[str, Callable] | dict[int, Callable] | None = None,
438453
true_values: Iterable[Hashable] | None = None,
439454
false_values: Iterable[Hashable] | None = None,
440455
skiprows: Sequence[int] | int | Callable[[int], object] | None = None,
@@ -443,8 +458,8 @@ def read_excel(
443458
keep_default_na: bool = True,
444459
na_filter: bool = True,
445460
verbose: bool = False,
446-
parse_dates=False,
447-
date_parser=None,
461+
parse_dates: list | dict | bool = False,
462+
date_parser: Callable | None = None,
448463
thousands: str | None = None,
449464
decimal: str = ".",
450465
comment: str | None = None,
@@ -687,8 +702,8 @@ def parse(
687702
nrows: int | None = None,
688703
na_values=None,
689704
verbose: bool = False,
690-
parse_dates=False,
691-
date_parser=None,
705+
parse_dates: list | dict | bool = False,
706+
date_parser: Callable | None = None,
692707
thousands: str | None = None,
693708
decimal: str = ".",
694709
comment: str | None = None,
@@ -1665,8 +1680,8 @@ def parse(
16651680
skiprows: Sequence[int] | int | Callable[[int], object] | None = None,
16661681
nrows: int | None = None,
16671682
na_values=None,
1668-
parse_dates=False,
1669-
date_parser=None,
1683+
parse_dates: list | dict | bool = False,
1684+
date_parser: Callable | None = None,
16701685
thousands: str | None = None,
16711686
comment: str | None = None,
16721687
skipfooter: int = 0,

pandas/io/formats/excel.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,9 @@ def get_formatted_cells(self) -> Iterable[ExcelCell]:
853853
def write(
854854
self,
855855
writer,
856-
sheet_name="Sheet1",
857-
startrow=0,
858-
startcol=0,
856+
sheet_name: str = "Sheet1",
857+
startrow: int = 0,
858+
startcol: int = 0,
859859
freeze_panes=None,
860860
engine=None,
861861
storage_options: StorageOptions = None,

pandas/io/formats/format.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
TYPE_CHECKING,
2121
Any,
2222
Callable,
23+
Final,
2324
Hashable,
2425
Iterable,
2526
Iterator,
@@ -117,7 +118,7 @@
117118
)
118119

119120

120-
common_docstring = """
121+
common_docstring: Final = """
121122
Parameters
122123
----------
123124
buf : str, Path or StringIO-like, optional, default None
@@ -190,7 +191,7 @@
190191
"unset",
191192
)
192193

193-
return_docstring = """
194+
return_docstring: Final = """
194195
Returns
195196
-------
196197
str or None

pandas/io/formats/html.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from textwrap import dedent
77
from typing import (
88
Any,
9+
Final,
910
Hashable,
1011
Iterable,
1112
Mapping,
@@ -39,7 +40,7 @@ class HTMLFormatter:
3940
and this class responsible for only producing html markup.
4041
"""
4142

42-
indent_delta = 2
43+
indent_delta: Final = 2
4344

4445
def __init__(
4546
self,

pandas/io/formats/style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3332,7 +3332,7 @@ def highlight_null(
33323332
color: str | None = None,
33333333
subset: Subset | None = None,
33343334
props: str | None = None,
3335-
null_color=lib.no_default,
3335+
null_color: str | lib.NoDefault = lib.no_default,
33363336
) -> Styler:
33373337
"""
33383338
Highlight missing values with a style.

pandas/io/html.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numbers
1111
import re
1212
from typing import (
13+
Iterable,
1314
Pattern,
1415
Sequence,
1516
cast,
@@ -971,7 +972,7 @@ def read_html(
971972
encoding: str | None = None,
972973
decimal: str = ".",
973974
converters: dict | None = None,
974-
na_values=None,
975+
na_values: Iterable[object] | None = None,
975976
keep_default_na: bool = True,
976977
displayed_only: bool = True,
977978
) -> list[DataFrame]:

pandas/io/json/_json.py

+34-30
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,16 @@ def obj_to_write(self) -> NDFrame | Mapping[IndexLabel, Any]:
365365
def read_json(
366366
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
367367
*,
368-
orient=...,
368+
orient: str | None = ...,
369369
typ: Literal["frame"] = ...,
370370
dtype: DtypeArg | None = ...,
371371
convert_axes=...,
372-
convert_dates=...,
372+
convert_dates: bool | list[str] = ...,
373373
keep_default_dates: bool = ...,
374374
numpy: bool = ...,
375375
precise_float: bool = ...,
376-
date_unit=...,
377-
encoding=...,
376+
date_unit: str | None = ...,
377+
encoding: str | None = ...,
378378
encoding_errors: str | None = ...,
379379
lines: bool = ...,
380380
chunksize: int,
@@ -389,16 +389,16 @@ def read_json(
389389
def read_json(
390390
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
391391
*,
392-
orient=...,
392+
orient: str | None = ...,
393393
typ: Literal["series"],
394394
dtype: DtypeArg | None = ...,
395395
convert_axes=...,
396-
convert_dates=...,
396+
convert_dates: bool | list[str] = ...,
397397
keep_default_dates: bool = ...,
398398
numpy: bool = ...,
399399
precise_float: bool = ...,
400-
date_unit=...,
401-
encoding=...,
400+
date_unit: str | None = ...,
401+
encoding: str | None = ...,
402402
encoding_errors: str | None = ...,
403403
lines: bool = ...,
404404
chunksize: int,
@@ -413,16 +413,16 @@ def read_json(
413413
def read_json(
414414
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
415415
*,
416-
orient=...,
416+
orient: str | None = ...,
417417
typ: Literal["series"],
418418
dtype: DtypeArg | None = ...,
419419
convert_axes=...,
420-
convert_dates=...,
420+
convert_dates: bool | list[str] = ...,
421421
keep_default_dates: bool = ...,
422422
numpy: bool = ...,
423423
precise_float: bool = ...,
424-
date_unit=...,
425-
encoding=...,
424+
date_unit: str | None = ...,
425+
encoding: str | None = ...,
426426
encoding_errors: str | None = ...,
427427
lines: bool = ...,
428428
chunksize: None = ...,
@@ -436,16 +436,16 @@ def read_json(
436436
@overload
437437
def read_json(
438438
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
439-
orient=...,
439+
orient: str | None = ...,
440440
typ: Literal["frame"] = ...,
441441
dtype: DtypeArg | None = ...,
442442
convert_axes=...,
443-
convert_dates=...,
443+
convert_dates: bool | list[str] = ...,
444444
keep_default_dates: bool = ...,
445445
numpy: bool = ...,
446446
precise_float: bool = ...,
447-
date_unit=...,
448-
encoding=...,
447+
date_unit: str | None = ...,
448+
encoding: str | None = ...,
449449
encoding_errors: str | None = ...,
450450
lines: bool = ...,
451451
chunksize: None = ...,
@@ -464,16 +464,16 @@ def read_json(
464464
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["path_or_buf"])
465465
def read_json(
466466
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
467-
orient=None,
467+
orient: str | None = None,
468468
typ: Literal["frame", "series"] = "frame",
469469
dtype: DtypeArg | None = None,
470470
convert_axes=None,
471-
convert_dates=True,
471+
convert_dates: bool | list[str] = True,
472472
keep_default_dates: bool = True,
473473
numpy: bool = False,
474474
precise_float: bool = False,
475-
date_unit=None,
476-
encoding=None,
475+
date_unit: str | None = None,
476+
encoding: str | None = None,
477477
encoding_errors: str | None = "strict",
478478
lines: bool = False,
479479
chunksize: int | None = None,
@@ -1009,11 +1009,11 @@ def __init__(
10091009
json,
10101010
orient,
10111011
dtype: DtypeArg | None = None,
1012-
convert_axes=True,
1013-
convert_dates=True,
1014-
keep_default_dates=False,
1015-
numpy=False,
1016-
precise_float=False,
1012+
convert_axes: bool = True,
1013+
convert_dates: bool | list[str] = True,
1014+
keep_default_dates: bool = False,
1015+
numpy: bool = False,
1016+
precise_float: bool = False,
10171017
date_unit=None,
10181018
) -> None:
10191019
self.json = json
@@ -1093,7 +1093,11 @@ def _try_convert_types(self):
10931093
raise AbstractMethodError(self)
10941094

10951095
def _try_convert_data(
1096-
self, name, data, use_dtypes: bool = True, convert_dates: bool = True
1096+
self,
1097+
name,
1098+
data,
1099+
use_dtypes: bool = True,
1100+
convert_dates: bool | list[str] = True,
10971101
):
10981102
"""
10991103
Try to parse a ndarray like into a column by inferring dtype.
@@ -1375,10 +1379,10 @@ def _try_convert_dates(self):
13751379
return
13761380

13771381
# our columns to parse
1378-
convert_dates = self.convert_dates
1379-
if convert_dates is True:
1380-
convert_dates = []
1381-
convert_dates = set(convert_dates)
1382+
convert_dates_list_bool = self.convert_dates
1383+
if isinstance(convert_dates_list_bool, bool):
1384+
convert_dates_list_bool = []
1385+
convert_dates = set(convert_dates_list_bool)
13821386

13831387
def is_ok(col) -> bool:
13841388
"""

pandas/io/parquet.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ def to_parquet(
444444

445445
@doc(storage_options=_shared_docs["storage_options"])
446446
def read_parquet(
447-
path,
447+
path: FilePath | ReadBuffer[bytes],
448448
engine: str = "auto",
449-
columns=None,
449+
columns: list[str] | None = None,
450450
storage_options: StorageOptions = None,
451451
use_nullable_dtypes: bool = False,
452452
**kwargs,

0 commit comments

Comments
 (0)