Skip to content

TST: assert monotonic is True #32601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,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 is True

# ordering ambiguous, raise exception
with pytest.raises(ValueError, match="ambiguous ordering"):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,17 @@ def test_sort_values(self):
idx = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])

ordered = idx.sort_values()
assert ordered.is_monotonic
assert ordered.is_monotonic is True

ordered = idx.sort_values(ascending=False)
assert ordered[::-1].is_monotonic
assert ordered[::-1].is_monotonic is True

ordered, dexer = idx.sort_values(return_indexer=True)
assert ordered.is_monotonic
assert ordered.is_monotonic is True
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 is True
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp))

def test_map_bug_1677(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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 is True

@pytest.mark.parametrize("freq", ["B", "C"])
def test_outer_join(self, freq):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/multi/test_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ def test_metadata_immutable(idx):

def test_level_setting_resets_attributes():
ind = pd.MultiIndex.from_arrays([["A", "A", "B", "B", "B"], [1, 2, 1, 2, 3]])
assert ind.is_monotonic
assert ind.is_monotonic is True
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 is True


def test_rangeindex_fallback_coercion_bug():
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/multi/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ 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_lexsorted()
assert mi.is_monotonic
assert mi.is_monotonic is True

recons = mi._sort_levels_monotonic()
assert recons.is_lexsorted()
assert recons.is_monotonic
assert recons.is_monotonic is True
assert mi is recons

assert mi.equals(recons)
Expand All @@ -160,11 +160,11 @@ def test_reconstruct_sort():
names=["one", "two"],
)
assert not mi.is_lexsorted()
assert not mi.is_monotonic
assert not mi.is_monotonic is True

recons = mi._sort_levels_monotonic()
assert not recons.is_lexsorted()
assert not recons.is_monotonic
assert not recons.is_monotonic is True

assert mi.equals(recons)
assert Index(mi.values).equals(Index(recons.values))
Expand All @@ -176,11 +176,11 @@ def test_reconstruct_sort():
names=["col1", "col2"],
)
assert not mi.is_lexsorted()
assert not mi.is_monotonic
assert not mi.is_monotonic is True

recons = mi._sort_levels_monotonic()
assert not recons.is_lexsorted()
assert not recons.is_monotonic
assert recons.is_monotonic is False

assert mi.equals(recons)
assert Index(mi.values).equals(Index(recons.values))
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,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 is True
assert obj.is_monotonic_increasing
assert not obj.is_monotonic_decreasing
assert obj.is_unique
Expand All @@ -665,7 +665,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 obj.is_monotonic is False
assert not obj.is_monotonic_increasing
assert not obj.is_monotonic_decreasing
assert obj.is_unique
Expand All @@ -677,7 +677,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 obj.is_monotonic is False
assert not obj.is_monotonic_increasing
assert not obj.is_monotonic_decreasing
assert obj.is_unique
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ def test_sort_values(self):
idx = TimedeltaIndex(["4d", "1d", "2d"])

ordered = idx.sort_values()
assert ordered.is_monotonic
assert ordered.is_monotonic is True

ordered = idx.sort_values(ascending=False)
assert ordered[::-1].is_monotonic
assert ordered[::-1].is_monotonic is True

ordered, dexer = idx.sort_values(return_indexer=True)
assert ordered.is_monotonic
assert ordered.is_monotonic is True

tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0]), check_dtype=False)

ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
assert ordered[::-1].is_monotonic
assert ordered[::-1].is_monotonic is True

tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1]), check_dtype=False)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexing/multiindex/test_sorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ def test_frame_getitem_not_sorted2(self):
df2.index.set_levels(["b", "d", "a"], level="col1", inplace=True)
df2.index.set_codes([0, 1, 0, 2], level="col1", inplace=True)
assert not df2.index.is_lexsorted()
assert not df2.index.is_monotonic
assert not df2.index.is_monotonic is True

assert df2_original.index.equals(df2.index)
expected = df2.sort_index()
assert expected.index.is_lexsorted()
assert expected.index.is_monotonic
assert expected.index.is_monotonic is True

result = df2.sort_index(level=0)
assert result.index.is_lexsorted()
assert result.index.is_monotonic
assert result.index.is_monotonic is True
tm.assert_frame_equal(result, expected)

def test_frame_getitem_not_sorted(self, multiindex_dataframe_random_data):
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ def test_minmax_timedelta64(self):

# monotonic
idx1 = TimedeltaIndex(["1 days", "2 days", "3 days"])
assert idx1.is_monotonic
assert idx1.is_monotonic is True

# non-monotonic
idx2 = TimedeltaIndex(["1 days", np.nan, "3 days", "NaT"])
assert not idx2.is_monotonic
assert not idx2.is_monotonic is True

for idx in [idx1, idx2]:
assert idx.min() == Timedelta("1 days")
Expand Down Expand Up @@ -339,13 +339,13 @@ def test_minmax_tz(self, tz_naive_fixture):
tz = tz_naive_fixture
# monotonic
idx1 = pd.DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"], tz=tz)
assert idx1.is_monotonic
assert idx1.is_monotonic is True

