From 27379dd609890be5abef332ee5d889b5b35c97c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Lipt=C3=A1k?= Date: Thu, 28 Apr 2016 20:22:31 -0400 Subject: [PATCH 1/2] Update truncate() documentation and testcases --- pandas/core/generic.py | 7 ++++--- pandas/tests/test_generic.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) 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..7ee229beddc1a 100644 --- a/pandas/tests/test_generic.py +++ b/pandas/tests/test_generic.py @@ -809,6 +809,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() From 5e2fbe55f0af92631590aa2d37f94294c03a9d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Lipt=C3=A1k?= Date: Wed, 4 May 2016 17:26:10 -0400 Subject: [PATCH 2/2] Refactor to use _construct() and _compare() --- pandas/tests/test_generic.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py index 7ee229beddc1a..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