Skip to content

Commit ea9ac65

Browse files
authored
Merge pull request #142 from pandas-dev/master
Sync Fork from Upstream Repo
2 parents 65b7536 + 0203f8d commit ea9ac65

File tree

14 files changed

+106
-204
lines changed

14 files changed

+106
-204
lines changed

pandas/_libs/parsers.pyx

+1-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,8 @@ def assert_series_equal(
976976
left_values = left._values
977977
right_values = right._values
978978
# Only check exact if dtype is numeric
979-
if is_extension_array_dtype(left_values) and is_extension_array_dtype(
980-
right_values
979+
if isinstance(left_values, ExtensionArray) and isinstance(
980+
right_values, ExtensionArray
981981
):
982982
assert_extension_array_equal(
983983
left_values,

pandas/core/algorithms.py

+11-24
Original file line numberDiff line numberDiff line change
@@ -235,41 +235,26 @@ def _reconstruct_data(
235235
# Catch DatetimeArray/TimedeltaArray
236236
return values
237237

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

245244
values = cls._from_sequence(values)
246245
elif is_bool_dtype(dtype):
247-
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has
248-
# incompatible type "Union[dtype, ExtensionDtype]"; expected
249-
# "Union[dtype, None, type, _SupportsDtype, str, Tuple[Any, int],
250-
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DtypeDict,
251-
# Tuple[Any, Any]]"
252-
values = values.astype(dtype, copy=False) # type: ignore[arg-type]
246+
values = values.astype(dtype, copy=False)
253247

254248
# we only support object dtypes bool Index
255249
if isinstance(original, ABCIndex):
256250
values = values.astype(object, copy=False)
257251
elif dtype is not None:
258252
if is_datetime64_dtype(dtype):
259-
# error: Incompatible types in assignment (expression has type
260-
# "str", variable has type "Union[dtype, ExtensionDtype]")
261-
dtype = "datetime64[ns]" # type: ignore[assignment]
253+
dtype = np.dtype("datetime64[ns]")
262254
elif is_timedelta64_dtype(dtype):
263-
# error: Incompatible types in assignment (expression has type
264-
# "str", variable has type "Union[dtype, ExtensionDtype]")
265-
dtype = "timedelta64[ns]" # type: ignore[assignment]
255+
dtype = np.dtype("timedelta64[ns]")
266256

267-
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has
268-
# incompatible type "Union[dtype, ExtensionDtype]"; expected
269-
# "Union[dtype, None, type, _SupportsDtype, str, Tuple[Any, int],
270-
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DtypeDict,
271-
# Tuple[Any, Any]]"
272-
values = values.astype(dtype, copy=False) # type: ignore[arg-type]
257+
values = values.astype(dtype, copy=False)
273258

274259
return values
275260

@@ -772,7 +757,8 @@ def factorize(
772757
uniques = Index(uniques)
773758
return codes, uniques
774759

775-
if is_extension_array_dtype(values.dtype):
760+
if not isinstance(values.dtype, np.dtype):
761+
# i.e. ExtensionDtype
776762
codes, uniques = values.factorize(na_sentinel=na_sentinel)
777763
dtype = original.dtype
778764
else:
@@ -1662,7 +1648,8 @@ def diff(arr, n: int, axis: int = 0, stacklevel=3):
16621648
arr = arr.to_numpy()
16631649
dtype = arr.dtype
16641650

1665-
if is_extension_array_dtype(dtype):
1651+
if not isinstance(dtype, np.dtype):
1652+
# i.e ExtensionDtype
16661653
if hasattr(arr, f"__{op.__name__}__"):
16671654
if axis != 0:
16681655
raise ValueError(f"cannot diff {type(arr).__name__} on axis={axis}")

pandas/core/arrays/categorical.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@
6666
needs_i8_conversion,
6767
pandas_dtype,
6868
)
69-
from pandas.core.dtypes.dtypes import CategoricalDtype
69+
from pandas.core.dtypes.dtypes import (
70+
CategoricalDtype,
71+
ExtensionDtype,
72+
)
7073
from pandas.core.dtypes.generic import (
7174
ABCIndex,
7275
ABCSeries,
@@ -504,7 +507,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
504507
result = self._set_dtype(dtype)
505508

506509
# TODO: consolidate with ndarray case?
507-
elif is_extension_array_dtype(dtype):
510+
elif isinstance(dtype, ExtensionDtype):
508511
result = pd_array(self, dtype=dtype, copy=copy)
509512

510513
elif is_integer_dtype(dtype) and self.isna().any():
@@ -515,28 +518,15 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
515518
# variable has type "Categorical")
516519
result = np.array( # type: ignore[assignment]
517520
self,
518-
# error: Argument "dtype" to "array" has incompatible type
519-
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float],
520-
# Type[int], Type[complex], Type[bool], Type[object]]"; expected
521-
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any,
522-
# int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
523-
# Tuple[Any, Any]]]"
524-
dtype=dtype, # type: ignore[arg-type]
521+
dtype=dtype,
525522
copy=copy,
526523
)
527524

528525
else:
529526
# GH8628 (PERF): astype category codes instead of astyping array
530527
try:
531528
new_cats = np.asarray(self.categories)
532-
# error: Argument "dtype" to "astype" of "_ArrayOrScalarCommon" has
533-
# incompatible type "Union[ExtensionDtype, dtype[Any]]"; expected
534-
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any,
535-
# int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
536-
# Tuple[Any, Any]]]"
537-
new_cats = new_cats.astype(
538-
dtype=dtype, copy=copy # type: ignore[arg-type]
539-
)
529+
new_cats = new_cats.astype(dtype=dtype, copy=copy)
540530
except (
541531
TypeError, # downstream error msg for CategoricalIndex is misleading
542532
ValueError,

pandas/core/arrays/datetimelike.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123
from pandas.tseries import frequencies
124124

125125
if TYPE_CHECKING:
126+
from typing import Literal
127+
126128
from pandas.core.arrays import (
127129
DatetimeArray,
128130
TimedeltaArray,
@@ -458,6 +460,14 @@ def astype(self, dtype, copy=True):
458460
def view(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT:
459461
...
460462

463+
@overload
464+
def view(self, dtype: Literal["M8[ns]"]) -> DatetimeArray:
465+
...
466+
467+
@overload
468+
def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray:
469+
...
470+
461471
@overload
462472
def view(self, dtype: Optional[Dtype] = ...) -> ArrayLike:
463473
...
@@ -878,12 +888,11 @@ def _isnan(self) -> np.ndarray:
878888
return self.asi8 == iNaT
879889

880890
@property # NB: override with cache_readonly in immutable subclasses
881-
def _hasnans(self) -> np.ndarray:
891+
def _hasnans(self) -> bool:
882892
"""
883893
return if I have any nans; enables various perf speedups
884894
"""
885-
# error: Incompatible return value type (got "bool", expected "ndarray")
886-
return bool(self._isnan.any()) # type: ignore[return-value]
895+
return bool(self._isnan.any())
887896

888897
def _maybe_mask_results(
889898
self, result: np.ndarray, fill_value=iNaT, convert=None

pandas/core/arrays/period.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,7 @@ def fillna(self, value=None, method=None, limit=None) -> PeriodArray:
643643
if method is not None:
644644
# view as dt64 so we get treated as timelike in core.missing
645645
dta = self.view("M8[ns]")
646-
# error: Item "ndarray" of "Union[ExtensionArray, ndarray]" has no attribute
647-
# "fillna"
648-
result = dta.fillna( # type: ignore[union-attr]
649-
value=value, method=method, limit=limit
650-
)
646+
result = dta.fillna(value=value, method=method, limit=limit)
651647
return result.view(self.dtype)
652648
return super().fillna(value=value, method=method, limit=limit)
653649

pandas/core/arrays/sparse/dtype.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from pandas.core.dtypes.cast import astype_nansafe
2828
from pandas.core.dtypes.common import (
2929
is_bool_dtype,
30-
is_extension_array_dtype,
3130
is_object_dtype,
3231
is_scalar,
3332
is_string_dtype,
@@ -339,14 +338,10 @@ def update_dtype(self, dtype):
339338
dtype = pandas_dtype(dtype)
340339

341340
if not isinstance(dtype, cls):
342-
if is_extension_array_dtype(dtype):
341+
if not isinstance(dtype, np.dtype):
343342
raise TypeError("sparse arrays of extension dtypes not supported")
344343

345-
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
346-
# attribute "item"
347-
fill_value = astype_nansafe( # type: ignore[union-attr]
348-
np.array(self.fill_value), dtype
349-
).item()
344+
fill_value = astype_nansafe(np.array(self.fill_value), dtype).item()
350345
dtype = cls(dtype, fill_value=fill_value)
351346

352347
return dtype

pandas/core/dtypes/cast.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Type,
2626
Union,
2727
cast,
28+
overload,
2829
)
2930
import warnings
3031

@@ -107,6 +108,8 @@
107108
)
108109

109110
if TYPE_CHECKING:
111+
from typing import Literal
112+
110113
from pandas import Series
111114
from pandas.core.arrays import (
112115
DatetimeArray,
@@ -1164,6 +1167,20 @@ def astype_td64_unit_conversion(
11641167
return result
11651168

11661169

1170+
@overload
1171+
def astype_nansafe(
1172+
arr: np.ndarray, dtype: np.dtype, copy: bool = ..., skipna: bool = ...
1173+
) -> np.ndarray:
1174+
...
1175+
1176+
1177+
@overload
1178+
def astype_nansafe(
1179+
arr: np.ndarray, dtype: ExtensionDtype, copy: bool = ..., skipna: bool = ...
1180+
) -> ExtensionArray:
1181+
...
1182+
1183+
11671184
def astype_nansafe(
11681185
arr: np.ndarray, dtype: DtypeObj, copy: bool = True, skipna: bool = False
11691186
) -> ArrayLike:
@@ -1190,14 +1207,10 @@ def astype_nansafe(
11901207
flags = arr.flags
11911208
flat = arr.ravel("K")
11921209
result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna)
1193-
order = "F" if flags.f_contiguous else "C"
1210+
order: Literal["C", "F"] = "F" if flags.f_contiguous else "C"
11941211
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
11951212
# attribute "reshape"
1196-
# error: No overload variant of "reshape" of "_ArrayOrScalarCommon" matches
1197-
# argument types "Tuple[int, ...]", "str"
1198-
return result.reshape( # type: ignore[union-attr,call-overload]
1199-
arr.shape, order=order
1200-
)
1213+
return result.reshape(arr.shape, order=order) # type: ignore[union-attr]
12011214

12021215
# We get here with 0-dim from sparse
12031216
arr = np.atleast_1d(arr)

0 commit comments

Comments
 (0)