Skip to content

Commit 2a17bf3

Browse files
authored
TYP: follow-ups to recent PRs (#38840)
1 parent 46841f8 commit 2a17bf3

File tree

6 files changed

+28
-44
lines changed

6 files changed

+28
-44
lines changed

pandas/core/dtypes/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
8989
# wrap datetime-likes in EA to ensure astype(object) gives Timestamp/Timedelta
9090
# this can happen when concat_compat is called directly on arrays (when arrays
9191
# are not coming from Index/Series._values), eg in BlockManager.quantile
92-
arr = array(arr)
92+
arr = ensure_wrapped_if_datetimelike(arr)
9393

9494
if is_extension_array_dtype(dtype):
9595
if isinstance(arr, np.ndarray):

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9600,15 +9600,15 @@ def _from_nested_dict(data) -> collections.defaultdict:
96009600
return new_data
96019601

96029602

9603-
def _reindex_for_setitem(value, index: Index):
9603+
def _reindex_for_setitem(value: FrameOrSeriesUnion, index: Index) -> ArrayLike:
96049604
# reindex if necessary
96059605

96069606
if value.index.equals(index) or not len(index):
96079607
return value._values.copy()
96089608

96099609
# GH#4107
96109610
try:
9611-
value = value.reindex(index)._values
9611+
reindexed_value = value.reindex(index)._values
96129612
except ValueError as err:
96139613
# raised in MultiIndex.from_tuples, see test_insert_error_msmgs
96149614
if not value.index.is_unique:
@@ -9618,7 +9618,7 @@ def _reindex_for_setitem(value, index: Index):
96189618
raise TypeError(
96199619
"incompatible index of inserted column with frame index"
96209620
) from err
9621-
return value
9621+
return reindexed_value
96229622

96239623

96249624
def _maybe_atleast_2d(value):

pandas/core/internals/blocks.py

+11-30
Original file line numberDiff line numberDiff line change
@@ -1274,22 +1274,6 @@ def shift(self, periods: int, axis: int = 0, fill_value=None):
12741274

12751275
return [self.make_block(new_values)]
12761276

1277-
def _maybe_reshape_where_args(self, values, other, cond, axis):
1278-
transpose = self.ndim == 2
1279-
1280-
cond = _extract_bool_array(cond)
1281-
1282-
# If the default broadcasting would go in the wrong direction, then
1283-
# explicitly reshape other instead
1284-
if getattr(other, "ndim", 0) >= 1:
1285-
if values.ndim - 1 == other.ndim and axis == 1:
1286-
other = other.reshape(tuple(other.shape + (1,)))
1287-
elif transpose and values.ndim == self.ndim - 1:
1288-
# TODO(EA2D): not neceesssary with 2D EAs
1289-
cond = cond.T
1290-
1291-
return other, cond
1292-
12931277
def where(self, other, cond, errors="raise", axis: int = 0) -> List["Block"]:
12941278
"""
12951279
evaluate the block; return result block(s) from the result
@@ -1319,7 +1303,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List["Block"]:
13191303
if transpose:
13201304
values = values.T
13211305

1322-
other, cond = self._maybe_reshape_where_args(values, other, cond, axis)
1306+
cond = _extract_bool_array(cond)
13231307

13241308
if cond.ravel("K").all():
13251309
result = values
@@ -2072,7 +2056,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List["Block"]:
20722056
# TODO(EA2D): reshape unnecessary with 2D EAs
20732057
arr = self.array_values().reshape(self.shape)
20742058

2075-
other, cond = self._maybe_reshape_where_args(arr, other, cond, axis)
2059+
cond = _extract_bool_array(cond)
20762060

20772061
try:
20782062
res_values = arr.T.where(cond, other).T
@@ -2572,23 +2556,20 @@ def _block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
25722556
return values
25732557

25742558

2575-
def safe_reshape(arr, new_shape: Shape):
2559+
def safe_reshape(arr: ArrayLike, new_shape: Shape) -> ArrayLike:
25762560
"""
2577-
If possible, reshape `arr` to have shape `new_shape`,
2578-
with a couple of exceptions (see gh-13012):
2579-
2580-
1) If `arr` is a ExtensionArray or Index, `arr` will be
2581-
returned as is.
2582-
2) If `arr` is a Series, the `_values` attribute will
2583-
be reshaped and returned.
2561+
Reshape `arr` to have shape `new_shape`, unless it is an ExtensionArray,
2562+
in which case it will be returned unchanged (see gh-13012).
25842563
25852564
Parameters
25862565
----------
2587-
arr : array-like, object to be reshaped
2588-
new_shape : int or tuple of ints, the new shape
2566+
arr : np.ndarray or ExtensionArray
2567+
new_shape : Tuple[int]
2568+
2569+
Returns
2570+
-------
2571+
np.ndarray or ExtensionArray
25892572
"""
2590-
if isinstance(arr, ABCSeries):
2591-
arr = arr._values
25922573
if not is_extension_array_dtype(arr.dtype):
25932574
# Note: this will include TimedeltaArray and tz-naive DatetimeArray
25942575
# TODO(EA2D): special case will be unnecessary with 2D EAs

pandas/core/internals/concat.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from pandas._libs import NaT, internals as libinternals
8-
from pandas._typing import DtypeObj, Shape
8+
from pandas._typing import ArrayLike, DtypeObj, Shape
99
from pandas.util._decorators import cache_readonly
1010

1111
from pandas.core.dtypes.cast import maybe_promote
@@ -29,11 +29,12 @@
2929
from pandas.core.internals.managers import BlockManager
3030

3131
if TYPE_CHECKING:
32+
from pandas import Index
3233
from pandas.core.arrays.sparse.dtype import SparseDtype
3334

3435

3536
def concatenate_block_managers(
36-
mgrs_indexers, axes, concat_axis: int, copy: bool
37+
mgrs_indexers, axes: List["Index"], concat_axis: int, copy: bool
3738
) -> BlockManager:
3839
"""
3940
Concatenate block managers into one.
@@ -96,7 +97,7 @@ def concatenate_block_managers(
9697
return BlockManager(blocks, axes)
9798

9899

99-
def _get_mgr_concatenation_plan(mgr, indexers):
100+
def _get_mgr_concatenation_plan(mgr: BlockManager, indexers: Dict[int, np.ndarray]):
100101
"""
101102
Construct concatenation plan for given block manager and indexers.
102103
@@ -235,7 +236,7 @@ def is_na(self) -> bool:
235236

236237
return isna_all(values_flat)
237238

238-
def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na):
239+
def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na) -> ArrayLike:
239240
if upcasted_na is None:
240241
# No upcasting is necessary
241242
fill_value = self.block.fill_value
@@ -307,7 +308,9 @@ def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na):
307308
return values
308309

309310

310-
def _concatenate_join_units(join_units, concat_axis, copy):
311+
def _concatenate_join_units(
312+
join_units: List[JoinUnit], concat_axis: int, copy: bool
313+
) -> ArrayLike:
311314
"""
312315
Concatenate values from several join units along selected axis.
313316
"""
@@ -513,7 +516,7 @@ def _is_uniform_reindex(join_units) -> bool:
513516
)
514517

515518

516-
def _trim_join_unit(join_unit, length):
519+
def _trim_join_unit(join_unit: JoinUnit, length: int) -> JoinUnit:
517520
"""
518521
Reduce join_unit's shape along item axis to length.
519522
@@ -540,7 +543,7 @@ def _trim_join_unit(join_unit, length):
540543
return JoinUnit(block=extra_block, indexers=extra_indexers, shape=extra_shape)
541544

542545

543-
def _combine_concat_plans(plans, concat_axis):
546+
def _combine_concat_plans(plans, concat_axis: int):
544547
"""
545548
Combine multiple concatenation plans into one.
546549

pandas/core/tools/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def to_numeric(arg, errors="raise", downcast=None):
171171
if is_numeric_dtype(values_dtype):
172172
pass
173173
elif is_datetime_or_timedelta_dtype(values_dtype):
174-
values = values.astype(np.int64)
174+
values = values.view(np.int64)
175175
else:
176176
values = ensure_object(values)
177177
coerce_numeric = errors not in ("ignore", "raise")

pandas/tests/plotting/frame/test_frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def test_nullable_int_plot(self):
156156
"A": [1, 2, 3, 4, 5],
157157
"B": [1.0, 2.0, 3.0, 4.0, 5.0],
158158
"C": [7, 5, np.nan, 3, 2],
159-
"D": pd.to_datetime(dates, format="%Y"),
160-
"E": pd.to_datetime(dates, format="%Y", utc=True),
159+
"D": pd.to_datetime(dates, format="%Y").view("i8"),
160+
"E": pd.to_datetime(dates, format="%Y", utc=True).view("i8"),
161161
},
162162
dtype=np.int64,
163163
)

0 commit comments

Comments
 (0)