Skip to content

DOC: Improve truncate docstring (#17763) #17925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the equivalent slicing example as well to show it is not the same ?


Even complicated fancy indexing that breaks the ``DatetimeIndex`` frequency
regularity will result in a ``DatetimeIndex``, although frequency is lost:
Expand Down
57 changes: 52 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line above this one

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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I prefer to use iso dates in our docs (apart for examples specifically on date string parsing), so "2016-01-01" etc

>>> 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:
Expand Down