-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ERR: Improve error message on non-sorted input with .truncate #17984
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
Changes from 3 commits
6217901
a679553
1d47030
1850cdd
e0d40b9
f1a7616
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6338,6 +6338,12 @@ def truncate(self, before=None, after=None, axis=None, copy=True): | |
axis = self._get_axis_number(axis) | ||
ax = self._get_axis(axis) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment here |
||
# GH 17935 | ||
# Check that index is sorted | ||
if (not ax.is_monotonic_increasing and | ||
not ax.is_monotonic_decreasing): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint error |
||
raise ValueError("truncate requires a sorted index") | ||
|
||
# if we have a date index, convert to dates, otherwise | ||
# treat like a slice | ||
if ax.is_all_dates: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,6 +377,33 @@ def test_truncate_copy(self): | |
truncated.values[:] = 5. | ||
assert not (self.tsframe.values[5:11] == 5).any() | ||
|
||
def test_truncate_nonsortedindex(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a test for series as well (you can also put this test generically in test_generic if its easier) |
||
# GH 17935 | ||
|
||
df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e']}, | ||
index=[5, 3, 2, 9, 0]) | ||
with tm.assert_raises_regex(ValueError, | ||
'truncate requires a sorted index'): | ||
df.truncate(before=3, after=9) | ||
|
||
rng = pd.date_range('2011-01-01', '2012-01-01', freq='W') | ||
ts = pd.DataFrame({'A': np.random.randn(len(rng)), | ||
'B': np.random.randn(len(rng))}, | ||
index=rng) | ||
with tm.assert_raises_regex(ValueError, | ||
'truncate requires a sorted index'): | ||
ts.sort_values(ascending=False).truncate(before='2011-11', | ||
after='2011-12') | ||
|
||
df = pd.DataFrame({3: np.random.randn(5), | ||
20: np.random.randn(5), | ||
2: np.random.randn(5), | ||
0: np.random.randn(5)}, | ||
columns=[3, 20, 2, 0]) | ||
with tm.assert_raises_regex(ValueError, | ||
'truncate requires a sorted index'): | ||
df.truncate(before=2, after=20, axis=1) | ||
|
||
def test_asfreq(self): | ||
offset_monthly = self.tsframe.asfreq(offsets.BMonthEnd()) | ||
rule_monthly = self.tsframe.asfreq('BM') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
say, rather than a non-helpful
KeyError