Skip to content

Commit 7d55808

Browse files
authored
Merge branch 'main' into pd-cut-doc
2 parents e4eae7f + 04424b0 commit 7d55808

File tree

10 files changed

+64
-24
lines changed

10 files changed

+64
-24
lines changed

doc/source/user_guide/text.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ and replacing any remaining whitespaces with underscores:
204204

205205
.. warning::
206206

207-
The type of the Series is inferred and the allowed types (i.e. strings).
207+
The type of the Series is inferred and is one among the allowed types (i.e. strings).
208208

209209
Generally speaking, the ``.str`` accessor is intended to work only on strings. With very few
210210
exceptions, other uses are not supported, and may be disabled at a later point.

pandas/_typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,5 @@ def closed(self) -> bool:
526526

527527
# maintaine the sub-type of any hashable sequence
528528
SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])
529+
530+
SliceType = Optional[Hashable]

pandas/core/generic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,14 +1491,12 @@ def __invert__(self) -> Self:
14911491
return res.__finalize__(self, method="__invert__")
14921492

14931493
@final
1494-
def __nonzero__(self) -> NoReturn:
1494+
def __bool__(self) -> NoReturn:
14951495
raise ValueError(
14961496
f"The truth value of a {type(self).__name__} is ambiguous. "
14971497
"Use a.empty, a.bool(), a.item(), a.any() or a.all()."
14981498
)
14991499

1500-
__bool__ = __nonzero__
1501-
15021500
@final
15031501
def abs(self) -> Self:
15041502
"""

pandas/core/indexes/base.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
ArrayLike,
4646
Axes,
4747
Axis,
48+
AxisInt,
4849
DropKeep,
50+
Dtype,
4951
DtypeObj,
5052
F,
5153
IgnoreRaise,
@@ -57,6 +59,7 @@
5759
ReindexMethod,
5860
Self,
5961
Shape,
62+
SliceType,
6063
npt,
6164
)
6265
from pandas.compat.numpy import function as nv
@@ -1087,7 +1090,7 @@ def view(self, cls=None):
10871090
result._id = self._id
10881091
return result
10891092

1090-
def astype(self, dtype, copy: bool = True):
1093+
def astype(self, dtype: Dtype, copy: bool = True):
10911094
"""
10921095
Create an Index with values cast to dtypes.
10931096
@@ -2907,14 +2910,12 @@ def __iadd__(self, other):
29072910
return self + other
29082911

29092912
@final
2910-
def __nonzero__(self) -> NoReturn:
2913+
def __bool__(self) -> NoReturn:
29112914
raise ValueError(
29122915
f"The truth value of a {type(self).__name__} is ambiguous. "
29132916
"Use a.empty, a.bool(), a.item(), a.any() or a.all()."
29142917
)
29152918

2916-
__bool__ = __nonzero__
2917-
29182919
# --------------------------------------------------------------------
29192920
# Set Operation Methods
29202921

