diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5231fa82c1a73..6c80ab9d87e33 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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 diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py index 1c2494e7d6b09..d10494210d04e 100644 --- a/pandas/tests/test_generic.py +++ b/pandas/tests/test_generic.py @@ -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 @@ -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()