Skip to content

Commit 5fa7f31

Browse files
authored
CLN: assorted (#53086)
1 parent b0140bf commit 5fa7f31

27 files changed

+64
-96
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ Metadata
439439

440440
Other
441441
^^^^^
442-
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are presnet (:issue:`52840`)
442+
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
443443
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
444444
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
445445
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)

pandas/_libs/algos.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def kth_smallest(numeric_t[::1] arr, Py_ssize_t k) -> numeric_t:
346346
def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
347347
cdef:
348348
Py_ssize_t i, xi, yi, N, K
349-
bint minpv
349+
int64_t minpv
350350
float64_t[:, ::1] result
351351
ndarray[uint8_t, ndim=2] mask
352352
int64_t nobs = 0
@@ -357,7 +357,7 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
357357
if minp is None:
358358
minpv = 1
359359
else:
360-
minpv = <int>minp
360+
minpv = <int64_t>minp
361361

362362
result = np.empty((K, K), dtype=np.float64)
363363
mask = np.isfinite(mat).view(np.uint8)

pandas/_libs/index.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ from pandas._typing import npt
55
from pandas import MultiIndex
66
from pandas.core.arrays import ExtensionArray
77

8+
multiindex_nulls_shift: int
9+
810
class IndexEngine:
911
over_size_threshold: bool
1012
def __init__(self, values: np.ndarray) -> None: ...

pandas/_libs/internals.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ class BlockValuesRefs:
102102
referenced_blocks: list[weakref.ref]
103103
def __init__(self, blk: SharedBlock | None = ...) -> None: ...
104104
def add_reference(self, blk: SharedBlock) -> None: ...
105-
def add_index_reference(self, index: object) -> None: ...
105+
def add_index_reference(self, index: Index) -> None: ...
106106
def has_reference(self) -> bool: ...

pandas/_libs/internals.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ cdef class BlockValuesRefs:
966966