# non-monotonic
idx2 = pd.DatetimeIndex(
["2011-01-01", pd.NaT, "2011-01-03", "2011-01-02", pd.NaT], tz=tz
)
assert not idx2.is_monotonic
assert idx2.is_monotonic is False

for idx in [idx1, idx2]:
assert idx.min() == Timestamp("2011-01-01", tz=tz)
Expand Down Expand Up @@ -445,14 +445,14 @@ def test_minmax_period(self):

# monotonic
idx1 = pd.PeriodIndex([NaT, "2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
assert not idx1.is_monotonic
assert idx1[1:].is_monotonic
assert idx1.is_monotonic is False
assert idx1[1:].is_monotonic is True

# non-monotonic
idx2 = pd.PeriodIndex(
["2011-01-01", NaT, "2011-01-03", "2011-01-02", NaT], freq="D"
)
assert not idx2.is_monotonic
assert idx2.is_monotonic is False

for idx in [idx1, idx2]:
assert idx.min() == pd.Period("2011-01-01", freq="D")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def test_join_inner_multiindex(self):
expected = expected.drop(["first", "second"], axis=1)
expected.index = joined.index

assert joined.index.is_monotonic
assert joined.index.is_monotonic is True
tm.assert_frame_equal(joined, expected)

# _assert_same_contents(expected, expected2.loc[:, expected.columns])
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,20 +570,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 trades.time.is_monotonic is False
assert quotes.time.is_monotonic is False
with pytest.raises(ValueError):
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 is True
assert quotes.time.is_monotonic is False
with pytest.raises(ValueError):
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 is True
assert quotes.time.is_monotonic is True

# ok, though has dupes
merge_asof(trades, self.quotes, on="time", by="ticker")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ def test_pivot_columns_lexsorted(self):
aggfunc="mean",
)

assert pivoted.columns.is_monotonic
assert pivoted.columns.is_monotonic is True

def test_pivot_complex_aggfunc(self):
f = {"D": ["std"], "E": ["sum"]}
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_errors(self):
)

# non-monotonic
assert not s.index.is_monotonic
assert s.index.is_monotonic is False
with pytest.raises(ValueError):
s.asof(s.index[0])

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_numpy_repeat(self):
def test_is_monotonic(self):

s = Series(np.random.randint(0, 10, size=1000))
assert not s.is_monotonic
assert s.is_monotonic is False
s = Series(np.arange(1000))
assert s.is_monotonic is True
assert s.is_monotonic_increasing is True
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,7 @@ def test_sort_index_and_reconstruction(self):
)
result = result.sort_index()
assert result.index.is_lexsorted()
assert result.index.is_monotonic
assert result.index.is_monotonic is True

tm.assert_frame_equal(result, expected)

Expand All @@ -2056,7 +2056,7 @@ def test_sort_index_and_reconstruction(self):
result = concatted.sort_index()

assert result.index.is_lexsorted()
assert result.index.is_monotonic
assert result.index.is_monotonic is True

tm.assert_frame_equal(result, expected)

Expand All @@ -2073,13 +2073,13 @@ def test_sort_index_and_reconstruction(self):
pd.to_datetime(df.columns.levels[1]), level=1, inplace=True
)
assert not df.columns.is_lexsorted()
assert not df.columns.is_monotonic
assert df.columns.is_monotonic is False
result = df.sort_index(axis=1)
assert result.columns.is_lexsorted()
assert result.columns.is_monotonic
assert result.columns.is_monotonic is True
result = df.sort_index(axis=1, level=1)
assert result.columns.is_lexsorted()
assert result.columns.is_monotonic
assert result.columns.is_monotonic is True

def test_sort_index_and_reconstruction_doc_example(self):
# doc example
Expand All @@ -2090,7 +2090,7 @@ def test_sort_index_and_reconstruction_doc_example(self):
),
)
assert df.index.is_lexsorted()
assert not df.index.is_monotonic
assert df.index.is_monotonic is False

# sort it
expected = DataFrame(
Expand All @@ -2101,15 +2101,15 @@ def test_sort_index_and_reconstruction_doc_example(self):
)
result = df.sort_index()
assert result.index.is_lexsorted()
assert result.index.is_monotonic
assert result.index.is_monotonic is True

tm.assert_frame_equal(result, expected)

# reconstruct
result = df.sort_index().copy()
result.index = result.index._sort_levels_monotonic()
assert result.index.is_lexsorted()
assert result.index.is_monotonic
assert result.index.is_monotonic is True

tm.assert_frame_equal(result, expected)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/window/test_timeseries_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,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 is True
df.rolling("2s", on="A").sum()

df = df.set_index("A")
assert df.index.is_monotonic
assert df.index.is_monotonic is True
df.rolling("2s").sum()

def test_non_monotonic_on(self):
Expand All @@ -123,7 +123,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 df.index.is_monotonic is False

with pytest.raises(ValueError):
df.rolling("2s").sum()
Expand Down