@@ -2957,7 +2958,7 @@ def _dti_setop_align_tzs(self, other: Index, setop: str_t) -> tuple[Index, Index
29572958
return self, other
29582959

29592960
@final
2960-
def union(self, other, sort=None):
2961+
def union(self, other, sort: bool | None = None):
29612962
"""
29622963
Form the union of two Index objects.
29632964
@@ -3334,7 +3335,7 @@ def _intersection_via_get_indexer(
33343335
return result
33353336

33363337
@final
3337-
def difference(self, other, sort=None):
3338+
def difference(self, other, sort: bool | None = None):
33383339
"""
33393340
Return a new Index with elements of index not in `other`.
33403341
@@ -3420,7 +3421,12 @@ def _wrap_difference_result(self, other, result):
34203421
# We will override for MultiIndex to handle empty results
34213422
return self._wrap_setop_result(other, result)
34223423

3423-
def symmetric_difference(self, other, result_name=None, sort=None):
3424+
def symmetric_difference(
3425+
self,
3426+
other,
3427+
result_name: abc.Hashable | None = None,
3428+
sort: bool | None = None,
3429+
):
34243430
"""
34253431
Compute the symmetric difference of two Index objects.
34263432
@@ -6389,7 +6395,7 @@ def _transform_index(self, func, *, level=None) -> Index:
63896395
items = [func(x) for x in self]
63906396
return Index(items, name=self.name, tupleize_cols=False)
63916397

6392-
def isin(self, values, level=None) -> npt.NDArray[np.bool_]:
6398+
def isin(self, values, level: str_t | int | None = None) -> npt.NDArray[np.bool_]:
63936399
"""
63946400
Return a boolean array where the index values are in `values`.
63956401
@@ -6687,7 +6693,12 @@ def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
66876693
else:
66886694
return slc
66896695

6690-
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
6696+
def slice_locs(
6697+
self,
6698+
start: SliceType = None,
6699+
end: SliceType = None,
6700+
step: int | None = None,
6701+
) -> tuple[int, int]:
66916702
"""
66926703
Compute slice locations for input labels.
66936704
@@ -6781,7 +6792,9 @@ def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
67816792

67826793
return start_slice, end_slice
67836794

6784-
def delete(self, loc) -> Self:
6795+
def delete(
6796+
self, loc: int | np.integer | list[int] | npt.NDArray[np.integer]
6797+
) -> Self:
67856798
"""
67866799
Make new Index with passed location(-s) deleted.
67876800
@@ -7227,7 +7240,9 @@ def _maybe_disable_logical_methods(self, opname: str_t) -> None:
72277240
raise TypeError(f"cannot perform {opname} with {type(self).__name__}")
72287241

72297242
@Appender(IndexOpsMixin.argmin.__doc__)
7230-
def argmin(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
7243+
def argmin(
7244+
self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs
7245+
) -> int:
72317246
nv.validate_argmin(args, kwargs)
72327247
nv.validate_minmax_axis(axis)
72337248

@@ -7240,7 +7255,9 @@ def argmin(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
72407255
return super().argmin(skipna=skipna)
72417256

72427257
@Appender(IndexOpsMixin.argmax.__doc__)
7243-
def argmax(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
7258+
def argmax(
7259+
self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs
7260+
) -> int:
72447261
nv.validate_argmax(args, kwargs)
72457262
nv.validate_minmax_axis(axis)
72467263

@@ -7251,7 +7268,7 @@ def argmax(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
72517268
raise ValueError("Encountered all NA values")
72527269
return super().argmax(skipna=skipna)
72537270

7254-
def min(self, axis=None, skipna: bool = True, *args, **kwargs):
7271+
def min(self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs):
72557272
"""
72567273
Return the minimum value of the Index.
72577274
@@ -7314,7 +7331,7 @@ def min(self, axis=None, skipna: bool = True, *args, **kwargs):
73147331

73157332
return nanops.nanmin(self._values, skipna=skipna)
73167333

7317-
def max(self, axis=None, skipna: bool = True, *args, **kwargs):
7334+
def max(self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs):
73187335
"""
73197336
Return the maximum value of the Index.
73207337

pandas/core/indexes/category.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,13 @@ def __contains__(self, key: Any) -> bool:
377377
# if key is a NaN, check if any NaN is in self.
378378
if is_valid_na_for_dtype(key, self.categories.dtype):
379379
return self.hasnans
380-
381-
return contains(self, key, container=self._engine)
380+
if self.categories._typ == "rangeindex":
381+
container: Index | libindex.IndexEngine | libindex.ExtensionEngine = (
382+
self.categories
383+
)
384+
else:
385+
container = self._engine
386+
return contains(self, key, container=container)
382387

383388
def reindex(
384389
self, target, method=None, level=None, limit: int | None = None, tolerance=None

pandas/core/internals/managers.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,9 @@ def make_empty(self, axes=None) -> Self:
263263
blocks = []
264264
return type(self).from_blocks(blocks, axes)
265265

266-
def __nonzero__(self) -> bool:
266+
def __bool__(self) -> bool:
267267
return True
268268

269-
# Python3 compat
270-
__bool__ = __nonzero__
271-
272269
def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
273270
# Caller is responsible for ensuring we have an Index object.
274271
self._validate_set_axis(axis, new_labels)

pandas/tests/indexes/categorical/test_category.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
class TestCategoricalIndex:
2222
@pytest.fixture
2323
def simple_index(self) -> CategoricalIndex:
24+
"""
25+
Fixture that provides a CategoricalIndex.
26+
"""
2427
return CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
2528

2629
def test_can_hold_identifiers(self):
@@ -392,3 +395,10 @@ def test_remove_maintains_order(self):
392395
["a", "b", np.nan, "d", "d", "a"], categories=list("dba"), ordered=True
393396
),
394397
)
398+
399+
400+
def test_contains_rangeindex_categories_no_engine():
401+
ci = CategoricalIndex(range(3))
402+
assert 2 in ci
403+
assert 5 not in ci
404+
assert "_engine" not in ci._cache

pandas/tests/indexes/interval/test_constructors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class TestFromArrays(ConstructorTests):
210210

211211
@pytest.fixture
212212
def constructor(self):
213+
"""Fixture for IntervalIndex.from_arrays constructor"""
213214
return IntervalIndex.from_arrays
214215

215216
def get_kwargs_from_breaks(self, breaks, closed="right"):
@@ -282,6 +283,7 @@ class TestFromBreaks(ConstructorTests):
282283

283284
@pytest.fixture
284285
def constructor(self):
286+
"""Fixture for IntervalIndex.from_breaks constructor"""
285287
return IntervalIndex.from_breaks
286288

287289
def get_kwargs_from_breaks(self, breaks, closed="right"):
@@ -320,6 +322,7 @@ class TestFromTuples(ConstructorTests):
320322

321323
@pytest.fixture
322324
def constructor(self):
325+
"""Fixture for IntervalIndex.from_tuples constructor"""
323326
return IntervalIndex.from_tuples
324327

325328
def get_kwargs_from_breaks(self, breaks, closed="right"):
@@ -370,6 +373,7 @@ class TestClassConstructors(ConstructorTests):
370373

371374
@pytest.fixture
372375
def constructor(self):
376+
"""Fixture for IntervalIndex class constructor"""
373377
return IntervalIndex
374378

375379
def get_kwargs_from_breaks(self, breaks, closed="right"):

pandas/tests/indexing/interval/test_interval_new.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
class TestIntervalIndex:
1818
@pytest.fixture
1919
def series_with_interval_index(self):
20+
"""
21+
Fixture providing a Series with an IntervalIndex.
22+
"""
2023
return Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6)))
2124

2225
def test_loc_with_interval(self, series_with_interval_index, indexer_sl):

pandas/tests/io/json/test_json_table_schema_ext_dtype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,22 @@ def test_as_json_table_type_ext_integer_dtype(self):
9797
class TestTableOrient:
9898
@pytest.fixture
9999
def da(self):
100+
"""Fixture for creating a DateArray."""
100101
return DateArray([dt.date(2021, 10, 10)])
101102

102103
@pytest.fixture
103104
def dc(self):
105+
"""Fixture for creating a DecimalArray."""
104106
return DecimalArray([decimal.Decimal(10)])
105107

106108
@pytest.fixture
107109
def sa(self):
110+
"""Fixture for creating a StringDtype array."""
108111
return array(["pandas"], dtype="string")
109112

110113
@pytest.fixture
111114
def ia(self):
115+
"""Fixture for creating an Int64Dtype array."""
112116
return array([10], dtype="Int64")
113117

114118
def test_build_date_series(self, da):

0 commit comments

Comments
 (0)