967967
Parameters
968968
----------
969-
index: object
969+
index : Index
970970
The index that the new reference should point to.
971971
"""
972972
self.referenced_blocks.append(weakref.ref(index))

pandas/_libs/lib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ def maybe_convert_objects(ndarray[object] objects,
26122612
return tdi._data._ndarray
26132613
seen.object_ = True
26142614

2615-
if seen.period_:
2615+
elif seen.period_:
26162616
if is_period_array(objects):
26172617
from pandas import PeriodIndex
26182618
pi = PeriodIndex(objects)
@@ -2621,7 +2621,7 @@ def maybe_convert_objects(ndarray[object] objects,
26212621
return pi._data
26222622
seen.object_ = True
26232623

2624-
if seen.interval_:
2624+
elif seen.interval_:
26252625
if is_interval_array(objects):
26262626
from pandas import IntervalIndex
26272627
ii = IntervalIndex(objects)
@@ -2631,7 +2631,7 @@ def maybe_convert_objects(ndarray[object] objects,
26312631

26322632
seen.object_ = True
26332633

2634-
if seen.nat_:
2634+
elif seen.nat_:
26352635
if not seen.object_ and not seen.numeric_ and not seen.bool_:
26362636
# all NaT, None, or nan (at least one NaT)
26372637
# see GH#49340 for discussion of desired behavior

pandas/_testing/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,7 @@ def _constructor_sliced(self):
852852

853853

854854
class SubclassedCategorical(Categorical):
855-
@property
856-
def _constructor(self):
857-
return SubclassedCategorical
855+
pass
858856

859857

860858
def _make_skipna_wrapper(alternative, skipna_alternative=None):

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ def take(
12401240
if not is_array_like(arr):
12411241
arr = np.asarray(arr)
12421242

1243-
indices = np.asarray(indices, dtype=np.intp)
1243+
indices = ensure_platform_int(indices)
12441244

12451245
if allow_fill:
12461246
# Pandas style, -1 means NA

pandas/core/apply.py

-5
Original file line numberDiff line numberDiff line change
@@ -1086,15 +1086,10 @@ def agg(self):
10861086
result = super().agg()
10871087
if result is None:
10881088
f = self.f
1089-
kwargs = self.kwargs
10901089

10911090
# string, list-like, and dict-like are entirely handled in super
10921091
assert callable(f)
10931092

1094-
# we can be called from an inner function which
1095-
# passes this meta-data
1096-
kwargs.pop("_level", None)
1097-
10981093
# try a regular apply, this evaluates lambdas
10991094
# row-by-row; however if the lambda is expected a Series
11001095
# expression, e.g.: lambda x: x-x.quantile(0.25)

pandas/core/array_algos/putmask.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def setitem_datetimelike_compat(values: np.ndarray, num_set: int, other):
138138
if values.dtype == object:
139139
dtype, _ = infer_dtype_from(other)
140140

141-
if isinstance(dtype, np.dtype) and dtype.kind in "mM":
141+
if lib.is_np_dtype(dtype, "mM"):
142142
# https://github.com/numpy/numpy/issues/12550
143143
# timedelta64 will incorrectly cast to int
144144
if not is_list_like(other):

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ def _validate_listlike(self, value):
20222022
"Cannot set a Categorical with another, "
20232023
"without identical categories"
20242024
)
2025-
# is_dtype_equal implies categories_match_up_to_permutation
2025+
# dtype equality implies categories_match_up_to_permutation
20262026
value = self._encode_with_my_categories(value)
20272027
return value._codes
20282028

pandas/core/arrays/datetimes.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363

6464
from pandas.core.arrays import datetimelike as dtl
6565
from pandas.core.arrays._ranges import generate_regular_range
66-
from pandas.core.arrays.sparse.dtype import SparseDtype
6766
import pandas.core.common as com
6867

6968
from pandas.tseries.frequencies import get_period_alias
@@ -2035,11 +2034,7 @@ def _sequence_to_dt64ns(
20352034
if out_unit is not None:
20362035
out_dtype = np.dtype(f"M8[{out_unit}]")
20372036

2038-
if (
2039-
data_dtype == object
2040-
or is_string_dtype(data_dtype)
2041-
or isinstance(data_dtype, SparseDtype)
2042-
):
2037+
if data_dtype == object or is_string_dtype(data_dtype):
20432038
# TODO: We do not have tests specific to string-dtypes,
20442039
# also complex or categorical or other extension
20452040
copy = False

pandas/core/arrays/masked.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
pandas_dtype,
4949
)
5050
from pandas.core.dtypes.dtypes import BaseMaskedDtype
51-
from pandas.core.dtypes.inference import is_array_like
5251
from pandas.core.dtypes.missing import (
5352
array_equivalent,
5453
is_valid_na_for_dtype,
@@ -172,20 +171,13 @@ def __getitem__(self, item: PositionalIndexer) -> Self | Any:
172171

173172
return type(self)(self._data[item], newmask)
174173

175-
@doc(ExtensionArray.fillna)
176174
@doc(ExtensionArray.fillna)
177175
def fillna(self, value=None, method=None, limit: int | None = None) -> Self:
178176
value, method = validate_fillna_kwargs(value, method)
179177

180178
mask = self._mask
181179

182-
if is_array_like(value):
183-
if len(value) != len(self):
184-
raise ValueError(
185-
f"Length of 'value' does not match. Got ({len(value)}) "
186-
f" expected {len(self)}"
187-
)
188-
value = value[mask]
180+
value = missing.check_value_size(value, mask, len(self))
189181

190182
if mask.any():
191183
if method is not None:

pandas/core/flags.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING
34
import weakref
45

6+
if TYPE_CHECKING:
7+
from pandas.core.generic import NDFrame
8+
59

610
class Flags:
711
"""
@@ -44,9 +48,9 @@ class Flags:
4448
<Flags(allows_duplicate_labels=True)>
4549
"""
4650

47-
_keys = {"allows_duplicate_labels"}
51+
_keys: set[str] = {"allows_duplicate_labels"}
4852

49-
def __init__(self, obj, *, allows_duplicate_labels) -> None:
53+
def __init__(self, obj: NDFrame, *, allows_duplicate_labels: bool) -> None:
5054
self._allows_duplicate_labels = allows_duplicate_labels
5155
self._obj = weakref.ref(obj)
5256

@@ -95,21 +99,21 @@ def allows_duplicate_labels(self, value: bool) -> None:
9599

96100
self._allows_duplicate_labels = value
97101

98-
def __getitem__(self, key):
102+
def __getitem__(self, key: str):
99103
if key not in self._keys:
100104
raise KeyError(key)
101105

102106
return getattr(self, key)
103107

104-
def __setitem__(self, key, value) -> None:
108+
def __setitem__(self, key: str, value) -> None:
105109
if key not in self._keys:
106110
raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}")
107111
setattr(self, key, value)
108112

109113
def __repr__(self) -> str:
110114
return f"<Flags(allows_duplicate_labels={self.allows_duplicate_labels})>"
111115

112-
def __eq__(self, other):
116+
def __eq__(self, other) -> bool:
113117
if isinstance(other, type(self)):
114118
return self.allows_duplicate_labels == other.allows_duplicate_labels
115119
return False

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6546,7 +6546,7 @@ def sort_values(
65466546
axis: Axis = ...,
65476547
ascending=...,
65486548
inplace: Literal[True],
6549-
kind: str = ...,
6549+
kind: SortKind = ...,
65506550
na_position: str = ...,
65516551
ignore_index: bool = ...,
65526552
key: ValueKeyFunc = ...,
@@ -6560,7 +6560,7 @@ def sort_values(
65606560
axis: Axis = 0,
65616561
ascending: bool | list[bool] | tuple[bool, ...] = True,
65626562
inplace: bool = False,
6563-
kind: str = "quicksort",
6563+
kind: SortKind = "quicksort",
65646564
na_position: str = "last",
65656565
ignore_index: bool = False,
65666566
key: ValueKeyFunc = None,

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6206,7 +6206,7 @@ def _check_inplace_setting(self, value) -> bool_t:
62066206
"""check whether we allow in-place setting with this type of value"""
62076207
if self._is_mixed_type and not self._mgr.is_numeric_mixed_type:
62086208
# allow an actual np.nan through
6209-
if is_float(value) and np.isnan(value) or value is lib.no_default:
6209+
if (is_float(value) and np.isnan(value)) or value is lib.no_default:
62106210
return True
62116211

62126212
raise TypeError(

pandas/core/indexes/base.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -2868,8 +2868,9 @@ def fillna(self, value=None, downcast=None):
28682868
DataFrame.fillna : Fill NaN values of a DataFrame.
28692869
Series.fillna : Fill NaN Values of a Series.
28702870
"""
2871+
if not is_scalar(value):
2872+
raise TypeError(f"'value' must be a scalar, passed: {type(value).__name__}")
28712873

