Skip to content

Commit 83942e5

Browse files
committed
DOC: Improve truncate docstring (pandas-dev#17763)
1 parent a2e5400 commit 83942e5

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

doc/source/timeseries.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,14 @@ 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+
ts.truncate(before='2011', after='2012')
665668
666669
Even complicated fancy indexing that breaks the ``DatetimeIndex`` frequency
667670
regularity will result in a ``DatetimeIndex``, although frequency is lost:

pandas/core/generic.py

+52-5
Original file line numberDiff line numberDiff line change
@@ -6330,17 +6330,64 @@ def truncate(self, before=None, after=None, axis=None, copy=True):
63306330
63316331
Parameters
63326332
----------
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
6333+
before : date, string, int
6334+
Truncate all rows before this index value
6335+
after : date, string, int
6336+
Truncate all rows after this index value
6337+
axis : {0 or 'index', 1 or 'columns'}
6338+
* 0 or 'index': apply truncation to rows
6339+
* 1 or 'columns': apply truncation to columns
6340+
Default is stat axis for given data type (0 for Series and
6341+
DataFrames, 1 for Panels)
63386342
copy : boolean, default is True,
63396343
return a copy of the truncated section
63406344
63416345
Returns
63426346
-------
63436347
truncated : type of caller
6348+
6349+
Examples
6350+
--------
6351+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'],
6352+
... 'B': ['f', 'g', 'h', 'i', 'j'],
6353+
... 'C': ['k', 'l', 'm', 'n', 'o']},
6354+
... index=[1, 2, 3, 4, 5])
6355+
>>> df.truncate(before=2, after=4)
6356+
A B C
6357+
2 b g l
6358+
3 c h m
6359+
4 d i n
6360+
>>> df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
6361+
... 'B': [6, 7, 8, 9, 10],
6362+
... 'C': [11, 12, 13, 14, 15]},
6363+
... index=['a', 'b', 'c', 'd', 'e'])
6364+
>>> df.truncate(before='b', after='d')
6365+
A B C
6366+
b 2 7 12
6367+
c 3 8 13
6368+
d 4 9 14
6369+
6370+
The index values in ``truncate`` can be datetimes or (partial) string
6371+
dates. Note that ``truncate`` assumes a 0 value for any unspecified
6372+
date component in a ``DatetimeIndex`` in contrast to slicing which
6373+
returns any partially matching dates.
6374+
6375+
>>> dates = pd.date_range('2016-1-1', '2016-2-1', freq='s')
6376+
>>> df = pd.DataFrame(index=dates, data={'A': 1})
6377+
>>> df.truncate('2016-1-5', '2016-1-10').tail()
6378+
A
6379+
2016-01-09 23:59:56 1
6380+
2016-01-09 23:59:57 1
6381+
2016-01-09 23:59:58 1
6382+
2016-01-09 23:59:59 1
6383+
2016-01-10 00:00:00 1
6384+
>>> df.loc['2016-1-5':'2016-1-10', :].tail()
6385+
A
6386+
2016-01-10 23:59:55 1
6387+
2016-01-10 23:59:56 1
6388+
2016-01-10 23:59:57 1
6389+
2016-01-10 23:59:58 1
6390+
2016-01-10 23:59:59 1
63446391
"""
63456392

63466393
if axis is None:

0 commit comments

Comments
 (0)