Skip to content

Commit b97a5ad

Browse files
simobassojorisvandenbossche
authored andcommitted
DOC: Improved the docstring of pandas.Series.truncate (#20125)
1 parent 30e0006 commit b97a5ad

File tree

1 file changed

+75
-27
lines changed

1 file changed

+75
-27
lines changed

pandas/core/generic.py

+75-27
Original file line numberDiff line numberDiff line change
@@ -7476,65 +7476,113 @@ def tshift(self, periods=1, freq=None, axis=0):
74767476

74777477
def truncate(self, before=None, after=None, axis=None, copy=True):
74787478
"""
7479-
Truncates a sorted DataFrame/Series before and/or after some
7480-
particular index value. If the axis contains only datetime values,
7481-
before/after parameters are converted to datetime values.
7479+
Truncate a Series or DataFrame before and after some index value.
7480+
7481+
This is a useful shorthand for boolean indexing based on index
7482+
values above or below certain thresholds.
74827483
74837484
Parameters
74847485
----------
74857486
before : date, string, int
7486-
Truncate all rows before this index value
7487+
Truncate all rows before this index value.
74877488
after : date, string, int
7488-
Truncate all rows after this index value
7489-
axis : {0 or 'index', 1 or 'columns'}
7490-
7491-
* 0 or 'index': apply truncation to rows
7492-
* 1 or 'columns': apply truncation to columns
7493-
7494-
Default is stat axis for given data type (0 for Series and
7495-
DataFrames, 1 for Panels)
7489+
Truncate all rows after this index value.
7490+
axis : {0 or 'index', 1 or 'columns'}, optional
7491+
Axis to truncate. Truncates the index (rows) by default.
74967492
copy : boolean, default is True,
7497-
return a copy of the truncated section
7493+
Return a copy of the truncated section.
74987494
74997495
Returns
75007496
-------
7501-
truncated : type of caller
7497+
type of caller
7498+
The truncated Series or DataFrame.
7499+
7500+
See Also
7501+
--------
7502+
DataFrame.loc : Select a subset of a DataFrame by label.
7503+
DataFrame.iloc : Select a subset of a DataFrame by position.
7504+
7505+
Notes
7506+
-----
7507+
If the index being truncated contains only datetime values,
7508+
`before` and `after` may be specified as strings instead of
7509+
Timestamps.
75027510
75037511
Examples
75047512
--------
75057513
>>> df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'],
75067514
... 'B': ['f', 'g', 'h', 'i', 'j'],
75077515
... 'C': ['k', 'l', 'm', 'n', 'o']},
75087516
... index=[1, 2, 3, 4, 5])
7517+
>>> df
7518+
A B C
7519+
1 a f k
7520+
2 b g l
7521+
3 c h m
7522+
4 d i n
7523+
5 e j o
7524+
75097525
>>> df.truncate(before=2, after=4)
75107526
A B C
75117527
2 b g l
75127528
3 c h m
75137529
4 d i n
7514-
>>> df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
7515-
... 'B': [6, 7, 8, 9, 10],
7516-
... 'C': [11, 12, 13, 14, 15]},
7517-
... index=['a', 'b', 'c', 'd', 'e'])
7518-
>>> df.truncate(before='b', after='d')
7519-
A B C
7520-
b 2 7 12
7521-
c 3 8 13
7522-
d 4 9 14
75237530
7524-
The index values in ``truncate`` can be datetimes or string
7525-
dates. Note that ``truncate`` assumes a 0 value for any unspecified
7526-
date component in a ``DatetimeIndex`` in contrast to slicing which
7527-
returns any partially matching dates.
7531+
The columns of a DataFrame can be truncated.
75287532
7533+
>>> df.truncate(before="A", after="B", axis="columns")
7534+
A B
7535+
1 a f
7536+
2 b g
7537+
3 c h
7538+
4 d i
7539+
5 e j
7540+
7541+
For Series, only rows can be truncated.
7542+
7543+
>>> df['A'].truncate(before=2, after=4)
7544+
2 b
7545+
3 c
7546+
4 d
7547+
Name: A, dtype: object
7548+
7549+
The index values in ``truncate`` can be datetimes or string
7550+
dates.
75297551
>>> dates = pd.date_range('2016-01-01', '2016-02-01', freq='s')
75307552
>>> df = pd.DataFrame(index=dates, data={'A': 1})
7553+
>>> df.tail()
7554+
A
7555+
2016-01-31 23:59:56 1
7556+
2016-01-31 23:59:57 1
7557+
2016-01-31 23:59:58 1
7558+
2016-01-31 23:59:59 1
7559+
2016-02-01 00:00:00 1
7560+
7561+
>>> df.truncate(before=pd.Timestamp('2016-01-05'),
7562+
... after=pd.Timestamp('2016-01-10')).tail()
7563+
A
7564+
2016-01-09 23:59:56 1
7565+
2016-01-09 23:59:57 1
7566+
2016-01-09 23:59:58 1
7567+
2016-01-09 23:59:59 1
7568+
2016-01-10 00:00:00 1
7569+
7570+
Because the index is a DatetimeIndex containing only dates, we can
7571+
specify `before` and `after` as strings. They will be coerced to
7572+
Timestamps before truncation.
7573+
75317574
>>> df.truncate('2016-01-05', '2016-01-10').tail()
75327575
A
75337576
2016-01-09 23:59:56 1
75347577
2016-01-09 23:59:57 1
75357578
2016-01-09 23:59:58 1
75367579
2016-01-09 23:59:59 1
75377580
2016-01-10 00:00:00 1
7581+
7582+
Note that ``truncate`` assumes a 0 value for any unspecified time
7583+
component (midnight). This differs from partial string slicing, which
7584+
returns any partially matching dates.
7585+
75387586
>>> df.loc['2016-01-05':'2016-01-10', :].tail()
75397587
A
75407588
2016-01-10 23:59:55 1

0 commit comments

Comments
 (0)