2872-
value = self._require_scalar(value)
28732874
if self.hasnans:
28742875
result = self.putmask(self._isnan, value)
28752876
if downcast is None:
@@ -3211,7 +3212,7 @@ def union(self, other, sort=None):
32113212

32123213
elif not len(other) or self.equals(other):
32133214
# NB: whether this (and the `if not len(self)` check below) come before
3214-
# or after the is_dtype_equal check above affects the returned dtype
3215+
# or after the dtype equality check above affects the returned dtype
32153216
result = self._get_reconciled_name_object(other)
32163217
if sort is True:
32173218
return result.sort_values()
@@ -5119,16 +5120,6 @@ def _validate_fill_value(self, value):
51195120
raise TypeError
51205121
return value
51215122

5122-
@final
5123-
def _require_scalar(self, value):
5124-
"""
5125-
Check that this is a scalar value that we can use for setitem-like
5126-
operations without changing dtype.
5127-
"""
5128-
if not is_scalar(value):
5129-
raise TypeError(f"'value' must be a scalar, passed: {type(value).__name__}")
5130-
return value
5131-
51325123
def _is_memory_usage_qualified(self) -> bool:
51335124
"""
51345125
Return a boolean if we need a qualified .info display.

pandas/core/indexes/multi.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1116,11 +1116,7 @@ def _engine(self):
11161116
# calculating the indexer are shifted to 0
11171117
sizes = np.ceil(
11181118
np.log2(
1119-
[
1120-
len(level)
1121-
+ libindex.multiindex_nulls_shift # type: ignore[attr-defined]
1122-
for level in self.levels
1123-
]
1119+
[len(level) + libindex.multiindex_nulls_shift for level in self.levels]
11241120
)
11251121
)
11261122

pandas/core/internals/blocks.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,7 @@ def should_store(self, value: ArrayLike) -> bool:
309309
-------
310310
bool
311311
"""
312-
# faster equivalent to is_dtype_equal(value.dtype, self.dtype)
313-
try:
314-
return value.dtype == self.dtype
315-
except TypeError:
316-
return False
312+
return value.dtype == self.dtype
317313

