Skip to content

Update truncate() documentation and testcases #13024

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 2 commits 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
7 changes: 4 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4633,14 +4633,15 @@ def tshift(self, periods=1, freq=None, axis=0):

def truncate(self, before=None, after=None, axis=None, copy=True):
"""Truncates a sorted NDFrame before and/or after some particular
dates.
index value. If the axis contains only datetime values, before/after
parameters are converted to datetime values.

Parameters
----------
before : date
Truncate before date
Truncate before index value
after : date
Truncate after date
Truncate after index value
axis : the truncation axis, defaults to the stat axis
copy : boolean, default is True,
return a copy of the truncated section
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,22 @@ def test_clip(self):
lower=lower, upper=upper,
axis=bad_axis)

def test_truncate_outofbounds_small(self):
# GH11382
shape = [int(2e3)] + ([1] * (self._ndim-1))
small = self._construct(shape, dtype='int8')
self._compare(small.truncate(), small)
self._compare(small.truncate(before=0, after=3e3), small)
self._compare(small.truncate(before=-1, after=2e3), small)

def test_truncate_outofbounds_big(self):
# GH11382
shape = [int(2e6)] + ([1] * (self._ndim-1))
big = self._construct(shape, dtype='int8')
self._compare(big.truncate(), big)
self._compare(big.truncate(before=0, after=3e6), big)
self._compare(big.truncate(before=-1, after=2e6), big)

def test_numpy_clip(self):
lower = 1
upper = 3
Expand Down Expand Up @@ -809,6 +825,17 @@ def test_describe_none(self):
expected = Series([0, 0], index=['count', 'unique'], name='None')
assert_series_equal(noneSeries.describe(), expected)

def test_truncate_outofbounds(self):
# GH11382
small = pd.Series(data=range(int(2e3)), index=range(int(2e3)))
assert_series_equal(small.truncate(), small)
assert_series_equal(small.truncate(before=0, after=3e3), small)
assert_series_equal(small.truncate(before=-1, after=2e3), small)
big = pd.Series(data=range(int(2e6)), index=range(int(2e6)))
assert_series_equal(big.truncate(), big)
assert_series_equal(big.truncate(before=0, after=3e6), big)
assert_series_equal(big.truncate(before=-1, after=2e6), big)

def test_to_xarray(self):

tm._skip_if_no_xarray()
Expand Down