Skip to content

Commit c83a628

Browse files
Merge remote-tracking branch 'upstream/master' into extensiontyping
2 parents 3ea2420 + 548f44f commit c83a628

File tree

11 files changed

+75
-184
lines changed

11 files changed

+75
-184
lines changed

pandas/_libs/parsers.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ cdef class TextReader:
337337
object skiprows
338338
object dtype
339339
object usecols
340-
list dtype_cast_order
340+
list dtype_cast_order # list[np.dtype]
341341
set unnamed_cols
342342
set noconvert
343343

pandas/_testing/asserters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ def assert_series_equal(
980980
left_values = left._values
981981
right_values = right._values
982982
# Only check exact if dtype is numeric
983-
if is_extension_array_dtype(left_values) and is_extension_array_dtype(
984-
right_values
983+
if isinstance(left_values, ExtensionArray) and isinstance(
984+
right_values, ExtensionArray
985985
):
986986
assert_extension_array_equal(
987987
left_values,

pandas/core/algorithms.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,38 +237,26 @@ def _reconstruct_data(
237237
# Catch DatetimeArray/TimedeltaArray
238238
return values
239239

240-
if is_extension_array_dtype(dtype):
241-
# error: Item "dtype[Any]" of "Union[dtype[Any], ExtensionDtype]" has no
242-
# attribute "construct_array_type"
243-
cls = dtype.construct_array_type() # type: ignore[union-attr]
240+
if not isinstance(dtype, np.dtype):
241+
# i.e. ExtensionDtype
242+
cls = dtype.construct_array_type()
244243
if isinstance(values, cls) and values.dtype == dtype:
245244
return values
246245

247246
values = cls._from_sequence(values)
248247
elif is_bool_dtype(dtype):
249-
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has incompatible
250-
# type "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
251-
# Type[int], Type[complex], Type[bool], Type[object]]"; expected
252-
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int],
253-
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
254-
# Tuple[Any, Any]]]"
255-
values = values.astype(dtype, copy=False) # type: ignore[arg-type]
248+
values = values.astype(dtype, copy=False)
256249

257250
# we only support object dtypes bool Index
258251
if isinstance(original, ABCIndex):
259252
values = values.astype(object, copy=False)
260253
elif dtype is not None:
261254
if is_datetime64_dtype(dtype):
262-
dtype = "datetime64[ns]"
255+
dtype = np.dtype("datetime64[ns]")
263256
elif is_timedelta64_dtype(dtype):
264-
dtype = "timedelta64[ns]"
265-
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has incompatible
266-
# type "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
267-
# Type[int], Type[complex], Type[bool], Type[object]]"; expected
268-
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int],
269-
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
270-
# Tuple[Any, Any]]]"
271-
values = values.astype(dtype, copy=False) # type: ignore[arg-type]
257+
dtype = np.dtype("timedelta64[ns]")
258+
259+
values = values.astype(dtype, copy=False)
272260

273261
return values
274262

@@ -773,7 +761,8 @@ def factorize(
773761
uniques = Index(uniques)
774762
return codes, uniques
775763

776-
if is_extension_array_dtype(values.dtype):
764+
if not isinstance(values.dtype, np.dtype):
765+
# i.e. ExtensionDtype
777766
codes, uniques = values.factorize(na_sentinel=na_sentinel)
778767
dtype = original.dtype
779768
else:
@@ -1663,7 +1652,8 @@ def diff(arr, n: int, axis: int = 0, stacklevel=3):
16631652
arr = arr.to_numpy()
16641653
dtype = arr.dtype
16651654

1666-
if is_extension_array_dtype(dtype):
1655+
if not isinstance(dtype, np.dtype):
1656+
# i.e ExtensionDtype
16671657
if hasattr(arr, f"__{op.__name__}__"):
16681658
if axis != 0:
16691659
raise ValueError(f"cannot diff {type(arr).__name__} on axis={axis}")

pandas/core/arrays/categorical.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@
6767
needs_i8_conversion,
6868
pandas_dtype,
6969
)
70-
from pandas.core.dtypes.dtypes import CategoricalDtype
70+
from pandas.core.dtypes.dtypes import (
71+
CategoricalDtype,
72+
ExtensionDtype,
73+
)
7174
from pandas.core.dtypes.generic import (
7275
ABCIndex,
7376
ABCSeries,
@@ -513,7 +516,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
513516
result = self._set_dtype(dtype)
514517

515518
# TODO: consolidate with ndarray case?
516-
elif is_extension_array_dtype(dtype):
519+
elif isinstance(dtype, ExtensionDtype):
517520
result = pd_array(self, dtype=dtype, copy=copy)
518521

519522
elif is_integer_dtype(dtype) and self.isna().any():
@@ -524,28 +527,15 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
524527
# variable has type "Categorical")
525528
result = np.array( # type: ignore[assignment]
526529
self,
527-
# error: Argument "dtype" to "array" has incompatible type
528-
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float],
529-
# Type[int], Type[complex], Type[bool], Type[object]]"; expected
530-
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any,
531-
# int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
532-
# Tuple[Any, Any]]]"
533-
dtype=dtype, # type: ignore[arg-type]
530+
dtype=dtype,
534531
copy=copy,
535532
)
536533

537534
else:
538535
# GH8628 (PERF): astype category codes instead of astyping array
539536
try:
540537
new_cats = np.asarray(self.categories)
541-
# error: Argument "dtype" to "astype" of "_ArrayOrScalarCommon" has
542-
# incompatible type "Union[ExtensionDtype, dtype[Any]]"; expected
543-
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any,
544-
# int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
545-
# Tuple[Any, Any]]]"
546-
new_cats = new_cats.astype(
547-
dtype=dtype, copy=copy # type: ignore[arg-type]
548-
)
538+
new_cats = new_cats.astype(dtype=dtype, copy=copy)
549539
except (
550540
TypeError, # downstream error msg for CategoricalIndex is misleading
551541
ValueError,

pandas/core/arrays/datetimelike.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,11 @@ def _isnan(self) -> np.ndarray:
877877
return self.asi8 == iNaT
878878

879879
@property # NB: override with cache_readonly in immutable subclasses
880-
def _hasnans(self) -> np.ndarray:
880+
def _hasnans(self) -> bool:
881881
"""
882882
return if I have any nans; enables various perf speedups
883883
"""
884-
# error: Incompatible return value type (got "bool", expected "ndarray")
885-
return bool(self._isnan.any()) # type: ignore[return-value]
884+
return bool(self._isnan.any())
886885

887886
def _maybe_mask_results(
888887
self, result: np.ndarray, fill_value=iNaT, convert=None

pandas/core/indexes/base.py

Lines changed: 29 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@
191191
str_t = str
192192

193193

194-
# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
195-
_o_dtype = np.dtype(object) # type: ignore[type-var]
194+
_o_dtype = np.dtype("object")
196195

197196

198197
_Identity = NewType("_Identity", object)
@@ -417,11 +416,7 @@ def __new__(
417416
# maybe coerce to a sub-class
418417
arr = data
419418
else:
420-
# error: Argument "dtype" to "asarray_tuplesafe" has incompatible type
421-
# "Type[object]"; expected "Union[str, dtype[Any], None]"
422-
arr = com.asarray_tuplesafe(
423-
data, dtype=object # type: ignore[arg-type]
424-
)
419+
arr = com.asarray_tuplesafe(data, dtype=np.dtype("object"))
425420

426421
if dtype is None:
427422
arr = _maybe_cast_data_without_dtype(arr)
@@ -456,9 +451,7 @@ def __new__(
456451
)
457452
# other iterable of some kind
458453

459-
# error: Argument "dtype" to "asarray_tuplesafe" has incompatible type
460-
# "Type[object]"; expected "Union[str, dtype[Any], None]"
461-
subarr = com.asarray_tuplesafe(data, dtype=object) # type: ignore[arg-type]
454+
subarr = com.asarray_tuplesafe(data, dtype=np.dtype("object"))
462455
return Index(subarr, dtype=dtype, copy=copy, name=name, **kwargs)
463456

464457
@classmethod
@@ -2902,16 +2895,10 @@ def union(self, other, sort=None):
29022895
# <T> | <T> -> T
29032896
# <T> | <U> -> object
29042897
if not (is_integer_dtype(self.dtype) and is_integer_dtype(other.dtype)):
2905-
# error: Incompatible types in assignment (expression has type
2906-
# "str", variable has type "Union[dtype[Any], ExtensionDtype]")
2907-
dtype = "float64" # type: ignore[assignment]
2898+
dtype = np.dtype("float64")
29082899
else:
29092900
# one is int64 other is uint64
2910-
2911-
# error: Incompatible types in assignment (expression has type
2912-
# "Type[object]", variable has type "Union[dtype[Any],
2913-
# ExtensionDtype]")
2914-
dtype = object # type: ignore[assignment]
2901+
dtype = np.dtype("object")
29152902

29162903
left = self.astype(dtype, copy=False)
29172904
right = other.astype(dtype, copy=False)
@@ -3906,6 +3893,9 @@ def join(
39063893
self_is_mi = isinstance(self, ABCMultiIndex)
39073894
other_is_mi = isinstance(other, ABCMultiIndex)
39083895

3896+
lindexer: Optional[np.ndarray]
3897+
rindexer: Optional[np.ndarray]
3898+
39093899
# try to figure out the join level
39103900
# GH3662
39113901
if level is None and (self_is_mi or other_is_mi):
@@ -4003,15 +3993,11 @@ def join(
40033993

40043994
if return_indexers:
40053995
if join_index is self:
4006-
# error: Incompatible types in assignment (expression has type "None",
4007-
# variable has type "ndarray")
4008-
lindexer = None # type: ignore[assignment]
3996+
lindexer = None
40093997
else:
40103998
lindexer = self.get_indexer(join_index)
40113999
if join_index is other:
4012-
# error: Incompatible types in assignment (expression has type "None",
4013-
# variable has type "ndarray")
4014-
rindexer = None # type: ignore[assignment]
4000+
rindexer = None
40154001
else:
40164002
rindexer = other.get_indexer(join_index)
40174003
return join_index, lindexer, rindexer
@@ -4114,15 +4100,11 @@ def _join_non_unique(self, other, how="left", return_indexers=False):
41144100
left_idx = ensure_platform_int(left_idx)
41154101
right_idx = ensure_platform_int(right_idx)
41164102

4117-
join_index = np.asarray(lvalues.take(left_idx))
4103+
join_array = np.asarray(lvalues.take(left_idx))
41184104
mask = left_idx == -1
4119-
np.putmask(join_index, mask, rvalues.take(right_idx))
4105+
np.putmask(join_array, mask, rvalues.take(right_idx))
41204106

4121-
# error: Incompatible types in assignment (expression has type "Index", variable
4122-
# has type "ndarray")
4123-
join_index = self._wrap_joined_index(
4124-
join_index, other # type: ignore[assignment]
4125-
)
4107+
join_index = self._wrap_joined_index(join_array, other)
41264108

41274109
if return_indexers:
41284110
return join_index, left_idx, right_idx
@@ -4286,6 +4268,9 @@ def _join_monotonic(self, other, how="left", return_indexers=False):
42864268
sv = self._get_engine_target()
42874269
ov = other._get_engine_target()
42884270

4271+
ridx: Optional[np.ndarray]
4272+
lidx: Optional[np.ndarray]
4273+
42894274
if self.is_unique and other.is_unique:
42904275
# We can perform much better than the general case
42914276
if how == "left":
@@ -4295,61 +4280,24 @@ def _join_monotonic(self, other, how="left", return_indexers=False):
42954280
elif how == "right":
42964281
join_index = other
42974282
lidx = self._left_indexer_unique(ov, sv)
4298-
# error: Incompatible types in assignment (expression has type "None",
4299-
# variable has type "ndarray")
4300-
ridx = None # type: ignore[assignment]
4283+
ridx = None
43014284
elif how == "inner":
4302-
# error: Incompatible types in assignment (expression has type
4303-
# "ndarray", variable has type "Index")
4304-
join_index, lidx, ridx = self._inner_indexer( # type:ignore[assignment]
4305-
sv, ov
4306-
)
4307-
# error: Argument 1 to "_wrap_joined_index" of "Index" has incompatible
4308-
# type "Index"; expected "ndarray"
4309-
join_index = self._wrap_joined_index(
4310-
join_index, other # type: ignore[arg-type]
4311-
)
4285+
join_array, lidx, ridx = self._inner_indexer(sv, ov)
4286+
join_index = self._wrap_joined_index(join_array, other)
43124287
elif how == "outer":
4313-
# error: Incompatible types in assignment (expression has type
4314-
# "ndarray", variable has type "Index")
4315-
join_index, lidx, ridx = self._outer_indexer( # type:ignore[assignment]
4316-
sv, ov
4317-
)
4318-
# error: Argument 1 to "_wrap_joined_index" of "Index" has incompatible
4319-
# type "Index"; expected "ndarray"
4320-
join_index = self._wrap_joined_index(
4321-
join_index, other # type: ignore[arg-type]
4322-
)
4288+
join_array, lidx, ridx = self._outer_indexer(sv, ov)
4289+
join_index = self._wrap_joined_index(join_array, other)
43234290
else:
43244291
if how == "left":
4325-
# error: Incompatible types in assignment (expression has type
4326-
# "ndarray", variable has type "Index")
4327-
join_index, lidx, ridx = self._left_indexer( # type: ignore[assignment]
4328-
sv, ov
4329-
)
4292+
join_array, lidx, ridx = self._left_indexer(sv, ov)
43304293
elif how == "right":
4331-
# error: Incompatible types in assignment (expression has type
4332-
# "ndarray", variable has type "Index")
4333-
join_index, ridx, lidx = self._left_indexer( # type: ignore[assignment]
4334-
ov, sv
4335-
)
4294+
join_array, ridx, lidx = self._left_indexer(ov, sv)
43364295
elif how == "inner":
4337-
# error: Incompatible types in assignment (expression has type
4338-
# "ndarray", variable has type "Index")
4339-
join_index, lidx, ridx = self._inner_indexer( # type:ignore[assignment]
4340-
sv, ov
4341-
)
4296+
join_array, lidx, ridx = self._inner_indexer(sv, ov)
43424297
elif how == "outer":
4343-
# error: Incompatible types in assignment (expression has type
4344-
# "ndarray", variable has type "Index")
4345-
join_index, lidx, ridx = self._outer_indexer( # type:ignore[assignment]
4346-
sv, ov
4347-
)
4348-
# error: Argument 1 to "_wrap_joined_index" of "Index" has incompatible type
4349-
# "Index"; expected "ndarray"
4350-
join_index = self._wrap_joined_index(
4351-
join_index, other # type: ignore[arg-type]
4352-
)
4298+
join_array, lidx, ridx = self._outer_indexer(sv, ov)
4299+
4300+
join_index = self._wrap_joined_index(join_array, other)
43534301

43544302
if return_indexers:
43554303
lidx = None if lidx is None else ensure_platform_int(lidx)
@@ -6477,12 +6425,8 @@ def _maybe_cast_data_without_dtype(subarr):
64776425
pass
64786426

64796427
elif inferred.startswith("timedelta"):
6480-
# error: Incompatible types in assignment (expression has type
6481-
# "TimedeltaArray", variable has type "ndarray")
6482-
data = TimedeltaArray._from_sequence( # type: ignore[assignment]
6483-
subarr, copy=False
6484-
)
6485-
return data
6428+
tda = TimedeltaArray._from_sequence(subarr, copy=False)
6429+
return tda
64866430
elif inferred == "period":
64876431
try:
64886432
data = PeriodArray._from_sequence(subarr)

pandas/core/internals/blocks.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@
118118
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
119119

120120
# comparison is faster than is_object_dtype
121-
122-
# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
123-
_dtype_obj = np.dtype(object) # type: ignore[type-var]
121+
_dtype_obj = np.dtype("object")
124122

125123

126124
class Block(PandasObject):
@@ -1598,14 +1596,9 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
15981596
values = self.values
15991597
mask = isna(values)
16001598

1601-
# error: Incompatible types in assignment (expression has type "ndarray",
1602-
# variable has type "ExtensionArray")
1603-
values = np.asarray(values.astype(object)) # type: ignore[assignment]
1604-
values[mask] = na_rep
1605-
1606-
# TODO(EA2D): reshape not needed with 2D EAs
1607-
# we are expected to return a 2-d ndarray
1608-
return self.make_block(values)
1599+
new_values = np.asarray(values.astype(object))
1600+
new_values[mask] = na_rep
1601+
return self.make_block(new_values)
16091602

16101603
def take_nd(
16111604
self, indexer, axis: int = 0, new_mgr_locs=None, fill_value=lib.no_default

0 commit comments

Comments
 (0)