Skip to content

Commit 0d78812

Browse files
reidy-palanbato
authored andcommitted
DOC: Improve truncate docstring (pandas-dev#17763) (pandas-dev#17925)
1 parent a6ac1ea commit 0d78812

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

doc/source/timeseries.rst

+10-3
Original file line numberDiff line numberDiff line change
@@ -657,18 +657,25 @@ With no defaults.
657657
Truncating & Fancy Indexing
658658
~~~~~~~~~~~~~~~~~~~~~~~~~~~
659659

660-
A ``truncate`` convenience function is provided that is equivalent to slicing:
660+
A ``truncate`` convenience function is provided that is similar to slicing.
661+
Note that ``truncate`` assumes a 0 value for any unspecified date component
662+
in a ``DatetimeIndex`` in contrast to slicing which returns any partially
663+
matching dates:
661664

662665
.. ipython:: python
663666
664-
ts.truncate(before='10/31/2011', after='12/31/2011')
667+
rng2 = pd.date_range('2011-01-01', '2012-01-01', freq='W')
668+
ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2)
669+
670+
ts2.truncate(before='2011-11', after='2011-12')
671+
ts2['2011-11':'2011-12']
665672
666673
Even complicated fancy indexing that breaks the ``DatetimeIndex`` frequency
667674
regularity will result in a ``DatetimeIndex``, although frequency is lost:
668675

669676
.. ipython:: python
670677
671-
ts[[0, 2, 6]].index
678+
ts2[[0, 2, 6]].index
672679
673680
.. _timeseries.components:
674681

pandas/core/generic.py

+57-8
Original file line numberDiff line numberDiff line change
@@ -6324,23 +6324,72 @@ def tshift(self, periods=1, freq=None, axis=0):
63246324
return self._constructor(new_data).__finalize__(self)
63256325

63266326
def truncate(self, before=None, after=None, axis=None, copy=True):
6327-
"""Truncates a sorted NDFrame before and/or after some particular
6328-
index value. If the axis contains only datetime values, before/after
6329-
parameters are converted to datetime values.
6327+
"""
6328+
Truncates a sorted DataFrame/Series before and/or after some
6329+
particular index value. If the axis contains only datetime values,
6330+
before/after parameters are converted to datetime values.
63306331
63316332
Parameters
63326333
----------
6333-
before : date
6334-
Truncate before index value
6335-
after : date
6336-
Truncate after index value
6337-
axis : the truncation axis, defaults to the stat axis
6334+
before : date, string, int
6335+
Truncate all rows before this index value
6336+
after : date, string, int
6337+
Truncate all rows after this index value
6338+
axis : {0 or 'index', 1 or 'columns'}
6339+
6340+
* 0 or 'index': apply truncation to rows
6341+
* 1 or 'columns': apply truncation to columns
6342+
Default is stat axis for given data type (0 for Series and
6343+
DataFrames, 1 for Panels)
63386344
copy : boolean, default is True,
63396345
return a copy of the truncated section
63406346
63416347
Returns
63426348
-------
63436349
truncated : type of caller
6350+
6351+
Examples
6352+
--------
6353+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'],
6354+
... 'B': ['f', 'g', 'h', 'i', 'j'],
6355+
... 'C': ['k', 'l', 'm', 'n', 'o']},
6356+
... index=[1, 2, 3, 4, 5])
6357+
>>> df.truncate(before=2, after=4)
6358+
A B C
6359+
2 b g l
6360+
3 c h m
6361+
4 d i n
6362+
>>> df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
6363+
... 'B': [6, 7, 8, 9, 10],
6364+
... 'C': [11, 12, 13, 14, 15]},
6365+
... index=['a', 'b', 'c', 'd', 'e'])
6366+
>>> df.truncate(before='b', after='d')
6367+
A B C
6368+
b 2 7 12
6369+
c 3 8 13
6370+
d 4 9 14
6371+
6372+
The index values in ``truncate`` can be datetimes or string
6373+
dates. Note that ``truncate`` assumes a 0 value for any unspecified
6374+
date component in a ``DatetimeIndex`` in contrast to slicing which
6375+
returns any partially matching dates.
6376+
6377+
>>> dates = pd.date_range('2016-01-01', '2016-02-01', freq='s')
6378+
>>> df = pd.DataFrame(index=dates, data={'A': 1})
6379+
>>> df.truncate('2016-01-05', '2016-01-10').tail()
6380+
A
6381+
2016-01-09 23:59:56 1
6382+
2016-01-09 23:59:57 1
6383+
2016-01-09 23:59:58 1
6384+
2016-01-09 23:59:59 1
6385+
2016-01-10 00:00:00 1
6386+
>>> df.loc['2016-01-05':'2016-01-10', :].tail()
6387+
A
6388+
2016-01-10 23:59:55 1
6389+
2016-01-10 23:59:56 1
6390+
2016-01-10 23:59:57 1
6391+
2016-01-10 23:59:58 1
6392+
2016-01-10 23:59:59 1
63446393
"""
63456394

63466395
if axis is None:

0 commit comments

Comments
 (0)