@@ -1425,7 +1425,7 @@ def to_dict(self, copy: bool = True):
1425
1425
def as_array (
1426
1426
self ,
1427
1427
transpose : bool = False ,
1428
- dtype : npt . DTypeLike | None = None ,
1428
+ dtype : np . dtype | None = None ,
1429
1429
copy : bool = False ,
1430
1430
na_value = lib .no_default ,
1431
1431
) -> np .ndarray :
@@ -1436,7 +1436,7 @@ def as_array(
1436
1436
----------
1437
1437
transpose : bool, default False
1438
1438
If True, transpose the return array.
1439
- dtype : object , default None
1439
+ dtype : np.dtype or None , default None
1440
1440
Data type of the return array.
1441
1441
copy : bool, default False
1442
1442
If True then guarantee that a copy is returned. A value of
@@ -1465,15 +1465,7 @@ def as_array(
1465
1465
# error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no
1466
1466
# attribute "to_numpy"
1467
1467
arr = blk .values .to_numpy ( # type: ignore[union-attr]
1468
- # pandas/core/internals/managers.py:1428: error: Argument "dtype" to
1469
- # "to_numpy" of "ExtensionArray" has incompatible type
1470
- # "Optional[Union[dtype[Any], None, type, _SupportsDType, str,
1471
- # Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex,
1472
- # Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any,
1473
- # Any]]]]"; expected "Optional[Union[ExtensionDtype, Union[str,
1474
- # dtype[Any]], Type[str], Type[float], Type[int], Type[complex],
1475
- # Type[bool], Type[object]]]"
1476
- dtype = dtype , # type: ignore[arg-type]
1468
+ dtype = dtype ,
1477
1469
na_value = na_value ,
1478
1470
).reshape (blk .shape )
1479
1471
else :
@@ -1495,34 +1487,44 @@ def as_array(
1495
1487
1496
1488
def _interleave (
1497
1489
self ,
1498
- dtype : npt . DTypeLike | ExtensionDtype | None = None ,
1490
+ dtype : np . dtype | None = None ,
1499
1491
na_value = lib .no_default ,
1500
1492
) -> np .ndarray :
1501
1493
"""
1502
1494
Return ndarray from blocks with specified item order
1503
1495
Items must be contained in the blocks
1504
1496
"""
1505
1497
if not dtype :
1506
- dtype = interleaved_dtype ([blk .dtype for blk in self .blocks ])
1498
+ # Incompatible types in assignment (expression has type
1499
+ # "Optional[Union[dtype[Any], ExtensionDtype]]", variable has
1500
+ # type "Optional[dtype[Any]]")
1501
+ dtype = interleaved_dtype ( # type: ignore[assignment]
1502
+ [blk .dtype for blk in self .blocks ]
1503
+ )
1507
1504
1508
1505
# TODO: https://github.com/pandas-dev/pandas/issues/22791
1509
1506
# Give EAs some input on what happens here. Sparse needs this.
1510
1507
if isinstance (dtype , SparseDtype ):
1511
1508
dtype = dtype .subtype
1509
+ dtype = cast (np .dtype , dtype )
1512
1510
elif isinstance (dtype , ExtensionDtype ):
1513
1511
dtype = np .dtype ("object" )
1514
1512
elif is_dtype_equal (dtype , str ):
1515
1513
dtype = np .dtype ("object" )
1516
1514
1517
- # error: Argument "dtype" to "empty" has incompatible type
1518
- # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected
1519
- # "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int],
1520
- # Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
1521
- # Tuple[Any, Any]]]"
1522
- result = np .empty (self .shape , dtype = dtype ) # type: ignore[arg-type]
1515
+ result = np .empty (self .shape , dtype = dtype )
1523
1516
1524
1517
itemmask = np .zeros (self .shape [0 ])
1525
1518
1519
+ if dtype == np .dtype ("object" ) and na_value is lib .no_default :
1520
+ # much more performant than using to_numpy below
1521
+ for blk in self .blocks :
1522
+ rl = blk .mgr_locs
1523
+ arr = blk .get_values (dtype )
1524
+ result [rl .indexer ] = arr
1525
+ itemmask [rl .indexer ] = 1
1526
+ return result
1527
+
1526
1528
for blk in self .blocks :
1527
1529
rl = blk .mgr_locs
1528
1530
if blk .is_extension :
@@ -1531,22 +1533,11 @@ def _interleave(
1531
1533
# error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no
1532
1534
# attribute "to_numpy"
1533
1535
arr = blk .values .to_numpy ( # type: ignore[union-attr]
1534
- # pandas/core/internals/managers.py:1485: error: Argument "dtype" to
1535
- # "to_numpy" of "ExtensionArray" has incompatible type
1536
- # "Union[dtype[Any], None, type, _SupportsDType, str, Tuple[Any,
1537
- # Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any],
1538
- # _DTypeDict, Tuple[Any, Any], ExtensionDtype]"; expected
1539
- # "Optional[Union[ExtensionDtype, Union[str, dtype[Any]], Type[str],
1540
- # Type[float], Type[int], Type[complex], Type[bool], Type[object]]]"
1541
- # [arg-type]
1542
- dtype = dtype , # type: ignore[arg-type]
1536
+ dtype = dtype ,
1543
1537
na_value = na_value ,
1544
1538
)
1545
1539
else :
1546
- # error: Argument 1 to "get_values" of "Block" has incompatible type
1547
- # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected
1548
- # "Union[dtype[Any], ExtensionDtype, None]"
1549
- arr = blk .get_values (dtype ) # type: ignore[arg-type]
1540
+ arr = blk .get_values (dtype )
1550
1541
result [rl .indexer ] = arr
1551
1542
itemmask [rl .indexer ] = 1
1552
1543
0 commit comments