Skip to content

Commit 56498ae

Browse files
authored
CLN: assorted follow-ups (#45402)
1 parent c2fc924 commit 56498ae

File tree

16 files changed

+119
-74
lines changed

16 files changed

+119
-74
lines changed

pandas/core/apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ def series_generator(self):
996996
# GH#35462 re-pin mgr in case setitem changed it
997997
ser._mgr = mgr
998998
mgr.set_values(arr)
999-
ser.name = name
999+
object.__setattr__(ser, "_name", name)
10001000
yield ser
10011001

10021002
@property

pandas/core/arrays/masked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393

9494
class BaseMaskedDtype(ExtensionDtype):
9595
"""
96-
Base class for dtypes for BasedMaskedArray subclasses.
96+
Base class for dtypes for BaseMaskedArray subclasses.
9797
"""
9898

9999
name: str

pandas/core/arrays/string_.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ def __init__(self, values, copy=False):
319319
super().__init__(values, copy=copy)
320320
if not isinstance(values, type(self)):
321321
self._validate()
322-
# error: Incompatible types in assignment (expression has type "StringDtype",
323-
# variable has type "PandasDtype")
324322
NDArrayBacked.__init__(self, self._ndarray, StringDtype(storage="python"))
325323

326324
def _validate(self):

pandas/core/construction.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ def sanitize_array(
561561
# it is lossy.
562562
dtype = cast(np.dtype, dtype)
563563
return np.array(data, dtype=dtype, copy=copy)
564+
565+
# We ignore the dtype arg and return floating values,
566+
# e.g. test_constructor_floating_data_int_dtype
567+
# TODO: where is the discussion that documents the reason for this?
564568
subarr = np.array(data, copy=copy)
565569
else:
566570
# we will try to copy by-definition here
@@ -591,18 +595,21 @@ def sanitize_array(
591595
try:
592596
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
593597
except ValueError:
594-
casted = np.array(data, copy=False)
595-
if casted.dtype.kind == "f" and is_integer_dtype(dtype):
596-
# GH#40110 match the behavior we have if we passed
597-
# a ndarray[float] to begin with
598-
return sanitize_array(
599-
casted,
600-
index,
601-
dtype,
602-
copy=False,
603-
raise_cast_failure=raise_cast_failure,
604-
allow_2d=allow_2d,
605-
)
598+
if is_integer_dtype(dtype):
599+
casted = np.array(data, copy=False)
600+
if casted.dtype.kind == "f":
601+
# GH#40110 match the behavior we have if we passed
602+
# a ndarray[float] to begin with
603+
return sanitize_array(
604+
casted,
605+
index,
606+
dtype,
607+
copy=False,
608+
raise_cast_failure=raise_cast_failure,
609+
allow_2d=allow_2d,
610+
)
611+
else:
612+
raise
606613
else:
607614
raise
608615
else:
@@ -762,7 +769,8 @@ def _try_cast(
762769
# data differently; _from_sequence treats naive as wall times,
763770
# while maybe_cast_to_datetime treats it as UTC
764771
# see test_maybe_promote_any_numpy_dtype_with_datetimetz
765-
772+
# TODO(2.0): with deprecations enforced, should be able to remove
773+
# special case.
766774
return maybe_cast_to_datetime(arr, dtype)
767775
# TODO: copy?
768776

pandas/core/dtypes/cast.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,23 +1447,18 @@ def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
14471447
"""
14481448
new_dtype: DtypeObj
14491449

1450-
if left.dtype.kind in ["i", "u", "c"] and (
1451-
lib.is_integer(right) or lib.is_float(right)
1450+
if (
1451+
isinstance(left, np.ndarray)
1452+
and left.dtype.kind in ["i", "u", "c"]
1453+
and (lib.is_integer(right) or lib.is_float(right))
14521454
):
14531455
# e.g. with int8 dtype and right=512, we want to end up with
14541456
# np.int16, whereas infer_dtype_from(512) gives np.int64,
14551457
# which will make us upcast too far.
14561458
if lib.is_float(right) and right.is_integer() and left.dtype.kind != "f":
14571459
right = int(right)
14581460

1459-
# Argument 1 to "result_type" has incompatible type "Union[ExtensionArray,
1460-
# ndarray[Any, Any]]"; expected "Union[Union[_SupportsArray[dtype[Any]],
1461-
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex,
1462-
# str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]],
1463-
# Union[dtype[Any], None, Type[Any], _SupportsDType[dtype[Any]], str,
1464-
# Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex,
1465-
# Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]]]]"
1466-
new_dtype = np.result_type(left, right) # type:ignore[arg-type]
1461+
new_dtype = np.result_type(left, right)
14671462

14681463
else:
14691464
dtype, _ = infer_dtype_from(right, pandas_dtype=True)

pandas/core/frame.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
from pandas.core.arrays import (
149149
DatetimeArray,
150150
ExtensionArray,
151+
PeriodArray,
151152
TimedeltaArray,
152153
)
153154
from pandas.core.arrays.sparse import SparseFrameAccessor
@@ -900,7 +901,7 @@ def _can_fast_transpose(self) -> bool:
900901
@property
901902
def _values( # type: ignore[override]
902903
self,
903-
) -> np.ndarray | DatetimeArray | TimedeltaArray:
904+
) -> np.ndarray | DatetimeArray | TimedeltaArray | PeriodArray:
904905
"""
905906
Analogue to ._values that may return a 2D ExtensionArray.
906907
"""
@@ -925,7 +926,7 @@ def _values( # type: ignore[override]
925926
return self.values
926927

927928
# more generally, whatever we allow in NDArrayBackedExtensionBlock
928-
arr = cast("np.ndarray | DatetimeArray | TimedeltaArray", arr)
929+
arr = cast("np.ndarray | DatetimeArray | TimedeltaArray | PeriodArray", arr)
929930
return arr.T
930931

931932
# ----------------------------------------------------------------------

pandas/core/groupby/ops.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -705,17 +705,14 @@ def get_iterator(
705705
"""
706706
splitter = self._get_splitter(data, axis=axis)
707707
keys = self.group_keys_seq
708-
for key, group in zip(keys, splitter):
709-
yield key, group.__finalize__(data, method="groupby")
708+
yield from zip(keys, splitter)
710709

711710
@final
712711
def _get_splitter(self, data: NDFrame, axis: int = 0) -> DataSplitter:
713712
"""
714713
Returns
715714
-------
716715
Generator yielding subsetted objects
717-
718-
__finalize__ has not been called for the subsetted objects returned.
719716
"""
720717
ids, _, ngroups = self.group_info
721718
return get_splitter(data, ids, ngroups, axis=axis)
@@ -753,7 +750,6 @@ def apply(
753750
zipped = zip(group_keys, splitter)
754751

755752
for key, group in zipped:
756-
group = group.__finalize__(data, method="groupby")
757753
object.__setattr__(group, "name", key)
758754

759755
# group might be modified
@@ -1001,7 +997,6 @@ def _aggregate_series_pure_python(
1001997
splitter = get_splitter(obj, ids, ngroups, axis=0)
1002998

1003999
for i, group in enumerate(splitter):
1004-
group = group.__finalize__(obj, method="groupby")
10051000
res = func(group)
10061001
res = libreduction.extract_result(res)
10071002

@@ -1244,8 +1239,8 @@ class SeriesSplitter(DataSplitter):
12441239
def _chop(self, sdata: Series, slice_obj: slice) -> Series:
12451240
# fastpath equivalent to `sdata.iloc[slice_obj]`
12461241
mgr = sdata._mgr.get_slice(slice_obj)
1247-
# __finalize__ not called here, must be applied by caller if applicable
1248-
return sdata._constructor(mgr, name=sdata.name, fastpath=True)
1242+
ser = sdata._constructor(mgr, name=sdata.name, fastpath=True)
1243+
return ser.__finalize__(sdata, method="groupby")
12491244

12501245

12511246
class FrameSplitter(DataSplitter):
@@ -1256,8 +1251,8 @@ def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
12561251
# else:
12571252
# return sdata.iloc[:, slice_obj]
12581253
mgr = sdata._mgr.get_slice(slice_obj, axis=1 - self.axis)
1259-
# __finalize__ not called here, must be applied by caller if applicable
1260-
return sdata._constructor(mgr)
1254+
df = sdata._constructor(mgr)
1255+
return df.__finalize__(sdata, method="groupby")
12611256

12621257

12631258
def get_splitter(

pandas/core/indexes/numeric.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ def _should_fallback_to_positional(self) -> bool:
272272

273273
@doc(Index._convert_slice_indexer)
274274
def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
275+
# TODO(2.0): once #45324 deprecation is enforced we should be able
276+
# to simplify this.
275277
if is_float_dtype(self.dtype):
276278
assert kind in ["loc", "getitem"]
277279

pandas/core/indexing.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,8 @@ def _is_nested_tuple_indexer(self, tup: tuple) -> bool:
805805
@final
806806
def _convert_tuple(self, key: tuple) -> tuple:
807807
# Note: we assume _tupleize_axis_indexer has been called, if necessary.
808-
keyidx = []
809808
self._validate_key_length(key)
810-
for i, k in enumerate(key):
811-
idx = self._convert_to_indexer(k, axis=i)
812-
keyidx.append(idx)
813-
809+
keyidx = [self._convert_to_indexer(k, axis=i) for i, k in enumerate(key)]
814810
return tuple(keyidx)
815811

816812
@final

pandas/core/reshape/merge.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,8 +2148,6 @@ def _factorize_keys(
21482148
rk = ensure_int64(rk.codes)
21492149

21502150
elif isinstance(lk, ExtensionArray) and is_dtype_equal(lk.dtype, rk.dtype):
2151-
# error: Incompatible types in assignment (expression has type "ndarray",
2152-
# variable has type "ExtensionArray")
21532151
lk, _ = lk._values_for_factorize()
21542152

21552153
# error: Item "ndarray" of "Union[Any, ndarray]" has no attribute

0 commit comments

Comments
 (0)