318314
# ---------------------------------------------------------------------
319315
# Apply/Reduce and Helpers

pandas/core/ops/invalid.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
from __future__ import annotations
55

66
import operator
7+
from typing import TYPE_CHECKING
78

89
import numpy as np
910

11+
if TYPE_CHECKING:
12+
from pandas._typing import npt
1013

11-
def invalid_comparison(left, right, op) -> np.ndarray:
14+
15+
def invalid_comparison(left, right, op) -> npt.NDArray[np.bool_]:
1216
"""
1317
If a comparison has mismatched types and is not necessarily meaningful,
1418
follow python3 conventions by:

pandas/core/ops/missing.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@
2727

2828
import numpy as np
2929

30-
from pandas.core.dtypes.common import (
31-
is_float_dtype,
32-
is_integer_dtype,
33-
is_scalar,
34-
)
35-
3630
from pandas.core import roperator
3731

3832

39-
def _fill_zeros(result, x, y):
33+
def _fill_zeros(result: np.ndarray, x, y):
4034
"""
4135
If this is a reversed op, then flip x,y
4236
@@ -46,11 +40,11 @@ def _fill_zeros(result, x, y):
4640
4741
Mask the nan's from x.
4842
"""
49-
if is_float_dtype(result.dtype):
43+
if result.dtype.kind == "f":
5044
return result
5145

5246
is_variable_type = hasattr(y, "dtype")
53-
is_scalar_type = is_scalar(y)
47+
is_scalar_type = not isinstance(y, np.ndarray)
5448

5549
if not is_variable_type and not is_scalar_type:
5650
# e.g. test_series_ops_name_retention with mod we get here with list/tuple
@@ -59,7 +53,7 @@ def _fill_zeros(result, x, y):
5953
if is_scalar_type:
6054
y = np.array(y)
6155

62-
if is_integer_dtype(y.dtype):
56+
if y.dtype.kind in "iu":
6357
ymask = y == 0
6458
if ymask.any():
6559
# GH#7325, mask and nans must be broadcastable
@@ -143,7 +137,9 @@ def dispatch_fill_zeros(op, left, right, result):
143137
----------
144138
op : function (operator.add, operator.div, ...)
145139
left : object (np.ndarray for non-reversed ops)
140+
We have excluded ExtensionArrays here
146141
right : object (np.ndarray for reversed ops)
142+
We have excluded ExtensionArrays here
147143
result : ndarray
148144
149145
Returns

0 commit comments

Comments
 (0)