diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 36ffe8806f373..fa8d036b655e6 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -657,18 +657,25 @@ 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') + 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 35a26702ad15a..52bebaed4a368 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6324,23 +6324,72 @@ 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 ---------- - 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-01-01', '2016-02-01', freq='s') + >>> df = pd.DataFrame(index=dates, data={'A': 1}) + >>> 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-01-05':'2016-01-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: