From f5930afd413c00952057f3ce564d81efd16f4600 Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Fri, 20 Oct 2017 00:21:45 +0100 Subject: [PATCH 1/2] DOC: Improve truncate docstring (#17763) --- doc/source/timeseries.rst | 7 +++-- pandas/core/generic.py | 57 +++++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 36ffe8806f373..732f2f38e3c5d 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -657,11 +657,14 @@ With no defaults. Truncating & Fancy Indexing ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -A ``truncate`` convenience function is provided that is equivalent to slicing: +A ``truncate`` convenience function is provided that is similar to slicing. +Note that ``truncate`` assumes a 0 value for any unspecified date component +in a ``DatetimeIndex`` in contrast to slicing which returns any partially +matching dates: .. ipython:: python - ts.truncate(before='10/31/2011', after='12/31/2011') + ts.truncate(before='2011', after='2012') Even complicated fancy indexing that breaks the ``DatetimeIndex`` frequency regularity will result in a ``DatetimeIndex``, although frequency is lost: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 35a26702ad15a..731b6560c1d5d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6330,17 +6330,64 @@ def truncate(self, before=None, after=None, axis=None, copy=True): Parameters ---------- - before : date - Truncate before index value - after : date - Truncate after index value - axis : the truncation axis, defaults to the stat axis + before : date, string, int + Truncate all rows before this index value + after : date, string, int + Truncate all rows after this index value + axis : {0 or 'index', 1 or 'columns'} + * 0 or 'index': apply truncation to rows + * 1 or 'columns': apply truncation to columns + Default is stat axis for given data type (0 for Series and + DataFrames, 1 for Panels) copy : boolean, default is True, return a copy of the truncated section Returns ------- truncated : type of caller + + Examples + -------- + >>> df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'], + ... 'B': ['f', 'g', 'h', 'i', 'j'], + ... 'C': ['k', 'l', 'm', 'n', 'o']}, + ... index=[1, 2, 3, 4, 5]) + >>> df.truncate(before=2, after=4) + A B C + 2 b g l + 3 c h m + 4 d i n + >>> df = pd.DataFrame({'A': [1, 2, 3, 4, 5], + ... 'B': [6, 7, 8, 9, 10], + ... 'C': [11, 12, 13, 14, 15]}, + ... index=['a', 'b', 'c', 'd', 'e']) + >>> df.truncate(before='b', after='d') + A B C + b 2 7 12 + c 3 8 13 + d 4 9 14 + + The index values in ``truncate`` can be datetimes or string + dates. Note that ``truncate`` assumes a 0 value for any unspecified + date component in a ``DatetimeIndex`` in contrast to slicing which + returns any partially matching dates. + + >>> dates = pd.date_range('2016-1-1', '2016-2-1', freq='s') + >>> df = pd.DataFrame(index=dates, data={'A': 1}) + >>> df.truncate('2016-1-5', '2016-1-10').tail() + A + 2016-01-09 23:59:56 1 + 2016-01-09 23:59:57 1 + 2016-01-09 23:59:58 1 + 2016-01-09 23:59:59 1 + 2016-01-10 00:00:00 1 + >>> df.loc['2016-1-5':'2016-1-10', :].tail() + A + 2016-01-10 23:59:55 1 + 2016-01-10 23:59:56 1 + 2016-01-10 23:59:57 1 + 2016-01-10 23:59:58 1 + 2016-01-10 23:59:59 1 """ if axis is None: From c29db4500403500eb1b706b66e05f7890e9b797b Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Sat, 21 Oct 2017 13:44:12 +0100 Subject: [PATCH 2/2] changes from review --- doc/source/timeseries.rst | 8 ++++++-- pandas/core/generic.py | 14 ++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 732f2f38e3c5d..fa8d036b655e6 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -664,14 +664,18 @@ matching dates: .. ipython:: python - ts.truncate(before='2011', after='2012') + rng2 = pd.date_range('2011-01-01', '2012-01-01', freq='W') + ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2) + + ts2.truncate(before='2011-11', after='2011-12') + ts2['2011-11':'2011-12'] Even complicated fancy indexing that breaks the ``DatetimeIndex`` frequency regularity will result in a ``DatetimeIndex``, although frequency is lost: .. ipython:: python - ts[[0, 2, 6]].index + ts2[[0, 2, 6]].index .. _timeseries.components: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 731b6560c1d5d..52bebaed4a368 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6324,9 +6324,10 @@ def tshift(self, periods=1, freq=None, axis=0): return self._constructor(new_data).__finalize__(self) def truncate(self, before=None, after=None, axis=None, copy=True): - """Truncates a sorted NDFrame before and/or after some particular - index value. If the axis contains only datetime values, before/after - parameters are converted to datetime values. + """ + Truncates a sorted DataFrame/Series before and/or after some + particular index value. If the axis contains only datetime values, + before/after parameters are converted to datetime values. Parameters ---------- @@ -6335,6 +6336,7 @@ def truncate(self, before=None, after=None, axis=None, copy=True): after : date, string, int Truncate all rows after this index value axis : {0 or 'index', 1 or 'columns'} + * 0 or 'index': apply truncation to rows * 1 or 'columns': apply truncation to columns Default is stat axis for given data type (0 for Series and @@ -6372,16 +6374,16 @@ def truncate(self, before=None, after=None, axis=None, copy=True): date component in a ``DatetimeIndex`` in contrast to slicing which returns any partially matching dates. - >>> dates = pd.date_range('2016-1-1', '2016-2-1', freq='s') + >>> dates = pd.date_range('2016-01-01', '2016-02-01', freq='s') >>> df = pd.DataFrame(index=dates, data={'A': 1}) - >>> df.truncate('2016-1-5', '2016-1-10').tail() + >>> df.truncate('2016-01-05', '2016-01-10').tail() A 2016-01-09 23:59:56 1 2016-01-09 23:59:57 1 2016-01-09 23:59:58 1 2016-01-09 23:59:59 1 2016-01-10 00:00:00 1 - >>> df.loc['2016-1-5':'2016-1-10', :].tail() + >>> df.loc['2016-01-05':'2016-01-10', :].tail() A 2016-01-10 23:59:55 1 2016-01-10 23:59:56 1