Skip to content

Commit 20e414a

Browse files
CLN: add typing to dtype arg in core/internals, core/reshape and core (GH38808) (pandas-dev#39030)
1 parent 83a12f5 commit 20e414a

File tree

6 files changed

+48
-26
lines changed

6 files changed

+48
-26
lines changed

pandas/core/base.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import numpy as np
2020

2121
import pandas._libs.lib as lib
22-
from pandas._typing import DtypeObj, IndexLabel
22+
from pandas._typing import Dtype, DtypeObj, IndexLabel
2323
from pandas.compat import PYPY
2424
from pandas.compat.numpy import function as nv
2525
from pandas.errors import AbstractMethodError
@@ -500,7 +500,13 @@ def array(self) -> ExtensionArray:
500500
"""
501501
raise AbstractMethodError(self)
502502

503-
def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
503+
def to_numpy(
504+
self,
505+
dtype: Optional[Dtype] = None,
506+
copy: bool = False,
507+
na_value=lib.no_default,
508+
**kwargs,
509+
):
504510
"""
505511
A NumPy ndarray representing the values in this Series or Index.
506512

pandas/core/generic.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737
from pandas._typing import (
3838
Axis,
3939
CompressionOptions,
40+
Dtype,
41+
DtypeArg,
4042
FilePathOrBuffer,
4143
FrameOrSeries,
4244
IndexKeyFunc,
4345
IndexLabel,
4446
JSONSerializable,
4547
Label,
4648
Level,
49+
NpDtype,
4750
Renamer,
4851
StorageOptions,
4952
TimedeltaConvertibleTypes,
@@ -210,7 +213,9 @@ def __init__(
210213
object.__setattr__(self, "_flags", Flags(self, allows_duplicate_labels=True))
211214

212215
@classmethod
213-
def _init_mgr(cls, mgr, axes, dtype=None, copy: bool = False) -> BlockManager:
216+
def _init_mgr(
217+
cls, mgr, axes, dtype: Optional[Dtype] = None, copy: bool = False
218+
) -> BlockManager:
214219
""" passed a manager and a axes dict """
215220
for a, axe in axes.items():
216221
if axe is not None:
@@ -1901,7 +1906,7 @@ def empty(self) -> bool_t:
19011906
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
19021907
__array_priority__ = 1000
19031908

1904-
def __array__(self, dtype=None) -> np.ndarray:
1909+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
19051910
return np.asarray(self._values, dtype=dtype)
19061911

19071912
def __array_wrap__(
@@ -2642,7 +2647,7 @@ def to_sql(
26422647
index: bool_t = True,
26432648
index_label=None,
26442649
chunksize=None,
2645-
dtype=None,
2650+
dtype: Optional[DtypeArg] = None,
26462651
method=None,
26472652
) -> None:
26482653
"""

pandas/core/internals/blocks.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from pandas._libs.internals import BlockPlacement
1717
from pandas._libs.tslibs import conversion
18-
from pandas._typing import ArrayLike, DtypeObj, Scalar, Shape
18+
from pandas._typing import ArrayLike, Dtype, DtypeObj, Scalar, Shape
1919
from pandas.util._validators import validate_bool_kwarg
2020

2121
from pandas.core.dtypes.cast import (
@@ -241,7 +241,7 @@ def array_values(self) -> ExtensionArray:
241241
"""
242242
return PandasArray(self.values)
243243

244-
def get_values(self, dtype=None):
244+
def get_values(self, dtype: Optional[Dtype] = None):
245245
"""
246246
return an internal format, currently just the ndarray
247247
this is often overridden to handle to_dense like operations
@@ -1669,7 +1669,7 @@ def setitem(self, indexer, value):
16691669
self.values[indexer] = value
16701670
return self
16711671

1672-
def get_values(self, dtype=None):
1672+
def get_values(self, dtype: Optional[Dtype] = None):
16731673
# ExtensionArrays must be iterable, so this works.
16741674
# TODO(EA2D): reshape not needed with 2D EAs
16751675
return np.asarray(self.values).reshape(self.shape)
@@ -1990,7 +1990,7 @@ class DatetimeLikeBlockMixin(Block):
19901990

19911991
_can_hold_na = True
19921992

1993-
def get_values(self, dtype=None):
1993+
def get_values(self, dtype: Optional[Dtype] = None):
19941994
"""
19951995
return object dtype as boxed values, such as Timestamps/Timedelta
19961996
"""
@@ -2168,7 +2168,7 @@ def is_view(self) -> bool:
21682168
# check the ndarray values of the DatetimeIndex values
21692169
return self.values._data.base is not None
21702170

2171-
def get_values(self, dtype=None):
2171+
def get_values(self, dtype: Optional[Dtype] = None):
21722172
"""
21732173
Returns an ndarray of values.
21742174
@@ -2449,7 +2449,7 @@ def replace(
24492449
# Constructor Helpers
24502450

24512451

2452-
def get_block_type(values, dtype=None):
2452+
def get_block_type(values, dtype: Optional[Dtype] = None):
24532453
"""
24542454
Find the appropriate Block subclass to use for the given values and dtype.
24552455
@@ -2464,7 +2464,7 @@ def get_block_type(values, dtype=None):
24642464
"""
24652465
# We use vtype and kind checks because they are much more performant
24662466
# than is_foo_dtype
2467-
dtype = dtype or values.dtype
2467+
dtype = cast(np.dtype, pandas_dtype(dtype) if dtype else values.dtype)
24682468
vtype = dtype.type
24692469
kind = dtype.kind
24702470

@@ -2500,7 +2500,7 @@ def get_block_type(values, dtype=None):
25002500
return cls
25012501

25022502

2503-
def make_block(values, placement, klass=None, ndim=None, dtype=None):
2503+
def make_block(values, placement, klass=None, ndim=None, dtype: Optional[Dtype] = None):
25042504
# Ensure that we don't allow PandasArray / PandasDtype in internals.
25052505
# For now, blocks should be backed by ndarrays when possible.
25062506
if isinstance(values, ABCPandasArray):

pandas/core/internals/managers.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import numpy as np
1818

1919
from pandas._libs import internals as libinternals, lib
20-
from pandas._typing import ArrayLike, DtypeObj, Label, Shape
20+
from pandas._typing import ArrayLike, Dtype, DtypeObj, Label, Shape
2121
from pandas.errors import PerformanceWarning
2222
from pandas.util._validators import validate_bool_kwarg
2323

@@ -816,7 +816,7 @@ def copy_func(ax):
816816
def as_array(
817817
self,
818818
transpose: bool = False,
819-
dtype=None,
819+
dtype: Optional[Dtype] = None,
820820
copy: bool = False,
821821
na_value=lib.no_default,
822822
) -> np.ndarray:
@@ -872,7 +872,9 @@ def as_array(
872872

873873
return arr.transpose() if transpose else arr
874874

875-
def _interleave(self, dtype=None, na_value=lib.no_default) -> np.ndarray:
875+
def _interleave(
876+
self, dtype: Optional[Dtype] = None, na_value=lib.no_default
877+
) -> np.ndarray:
876878
"""
877879
Return ndarray from blocks with specified item order
878880
Items must be contained in the blocks
@@ -1842,7 +1844,7 @@ def _simple_blockify(tuples, dtype) -> List[Block]:
18421844
return [block]
18431845

18441846

1845-
def _multi_blockify(tuples, dtype=None):
1847+
def _multi_blockify(tuples, dtype: Optional[Dtype] = None):
18461848
""" return an array of blocks that potentially have different dtypes """
18471849
# group by dtype
18481850
grouper = itertools.groupby(tuples, lambda x: x[2].dtype)

pandas/core/reshape/reshape.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas._libs.algos as libalgos
77
import pandas._libs.reshape as libreshape
88
from pandas._libs.sparse import IntIndex
9+
from pandas._typing import Dtype
910
from pandas.util._decorators import cache_readonly
1011

1112
from pandas.core.dtypes.cast import maybe_promote
@@ -732,11 +733,11 @@ def get_dummies(
732733
data,
733734
prefix=None,
734735
prefix_sep="_",
735-
dummy_na=False,
736+
dummy_na: bool = False,
736737
columns=None,
737-
sparse=False,
738-
drop_first=False,
739-
dtype=None,
738+
sparse: bool = False,
739+
drop_first: bool = False,
740+
dtype: Optional[Dtype] = None,
740741
) -> "DataFrame":
741742
"""
742743
Convert categorical variable into dummy/indicator variables.
@@ -921,7 +922,7 @@ def _get_dummies_1d(
921922
dummy_na=False,
922923
sparse=False,
923924
drop_first=False,
924-
dtype=None,
925+
dtype: Optional[Dtype] = None,
925926
):
926927
from pandas.core.reshape.concat import concat
927928

pandas/core/series.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
AggFuncType,
2929
ArrayLike,
3030
Axis,
31+
Dtype,
3132
DtypeObj,
3233
FrameOrSeriesUnion,
3334
IndexKeyFunc,
3435
Label,
36+
NpDtype,
3537
StorageOptions,
3638
ValueKeyFunc,
3739
)
@@ -214,7 +216,13 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
214216
# Constructors
215217

216218
def __init__(
217-
self, data=None, index=None, dtype=None, name=None, copy=False, fastpath=False
219+
self,
220+
data=None,
221+
index=None,
222+
dtype: Optional[Dtype] = None,
223+
name=None,
224+
copy: bool = False,
225+
fastpath: bool = False,
218226
):
219227

220228
if (
@@ -337,7 +345,7 @@ def __init__(
337345
self.name = name
338346
self._set_axis(0, index, fastpath=True)
339347

340-
def _init_dict(self, data, index=None, dtype=None):
348+
def _init_dict(self, data, index=None, dtype: Optional[Dtype] = None):
341349
"""
342350
Derive the "_mgr" and "index" attributes of a new Series from a
343351
dictionary input.
@@ -612,7 +620,7 @@ def __len__(self) -> int:
612620
"""
613621
return len(self._mgr)
614622

615-
def view(self, dtype=None) -> "Series":
623+
def view(self, dtype: Optional[Dtype] = None) -> "Series":
616624
"""
617625
Create a new view of the Series.
618626
@@ -686,7 +694,7 @@ def view(self, dtype=None) -> "Series":
686694
# NDArray Compat
687695
_HANDLED_TYPES = (Index, ExtensionArray, np.ndarray)
688696

689-
def __array__(self, dtype=None) -> np.ndarray:
697+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
690698
"""
691699
Return the values as a NumPy array.
692700

0 commit comments

Comments
 (0)