Skip to content

Commit 3d351ed

Browse files
CLN: Add typing for dtype argument in io directory (GH38808) (#38814)
1 parent 8232658 commit 3d351ed

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

pandas/io/excel/_base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pandas._config import config
1313

1414
from pandas._libs.parsers import STR_NA_VALUES
15-
from pandas._typing import Buffer, FilePathOrBuffer, StorageOptions
15+
from pandas._typing import Buffer, DtypeArg, FilePathOrBuffer, StorageOptions
1616
from pandas.compat._optional import import_optional_dependency
1717
from pandas.errors import EmptyDataError
1818
from pandas.util._decorators import Appender, deprecate_nonkeyword_arguments, doc
@@ -309,7 +309,7 @@ def read_excel(
309309
index_col=None,
310310
usecols=None,
311311
squeeze=False,
312-
dtype=None,
312+
dtype: Optional[DtypeArg] = None,
313313
engine=None,
314314
converters=None,
315315
true_values=None,
@@ -433,7 +433,7 @@ def parse(
433433
index_col=None,
434434
usecols=None,
435435
squeeze=False,
436-
dtype=None,
436+
dtype: Optional[DtypeArg] = None,
437437
true_values=None,
438438
false_values=None,
439439
skiprows=None,

pandas/io/json/_json.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from pandas._libs.tslibs import iNaT
1212
from pandas._typing import (
1313
CompressionOptions,
14+
DtypeArg,
15+
FrameOrSeriesUnion,
1416
IndexLabel,
1517
JSONSerializable,
1618
StorageOptions,
@@ -296,7 +298,7 @@ def read_json(
296298
path_or_buf=None,
297299
orient=None,
298300
typ="frame",
299-
dtype=None,
301+
dtype: Optional[DtypeArg] = None,
300302
convert_axes=None,
301303
convert_dates=True,
302304
keep_default_dates: bool = True,
@@ -775,7 +777,7 @@ def __init__(
775777
self,
776778
json,
777779
orient,
778-
dtype=None,
780+
dtype: Optional[DtypeArg] = None,
779781
convert_axes=True,
780782
convert_dates=True,
781783
keep_default_dates=False,
@@ -809,7 +811,7 @@ def __init__(
809811
self.convert_dates = convert_dates
810812
self.date_unit = date_unit
811813
self.keep_default_dates = keep_default_dates
812-
self.obj = None
814+
self.obj: Optional[FrameOrSeriesUnion] = None
813815

814816
def check_keys_split(self, decoded):
815817
"""

pandas/io/parsers.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import pandas._libs.parsers as parsers
3232
from pandas._libs.parsers import STR_NA_VALUES
3333
from pandas._libs.tslibs import parsing
34-
from pandas._typing import FilePathOrBuffer, StorageOptions, Union
34+
from pandas._typing import DtypeArg, FilePathOrBuffer, StorageOptions, Union
3535
from pandas.errors import (
3636
AbstractMethodError,
3737
EmptyDataError,
@@ -546,7 +546,7 @@ def read_csv(
546546
prefix=None,
547547
mangle_dupe_cols=True,
548548
# General Parsing Configuration
549-
dtype=None,
549+
dtype: Optional[DtypeArg] = None,
550550
engine=None,
551551
converters=None,
552552
true_values=None,
@@ -626,7 +626,7 @@ def read_table(
626626
prefix=None,
627627
mangle_dupe_cols=True,
628628
# General Parsing Configuration
629-
dtype=None,
629+
dtype: Optional[DtypeArg] = None,
630630
engine=None,
631631
converters=None,
632632
true_values=None,
@@ -3502,25 +3502,22 @@ def _clean_index_names(columns, index_col, unnamed_cols):
35023502
return index_names, columns, index_col
35033503

35043504

3505-
def _get_empty_meta(columns, index_col, index_names, dtype=None):
3505+
def _get_empty_meta(columns, index_col, index_names, dtype: Optional[DtypeArg] = None):
35063506
columns = list(columns)
35073507

35083508
# Convert `dtype` to a defaultdict of some kind.
35093509
# This will enable us to write `dtype[col_name]`
35103510
# without worrying about KeyError issues later on.
3511-
if not isinstance(dtype, dict):
3511+
if not is_dict_like(dtype):
35123512
# if dtype == None, default will be object.
35133513
default_dtype = dtype or object
35143514
dtype = defaultdict(lambda: default_dtype)
35153515
else:
3516-
# Save a copy of the dictionary.
3517-
_dtype = dtype.copy()
3518-
dtype = defaultdict(lambda: object)
3519-
3520-
# Convert column indexes to column names.
3521-
for k, v in _dtype.items():
3522-
col = columns[k] if is_integer(k) else k
3523-
dtype[col] = v
3516+
dtype = cast(dict, dtype)
3517+
dtype = defaultdict(
3518+
lambda: object,
3519+
{columns[k] if is_integer(k) else k: v for k, v in dtype.items()},
3520+
)
35243521

35253522
# Even though we have no data, the "index" of the empty DataFrame
35263523
# could for example still be an empty MultiIndex. Thus, we need to

pandas/io/pytables.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929

3030
from pandas._libs import lib, writers as libwriters
3131
from pandas._libs.tslibs import timezones
32-
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label, Shape
32+
from pandas._typing import (
33+
ArrayLike,
34+
DtypeArg,
35+
FrameOrSeries,
36+
FrameOrSeriesUnion,
37+
Label,
38+
Shape,
39+
)
3340
from pandas.compat._optional import import_optional_dependency
3441
from pandas.compat.pickle_compat import patch_pickle
3542
from pandas.errors import PerformanceWarning
@@ -2259,7 +2266,7 @@ def __init__(
22592266
table=None,
22602267
meta=None,
22612268
metadata=None,
2262-
dtype=None,
2269+
dtype: Optional[DtypeArg] = None,
22632270
data=None,
22642271
):
22652272
super().__init__(

0 commit comments

Comments
 (0)