diff --git a/asv_bench/benchmarks/index_cached_properties.py b/asv_bench/benchmarks/index_cached_properties.py index 16fbc741775e4..1020d07e5a716 100644 --- a/asv_bench/benchmarks/index_cached_properties.py +++ b/asv_bench/benchmarks/index_cached_properties.py @@ -56,9 +56,6 @@ def time_values(self, index_type): def time_shape(self, index_type): self.idx.shape - def time_is_monotonic(self, index_type): - self.idx.is_monotonic - def time_is_monotonic_decreasing(self, index_type): self.idx.is_monotonic_decreasing diff --git a/asv_bench/benchmarks/multiindex_object.py b/asv_bench/benchmarks/multiindex_object.py index 25df5b0214959..3d7e4b9b4c787 100644 --- a/asv_bench/benchmarks/multiindex_object.py +++ b/asv_bench/benchmarks/multiindex_object.py @@ -112,7 +112,7 @@ def time_get_indexer_and_pad(self): self.mi_int.get_indexer(self.other_mi_many_mismatches, method="pad") def time_is_monotonic(self): - self.mi_int.is_monotonic + self.mi_int.is_monotonic_increasing class Duplicated: diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index e79bb0c50dfd8..24fde538bfc56 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -147,6 +147,7 @@ Other Deprecations - Deprecated behavior of :meth:`SparseArray.astype`, :meth:`Series.astype`, and :meth:`DataFrame.astype` with :class:`SparseDtype` when passing a non-sparse ``dtype``. In a future version, this will cast to that non-sparse dtype instead of wrapping it in a :class:`SparseDtype` (:issue:`34457`) - Deprecated behavior of :meth:`DatetimeIndex.intersection` and :meth:`DatetimeIndex.symmetric_difference` (``union`` behavior was already deprecated in version 1.3.0) with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`, :issue:`45357`) - Deprecated :meth:`DataFrame.iteritems`, :meth:`Series.iteritems`, :meth:`HDFStore.iteritems` in favor of :meth:`DataFrame.items`, :meth:`Series.items`, :meth:`HDFStore.items` (:issue:`45321`) +- Deprecated :meth:`Series.is_monotonic` and :meth:`Index.is_monotonic` in favor of :meth:`Series.is_monotonic_increasing` and :meth:`Index.is_monotonic_increasing` (:issue:`45422`, :issue:`21335`) - Deprecated the ``__array_wrap__`` method of DataFrame and Series, rely on standard numpy ufuncs instead (:issue:`45451`) - diff --git a/pandas/core/base.py b/pandas/core/base.py index 075b1437992a4..84bc6cb161bec 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -16,6 +16,7 @@ final, overload, ) +import warnings import numpy as np @@ -35,6 +36,7 @@ cache_readonly, doc, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_categorical_dtype, @@ -1050,17 +1052,27 @@ def is_monotonic(self) -> bool: ------- bool """ - from pandas import Index - - return Index(self).is_monotonic + warnings.warn( + "is_monotonic is deprecated and will be removed in a future version. " + "Use is_monotonic_increasing instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + return self.is_monotonic_increasing @property def is_monotonic_increasing(self) -> bool: """ - Alias for is_monotonic. + Return boolean if values in the object are + monotonic_increasing. + + Returns + ------- + bool """ - # mypy complains if we alias directly - return self.is_monotonic + from pandas import Index + + return Index(self).is_monotonic_increasing @property def is_monotonic_decreasing(self) -> bool: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ac78a163a6f89..4971c7f34cfdb 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7161,7 +7161,7 @@ def asof(self, where, subset=None): if isinstance(where, str): where = Timestamp(where) - if not self.index.is_monotonic: + if not self.index.is_monotonic_increasing: raise ValueError("asof requires a sorted index") is_series = isinstance(self, ABCSeries) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 3cf56af18c076..af60192676597 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -400,7 +400,7 @@ def _set_grouper(self, obj: NDFrame, sort: bool = False): raise ValueError(f"The level {level} is not valid") # possibly sort - if (self.sort or sort) and not ax.is_monotonic: + if (self.sort or sort) and not ax.is_monotonic_increasing: # use stable sort to support first, last, nth # TODO: why does putting na_position="first" fix datetimelike cases? indexer = self.indexer = ax.array.argsort( diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 0e8a7a5b7d9a9..467da586f604d 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -823,7 +823,7 @@ def groups(self) -> dict[Hashable, np.ndarray]: @cache_readonly def is_monotonic(self) -> bool: # return if my group orderings are monotonic - return Index(self.group_info[0]).is_monotonic + return Index(self.group_info[0]).is_monotonic_increasing @cache_readonly def group_info(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 3b3b896765976..d17e8f69a8a65 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2172,6 +2172,12 @@ def is_monotonic(self) -> bool: """ Alias for is_monotonic_increasing. """ + warnings.warn( + "is_monotonic is deprecated and will be removed in a future version. " + "Use is_monotonic_increasing instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) return self.is_monotonic_increasing @property @@ -3235,8 +3241,8 @@ def _union(self, other: Index, sort): if ( sort is None - and self.is_monotonic - and other.is_monotonic + and self.is_monotonic_increasing + and other.is_monotonic_increasing and not (self.has_duplicates and other.has_duplicates) and self._can_use_libjoin ): @@ -3274,7 +3280,7 @@ def _union(self, other: Index, sort): else: result = lvals - if not self.is_monotonic or not other.is_monotonic: + if not self.is_monotonic_increasing or not other.is_monotonic_increasing: # if both are monotonic then result should already be sorted result = _maybe_try_sort(result, sort) @@ -3379,7 +3385,11 @@ def _intersection(self, other: Index, sort=False): """ intersection specialized to the case with matching dtypes. """ - if self.is_monotonic and other.is_monotonic and self._can_use_libjoin: + if ( + self.is_monotonic_increasing + and other.is_monotonic_increasing + and self._can_use_libjoin + ): try: result = self._inner_indexer(other)[0] except TypeError: @@ -4475,15 +4485,15 @@ def join( if not self.is_unique and not other.is_unique: return self._join_non_unique(other, how=how) elif not self.is_unique or not other.is_unique: - if self.is_monotonic and other.is_monotonic: + if self.is_monotonic_increasing and other.is_monotonic_increasing: if self._can_use_libjoin: # otherwise we will fall through to _join_via_get_indexer return self._join_monotonic(other, how=how) else: return self._join_non_unique(other, how=how) elif ( - self.is_monotonic - and other.is_monotonic + self.is_monotonic_increasing + and other.is_monotonic_increasing and self._can_use_libjoin and ( not isinstance(self, ABCMultiIndex) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 73b7cf3006994..d65062419477c 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1557,7 +1557,7 @@ def is_monotonic_increasing(self) -> bool: if any(-1 in code for code in self.codes): return False - if all(level.is_monotonic for level in self.levels): + if all(level.is_monotonic_increasing for level in self.levels): # If each level is sorted, we can operate on the codes directly. GH27495 return libalgos.is_lexsorted( [x.astype("int64", copy=False) for x in self.codes] @@ -1574,11 +1574,11 @@ def is_monotonic_increasing(self) -> bool: # int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, # complex, str, bytes]]]" [arg-type] sort_order = np.lexsort(values) # type: ignore[arg-type] - return Index(sort_order).is_monotonic + return Index(sort_order).is_monotonic_increasing except TypeError: # we have mixed types and np.lexsort is not happy - return Index(self._values).is_monotonic + return Index(self._values).is_monotonic_increasing @cache_readonly def is_monotonic_decreasing(self) -> bool: @@ -1946,7 +1946,7 @@ def _sort_levels_monotonic(self) -> MultiIndex: ('b', 'bb')], ) """ - if self._is_lexsorted() and self.is_monotonic: + if self._is_lexsorted() and self.is_monotonic_increasing: return self new_levels = [] @@ -1954,7 +1954,7 @@ def _sort_levels_monotonic(self) -> MultiIndex: for lev, level_codes in zip(self.levels, self.codes): - if not lev.is_monotonic: + if not lev.is_monotonic_increasing: try: # indexer to reorder the levels indexer = lev.argsort() diff --git a/pandas/core/missing.py b/pandas/core/missing.py index e09701e69c62c..d922cfd06f7ac 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -161,7 +161,7 @@ def clean_interp_method(method: str, index: Index, **kwargs) -> str: raise ValueError(f"method must be one of {valid}. Got '{method}' instead.") if method in ("krogh", "piecewise_polynomial", "pchip"): - if not index.is_monotonic: + if not index.is_monotonic_increasing: raise ValueError( f"{method} interpolation requires that the index be monotonic." ) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index c010addd10404..9690efb57e87b 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1929,14 +1929,14 @@ def flip(xs) -> np.ndarray: tolerance = self.tolerance # we require sortedness and non-null values in the join keys - if not Index(left_values).is_monotonic: + if not Index(left_values).is_monotonic_increasing: side = "left" if isna(left_values).any(): raise ValueError(f"Merge keys contain null values on {side} side") else: raise ValueError(f"{side} keys must be sorted") - if not Index(right_values).is_monotonic: + if not Index(right_values).is_monotonic_increasing: side = "right" if isna(right_values).any(): raise ValueError(f"Merge keys contain null values on {side} side") diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index 99ff0f04afd60..bc5c68e4ec18d 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -25,7 +25,7 @@ def test_sort_index_and_reconstruction_doc_example(self): ), ) assert df.index._is_lexsorted() - assert not df.index.is_monotonic + assert not df.index.is_monotonic_increasing # sort it expected = DataFrame( @@ -35,15 +35,13 @@ def test_sort_index_and_reconstruction_doc_example(self): ), ) result = df.sort_index() - assert result.index.is_monotonic - + assert result.index.is_monotonic_increasing tm.assert_frame_equal(result, expected) # reconstruct result = df.sort_index().copy() result.index = result.index._sort_levels_monotonic() - assert result.index.is_monotonic - + assert result.index.is_monotonic_increasing tm.assert_frame_equal(result, expected) def test_sort_index_non_existent_label_multiindex(self): @@ -51,7 +49,7 @@ def test_sort_index_non_existent_label_multiindex(self): df = DataFrame(0, columns=[], index=MultiIndex.from_product([[], []])) df.loc["b", "2"] = 1 df.loc["a", "3"] = 1 - result = df.sort_index().index.is_monotonic + result = df.sort_index().index.is_monotonic_increasing assert result is True def test_sort_index_reorder_on_ops(self): @@ -549,7 +547,7 @@ def test_sort_index_and_reconstruction(self): index=MultiIndex.from_product([[0.5, 0.8], list("ab")]), ) result = result.sort_index() - assert result.index.is_monotonic + assert result.index.is_monotonic_increasing tm.assert_frame_equal(result, expected) @@ -567,7 +565,7 @@ def test_sort_index_and_reconstruction(self): concatted = pd.concat([df, df], keys=[0.8, 0.5]) result = concatted.sort_index() - assert result.index.is_monotonic + assert result.index.is_monotonic_increasing tm.assert_frame_equal(result, expected) @@ -583,11 +581,11 @@ def test_sort_index_and_reconstruction(self): df.columns = df.columns.set_levels( pd.to_datetime(df.columns.levels[1]), level=1 ) - assert not df.columns.is_monotonic + assert not df.columns.is_monotonic_increasing result = df.sort_index(axis=1) - assert result.columns.is_monotonic + assert result.columns.is_monotonic_increasing result = df.sort_index(axis=1, level=1) - assert result.columns.is_monotonic + assert result.columns.is_monotonic_increasing # TODO: better name, de-duplicate with test_sort_index_level above def test_sort_index_level2(self, multiindex_dataframe_random_data): diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 617edc44f6a58..cd20c379fde38 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1530,7 +1530,7 @@ def test_constructor_mixed_dict_and_Series(self): data["B"] = Series([4, 3, 2, 1], index=["bar", "qux", "baz", "foo"]) result = DataFrame(data) - assert result.index.is_monotonic + assert result.index.is_monotonic_increasing # ordering ambiguous, raise exception with pytest.raises(ValueError, match="ambiguous ordering"): diff --git a/pandas/tests/indexes/datetimelike_/test_is_monotonic.py b/pandas/tests/indexes/datetimelike_/test_is_monotonic.py index 22247c982edbc..088ccc406cb81 100644 --- a/pandas/tests/indexes/datetimelike_/test_is_monotonic.py +++ b/pandas/tests/indexes/datetimelike_/test_is_monotonic.py @@ -7,7 +7,7 @@ def test_is_monotonic_with_nat(): # GH#31437 - # PeriodIndex.is_monotonic should behave analogously to DatetimeIndex, + # PeriodIndex.is_monotonic_increasing should behave analogously to DatetimeIndex, # in particular never be monotonic when we have NaT dti = date_range("2016-01-01", periods=3) pi = dti.to_period("D") @@ -16,7 +16,7 @@ def test_is_monotonic_with_nat(): for obj in [pi, pi._engine, dti, dti._engine, tdi, tdi._engine]: if isinstance(obj, Index): # i.e. not Engines - assert obj.is_monotonic + assert obj.is_monotonic_increasing assert obj.is_monotonic_increasing assert not obj.is_monotonic_decreasing assert obj.is_unique @@ -28,7 +28,7 @@ def test_is_monotonic_with_nat(): for obj in [pi1, pi1._engine, dti1, dti1._engine, tdi1, tdi1._engine]: if isinstance(obj, Index): # i.e. not Engines - assert not obj.is_monotonic + assert not obj.is_monotonic_increasing assert not obj.is_monotonic_increasing assert not obj.is_monotonic_decreasing assert obj.is_unique @@ -40,7 +40,7 @@ def test_is_monotonic_with_nat(): for obj in [pi2, pi2._engine, dti2, dti2._engine, tdi2, tdi2._engine]: if isinstance(obj, Index): # i.e. not Engines - assert not obj.is_monotonic + assert not obj.is_monotonic_increasing assert not obj.is_monotonic_increasing assert not obj.is_monotonic_decreasing assert obj.is_unique diff --git a/pandas/tests/indexes/datetimelike_/test_sort_values.py b/pandas/tests/indexes/datetimelike_/test_sort_values.py index 9a91cc26c1430..6b7ad79e8f76d 100644 --- a/pandas/tests/indexes/datetimelike_/test_sort_values.py +++ b/pandas/tests/indexes/datetimelike_/test_sort_values.py @@ -59,17 +59,16 @@ def test_argmin_argmax(self, non_monotonic_idx): def test_sort_values(self, non_monotonic_idx): idx = non_monotonic_idx ordered = idx.sort_values() - assert ordered.is_monotonic - + assert ordered.is_monotonic_increasing ordered = idx.sort_values(ascending=False) - assert ordered[::-1].is_monotonic + assert ordered[::-1].is_monotonic_increasing ordered, dexer = idx.sort_values(return_indexer=True) - assert ordered.is_monotonic + assert ordered.is_monotonic_increasing tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0], dtype=np.intp)) ordered, dexer = idx.sort_values(return_indexer=True, ascending=False) - assert ordered[::-1].is_monotonic + assert ordered[::-1].is_monotonic_increasing tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp)) def check_sort_values_with_freq(self, idx): diff --git a/pandas/tests/indexes/datetimes/test_join.py b/pandas/tests/indexes/datetimes/test_join.py index 8b633e8db8836..9afeb7ce924df 100644 --- a/pandas/tests/indexes/datetimes/test_join.py +++ b/pandas/tests/indexes/datetimes/test_join.py @@ -89,7 +89,7 @@ def test_join_nonunique(self): idx1 = to_datetime(["2012-11-06 16:00:11.477563", "2012-11-06 16:00:11.477563"]) idx2 = to_datetime(["2012-11-06 15:11:09.006507", "2012-11-06 15:11:09.006507"]) rs = idx1.join(idx2, how="outer") - assert rs.is_monotonic + assert rs.is_monotonic_increasing @pytest.mark.parametrize("freq", ["B", "C"]) def test_outer_join(self, freq): diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 843885832690f..37c13c37d070b 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -259,70 +259,70 @@ def test_is_unique_interval(self, closed): def test_monotonic(self, closed): # increasing non-overlapping idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)], closed=closed) - assert idx.is_monotonic is True + assert idx.is_monotonic_increasing is True assert idx._is_strictly_monotonic_increasing is True assert idx.is_monotonic_decreasing is False assert idx._is_strictly_monotonic_decreasing is False # decreasing non-overlapping idx = IntervalIndex.from_tuples([(4, 5), (2, 3), (1, 2)], closed=closed) - assert idx.is_monotonic is False + assert idx.is_monotonic_increasing is False assert idx._is_strictly_monotonic_increasing is False assert idx.is_monotonic_decreasing is True assert idx._is_strictly_monotonic_decreasing is True # unordered non-overlapping idx = IntervalIndex.from_tuples([(0, 1), (4, 5), (2, 3)], closed=closed) - assert idx.is_monotonic is False + assert idx.is_monotonic_increasing is False assert idx._is_strictly_monotonic_increasing is False assert idx.is_monotonic_decreasing is False assert idx._is_strictly_monotonic_decreasing is False # increasing overlapping idx = IntervalIndex.from_tuples([(0, 2), (0.5, 2.5), (1, 3)], closed=closed) - assert idx.is_monotonic is True + assert idx.is_monotonic_increasing is True assert idx._is_strictly_monotonic_increasing is True assert idx.is_monotonic_decreasing is False assert idx._is_strictly_monotonic_decreasing is False # decreasing overlapping idx = IntervalIndex.from_tuples([(1, 3), (0.5, 2.5), (0, 2)], closed=closed) - assert idx.is_monotonic is False + assert idx.is_monotonic_increasing is False assert idx._is_strictly_monotonic_increasing is False assert idx.is_monotonic_decreasing is True assert idx._is_strictly_monotonic_decreasing is True # unordered overlapping idx = IntervalIndex.from_tuples([(0.5, 2.5), (0, 2), (1, 3)], closed=closed) - assert idx.is_monotonic is False + assert idx.is_monotonic_increasing is False assert idx._is_strictly_monotonic_increasing is False assert idx.is_monotonic_decreasing is False assert idx._is_strictly_monotonic_decreasing is False # increasing overlapping shared endpoints idx = IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed) - assert idx.is_monotonic is True + assert idx.is_monotonic_increasing is True assert idx._is_strictly_monotonic_increasing is True assert idx.is_monotonic_decreasing is False assert idx._is_strictly_monotonic_decreasing is False # decreasing overlapping shared endpoints idx = IntervalIndex.from_tuples([(2, 3), (1, 3), (1, 2)], closed=closed) - assert idx.is_monotonic is False + assert idx.is_monotonic_increasing is False assert idx._is_strictly_monotonic_increasing is False assert idx.is_monotonic_decreasing is True assert idx._is_strictly_monotonic_decreasing is True # stationary idx = IntervalIndex.from_tuples([(0, 1), (0, 1)], closed=closed) - assert idx.is_monotonic is True + assert idx.is_monotonic_increasing is True assert idx._is_strictly_monotonic_increasing is False assert idx.is_monotonic_decreasing is True assert idx._is_strictly_monotonic_decreasing is False # empty idx = IntervalIndex([], closed=closed) - assert idx.is_monotonic is True + assert idx.is_monotonic_increasing is True assert idx._is_strictly_monotonic_increasing is True assert idx.is_monotonic_decreasing is True assert idx._is_strictly_monotonic_decreasing is True @@ -331,7 +331,7 @@ def test_is_monotonic_with_nans(self): # GH#41831 index = IntervalIndex([np.nan, np.nan]) - assert not index.is_monotonic + assert not index.is_monotonic_increasing assert not index._is_strictly_monotonic_increasing assert not index.is_monotonic_increasing assert not index._is_strictly_monotonic_decreasing diff --git a/pandas/tests/indexes/multi/test_get_level_values.py b/pandas/tests/indexes/multi/test_get_level_values.py index 25b4501a03adb..300df00d6151c 100644 --- a/pandas/tests/indexes/multi/test_get_level_values.py +++ b/pandas/tests/indexes/multi/test_get_level_values.py @@ -111,4 +111,4 @@ def test_get_level_values_when_periods(): idx2 = MultiIndex.from_arrays( [idx._get_level_values(level) for level in range(idx.nlevels)] ) - assert all(x.is_monotonic for x in idx2.levels) + assert all(x.is_monotonic_increasing for x in idx2.levels) diff --git a/pandas/tests/indexes/multi/test_indexing.py b/pandas/tests/indexes/multi/test_indexing.py index 9cb65128e7068..ae926932c8002 100644 --- a/pandas/tests/indexes/multi/test_indexing.py +++ b/pandas/tests/indexes/multi/test_indexing.py @@ -353,9 +353,9 @@ def test_get_indexer_three_or_more_levels(self): ] ) # sanity check - assert mult_idx_1.is_monotonic + assert mult_idx_1.is_monotonic_increasing assert mult_idx_1.is_unique - assert mult_idx_2.is_monotonic + assert mult_idx_2.is_monotonic_increasing assert mult_idx_2.is_unique # show the relationships between the two diff --git a/pandas/tests/indexes/multi/test_integrity.py b/pandas/tests/indexes/multi/test_integrity.py index e1c2134b5b1f8..7bb55c6b8c119 100644 --- a/pandas/tests/indexes/multi/test_integrity.py +++ b/pandas/tests/indexes/multi/test_integrity.py @@ -225,11 +225,11 @@ def test_metadata_immutable(idx): def test_level_setting_resets_attributes(): ind = MultiIndex.from_arrays([["A", "A", "B", "B", "B"], [1, 2, 1, 2, 3]]) - assert ind.is_monotonic + assert ind.is_monotonic_increasing with tm.assert_produces_warning(FutureWarning): ind.set_levels([["A", "B"], [1, 3, 2]], inplace=True) # if this fails, probably didn't reset the cache correctly. - assert not ind.is_monotonic + assert not ind.is_monotonic_increasing def test_rangeindex_fallback_coercion_bug(): diff --git a/pandas/tests/indexes/multi/test_monotonic.py b/pandas/tests/indexes/multi/test_monotonic.py index 74be9f829cb68..2b0b3f7cb36d7 100644 --- a/pandas/tests/indexes/multi/test_monotonic.py +++ b/pandas/tests/indexes/multi/test_monotonic.py @@ -10,39 +10,39 @@ def test_is_monotonic_increasing_lexsorted(lexsorted_two_level_string_multiindex): # string ordering mi = lexsorted_two_level_string_multiindex - assert mi.is_monotonic is False - assert Index(mi.values).is_monotonic is False + assert mi.is_monotonic_increasing is False + assert Index(mi.values).is_monotonic_increasing is False assert mi._is_strictly_monotonic_increasing is False assert Index(mi.values)._is_strictly_monotonic_increasing is False def test_is_monotonic_increasing(): i = MultiIndex.from_product([np.arange(10), np.arange(10)], names=["one", "two"]) - assert i.is_monotonic is True + assert i.is_monotonic_increasing is True assert i._is_strictly_monotonic_increasing is True - assert Index(i.values).is_monotonic is True + assert Index(i.values).is_monotonic_increasing is True assert i._is_strictly_monotonic_increasing is True i = MultiIndex.from_product( [np.arange(10, 0, -1), np.arange(10)], names=["one", "two"] ) - assert i.is_monotonic is False + assert i.is_monotonic_increasing is False assert i._is_strictly_monotonic_increasing is False - assert Index(i.values).is_monotonic is False + assert Index(i.values).is_monotonic_increasing is False assert Index(i.values)._is_strictly_monotonic_increasing is False i = MultiIndex.from_product( [np.arange(10), np.arange(10, 0, -1)], names=["one", "two"] ) - assert i.is_monotonic is False + assert i.is_monotonic_increasing is False assert i._is_strictly_monotonic_increasing is False - assert Index(i.values).is_monotonic is False + assert Index(i.values).is_monotonic_increasing is False assert Index(i.values)._is_strictly_monotonic_increasing is False i = MultiIndex.from_product([[1.0, np.nan, 2.0], ["a", "b", "c"]]) - assert i.is_monotonic is False + assert i.is_monotonic_increasing is False assert i._is_strictly_monotonic_increasing is False - assert Index(i.values).is_monotonic is False + assert Index(i.values).is_monotonic_increasing is False assert Index(i.values)._is_strictly_monotonic_increasing is False i = MultiIndex( @@ -50,8 +50,8 @@ def test_is_monotonic_increasing(): codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=["first", "second"], ) - assert i.is_monotonic is True - assert Index(i.values).is_monotonic is True + assert i.is_monotonic_increasing is True + assert Index(i.values).is_monotonic_increasing is True assert i._is_strictly_monotonic_increasing is True assert Index(i.values)._is_strictly_monotonic_increasing is True @@ -71,13 +71,13 @@ def test_is_monotonic_increasing(): names=["household_id", "asset_id"], ) - assert i.is_monotonic is False + assert i.is_monotonic_increasing is False assert i._is_strictly_monotonic_increasing is False # empty i = MultiIndex.from_arrays([[], []]) - assert i.is_monotonic is True - assert Index(i.values).is_monotonic is True + assert i.is_monotonic_increasing is True + assert Index(i.values).is_monotonic_increasing is True assert i._is_strictly_monotonic_increasing is True assert Index(i.values)._is_strictly_monotonic_increasing is True diff --git a/pandas/tests/indexes/multi/test_sorting.py b/pandas/tests/indexes/multi/test_sorting.py index 63d3fe53f9db5..6fd1781beeda4 100644 --- a/pandas/tests/indexes/multi/test_sorting.py +++ b/pandas/tests/indexes/multi/test_sorting.py @@ -157,10 +157,9 @@ def test_reconstruct_sort(): # starts off lexsorted & monotonic mi = MultiIndex.from_arrays([["A", "A", "B", "B", "B"], [1, 2, 1, 2, 3]]) - assert mi.is_monotonic - + assert mi.is_monotonic_increasing recons = mi._sort_levels_monotonic() - assert recons.is_monotonic + assert recons.is_monotonic_increasing assert mi is recons assert mi.equals(recons) @@ -171,11 +170,9 @@ def test_reconstruct_sort(): [("z", "a"), ("x", "a"), ("y", "b"), ("x", "b"), ("y", "a"), ("z", "b")], names=["one", "two"], ) - assert not mi.is_monotonic - + assert not mi.is_monotonic_increasing recons = mi._sort_levels_monotonic() - assert not recons.is_monotonic - + assert not recons.is_monotonic_increasing assert mi.equals(recons) assert Index(mi.values).equals(Index(recons.values)) @@ -185,11 +182,9 @@ def test_reconstruct_sort(): codes=[[0, 1, 0, 2], [2, 0, 0, 1]], names=["col1", "col2"], ) - assert not mi.is_monotonic - + assert not mi.is_monotonic_increasing recons = mi._sort_levels_monotonic() - assert not recons.is_monotonic - + assert not recons.is_monotonic_increasing assert mi.equals(recons) assert Index(mi.values).equals(Index(recons.values)) diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index ebf5f9a1dc696..e0259b5445eee 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -326,19 +326,19 @@ def test_is_monotonic(self): index_cls = self._index_cls index = index_cls([1, 2, 3, 4]) - assert index.is_monotonic is True + assert index.is_monotonic_increasing is True assert index.is_monotonic_increasing is True assert index._is_strictly_monotonic_increasing is True assert index.is_monotonic_decreasing is False assert index._is_strictly_monotonic_decreasing is False index = index_cls([4, 3, 2, 1]) - assert index.is_monotonic is False + assert index.is_monotonic_increasing is False assert index._is_strictly_monotonic_increasing is False assert index._is_strictly_monotonic_decreasing is True index = index_cls([1]) - assert index.is_monotonic is True + assert index.is_monotonic_increasing is True assert index.is_monotonic_increasing is True assert index.is_monotonic_decreasing is True assert index._is_strictly_monotonic_increasing is True diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index c45a4c771856c..1c65c369a7e99 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -311,34 +311,34 @@ def test_cache(self): def test_is_monotonic(self): index = RangeIndex(0, 20, 2) - assert index.is_monotonic is True + assert index.is_monotonic_increasing is True assert index.is_monotonic_increasing is True assert index.is_monotonic_decreasing is False assert index._is_strictly_monotonic_increasing is True assert index._is_strictly_monotonic_decreasing is False index = RangeIndex(4, 0, -1) - assert index.is_monotonic is False + assert index.is_monotonic_increasing is False assert index._is_strictly_monotonic_increasing is False assert index.is_monotonic_decreasing is True assert index._is_strictly_monotonic_decreasing is True index = RangeIndex(1, 2) - assert index.is_monotonic is True + assert index.is_monotonic_increasing is True assert index.is_monotonic_increasing is True assert index.is_monotonic_decreasing is True assert index._is_strictly_monotonic_increasing is True assert index._is_strictly_monotonic_decreasing is True index = RangeIndex(2, 1) - assert index.is_monotonic is True + assert index.is_monotonic_increasing is True assert index.is_monotonic_increasing is True assert index.is_monotonic_decreasing is True assert index._is_strictly_monotonic_increasing is True assert index._is_strictly_monotonic_decreasing is True index = RangeIndex(1, 1) - assert index.is_monotonic is True + assert index.is_monotonic_increasing is True assert index.is_monotonic_increasing is True assert index.is_monotonic_decreasing is True assert index._is_strictly_monotonic_increasing is True diff --git a/pandas/tests/indexing/multiindex/test_sorted.py b/pandas/tests/indexing/multiindex/test_sorted.py index 6ba083d65ac3f..b6cdd0e19a94e 100644 --- a/pandas/tests/indexing/multiindex/test_sorted.py +++ b/pandas/tests/indexing/multiindex/test_sorted.py @@ -54,14 +54,14 @@ def test_frame_getitem_not_sorted2(self, key): with tm.assert_produces_warning(FutureWarning): return_value = df2.index.set_codes([0, 1, 0, 2], level="col1", inplace=True) assert return_value is None - assert not df2.index.is_monotonic + assert not df2.index.is_monotonic_increasing assert df2_original.index.equals(df2.index) expected = df2.sort_index(key=key) - assert expected.index.is_monotonic + assert expected.index.is_monotonic_increasing result = df2.sort_index(level=0, key=key) - assert result.index.is_monotonic + assert result.index.is_monotonic_increasing tm.assert_frame_equal(result, expected) def test_sort_values_key(self, multiindex_dataframe_random_data): diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 31695b03e7539..5761694f64127 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -243,11 +243,11 @@ def test_minmax_timedelta64(self): # monotonic idx1 = TimedeltaIndex(["1 days", "2 days", "3 days"]) - assert idx1.is_monotonic + assert idx1.is_monotonic_increasing # non-monotonic idx2 = TimedeltaIndex(["1 days", np.nan, "3 days", "NaT"]) - assert not idx2.is_monotonic + assert not idx2.is_monotonic_increasing for idx in [idx1, idx2]: assert idx.min() == Timedelta("1 days") @@ -366,13 +366,13 @@ def test_minmax_tz(self, tz_naive_fixture): tz = tz_naive_fixture # monotonic idx1 = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"], tz=tz) - assert idx1.is_monotonic + assert idx1.is_monotonic_increasing # non-monotonic idx2 = DatetimeIndex( ["2011-01-01", NaT, "2011-01-03", "2011-01-02", NaT], tz=tz ) - assert not idx2.is_monotonic + assert not idx2.is_monotonic_increasing for idx in [idx1, idx2]: assert idx.min() == Timestamp("2011-01-01", tz=tz) @@ -470,14 +470,14 @@ def test_minmax_period(self): # monotonic idx1 = PeriodIndex([NaT, "2011-01-01", "2011-01-02", "2011-01-03"], freq="D") - assert not idx1.is_monotonic - assert idx1[1:].is_monotonic + assert not idx1.is_monotonic_increasing + assert idx1[1:].is_monotonic_increasing # non-monotonic idx2 = PeriodIndex( ["2011-01-01", NaT, "2011-01-03", "2011-01-02", NaT], freq="D" ) - assert not idx2.is_monotonic + assert not idx2.is_monotonic_increasing for idx in [idx1, idx2]: assert idx.min() == Period("2011-01-01", freq="D") diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py index a091d072c0911..7ca3ac325d788 100644 --- a/pandas/tests/reshape/merge/test_join.py +++ b/pandas/tests/reshape/merge/test_join.py @@ -401,7 +401,7 @@ def test_join_inner_multiindex(self, lexsorted_two_level_string_multiindex): expected = expected.drop(["first", "second"], axis=1) expected.index = joined.index - assert joined.index.is_monotonic + assert joined.index.is_monotonic_increasing tm.assert_frame_equal(joined, expected) # _assert_same_contents(expected, expected2.loc[:, expected.columns]) diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py index f9310db3123f6..2b02b7ad39d6f 100644 --- a/pandas/tests/reshape/merge/test_merge_asof.py +++ b/pandas/tests/reshape/merge/test_merge_asof.py @@ -586,20 +586,20 @@ def test_non_sorted(self): quotes = self.quotes.sort_values("time", ascending=False) # we require that we are already sorted on time & quotes - assert not trades.time.is_monotonic - assert not quotes.time.is_monotonic + assert not trades.time.is_monotonic_increasing + assert not quotes.time.is_monotonic_increasing with pytest.raises(ValueError, match="left keys must be sorted"): merge_asof(trades, quotes, on="time", by="ticker") trades = self.trades.sort_values("time") - assert trades.time.is_monotonic - assert not quotes.time.is_monotonic + assert trades.time.is_monotonic_increasing + assert not quotes.time.is_monotonic_increasing with pytest.raises(ValueError, match="right keys must be sorted"): merge_asof(trades, quotes, on="time", by="ticker") quotes = self.quotes.sort_values("time") - assert trades.time.is_monotonic - assert quotes.time.is_monotonic + assert trades.time.is_monotonic_increasing + assert quotes.time.is_monotonic_increasing # ok, though has dupes merge_asof(trades, self.quotes, on="time", by="ticker") diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index a38cf8b067b27..f4719ba0bda9a 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -1123,7 +1123,7 @@ def test_pivot_columns_lexsorted(self): aggfunc="mean", ) - assert pivoted.columns.is_monotonic + assert pivoted.columns.is_monotonic_increasing def test_pivot_complex_aggfunc(self): f = {"D": ["std"], "E": ["sum"]} diff --git a/pandas/tests/series/methods/test_asof.py b/pandas/tests/series/methods/test_asof.py index 7ede868861ac0..5557322eae42d 100644 --- a/pandas/tests/series/methods/test_asof.py +++ b/pandas/tests/series/methods/test_asof.py @@ -172,7 +172,7 @@ def test_errors(self): ) # non-monotonic - assert not s.index.is_monotonic + assert not s.index.is_monotonic_increasing with pytest.raises(ValueError, match="requires a sorted index"): s.asof(s.index[0]) diff --git a/pandas/tests/series/methods/test_is_monotonic.py b/pandas/tests/series/methods/test_is_monotonic.py index f02939374cc5b..8d7769539ad82 100644 --- a/pandas/tests/series/methods/test_is_monotonic.py +++ b/pandas/tests/series/methods/test_is_monotonic.py @@ -10,9 +10,9 @@ class TestIsMonotonic: def test_is_monotonic_numeric(self): ser = Series(np.random.randint(0, 10, size=1000)) - assert not ser.is_monotonic + assert not ser.is_monotonic_increasing ser = Series(np.arange(1000)) - assert ser.is_monotonic is True + assert ser.is_monotonic_increasing is True assert ser.is_monotonic_increasing is True ser = Series(np.arange(1000, 0, -1)) assert ser.is_monotonic_decreasing is True @@ -20,9 +20,9 @@ def test_is_monotonic_numeric(self): def test_is_monotonic_dt64(self): ser = Series(date_range("20130101", periods=10)) - assert ser.is_monotonic is True + assert ser.is_monotonic_increasing is True assert ser.is_monotonic_increasing is True ser = Series(list(reversed(ser))) - assert ser.is_monotonic is False + assert ser.is_monotonic_increasing is False assert ser.is_monotonic_decreasing is True diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 7d8817e8dfa34..80e34efb80789 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -172,7 +172,9 @@ def test_attrs(self): def test_inspect_getmembers(self): # GH38782 ser = Series(dtype=object) - with tm.assert_produces_warning(None): + # TODO(2.0): Change to None once is_monotonic deprecation + # is enforced + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): inspect.getmembers(ser) def test_unknown_attribute(self): diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 1bff5b4cc82a6..a06efea47dd6d 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -406,10 +406,10 @@ def test_sort_non_lexsorted(self): ) df = DataFrame({"col": range(len(idx))}, index=idx, dtype="int64") - assert df.index.is_monotonic is False + assert df.index.is_monotonic_increasing is False sorted = df.sort_index() - assert sorted.index.is_monotonic is True + assert sorted.index.is_monotonic_increasing is True expected = DataFrame( {"col": [1, 4, 5, 2]}, diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index f2cf7bd47e15b..c94c418c3d9a7 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -115,11 +115,11 @@ def test_monotonic_on(self): {"A": date_range("20130101", periods=5, freq="s"), "B": range(5)} ) - assert df.A.is_monotonic + assert df.A.is_monotonic_increasing df.rolling("2s", on="A").sum() df = df.set_index("A") - assert df.index.is_monotonic + assert df.index.is_monotonic_increasing df.rolling("2s").sum() def test_non_monotonic_on(self): @@ -132,7 +132,7 @@ def test_non_monotonic_on(self): non_monotonic_index[0] = non_monotonic_index[3] df.index = non_monotonic_index - assert not df.index.is_monotonic + assert not df.index.is_monotonic_increasing msg = "index must be monotonic" with pytest.raises(ValueError, match=msg):