Truncate a Series or DataFrame before and after some index value.
This is a useful shorthand for boolean indexing based on index
values above or below certain thresholds.
Parameters:
before : date, str, int
Truncate all rows before this index value.
after : date, str, int
Truncate all rows after this index value.
axis : {0 or ‘index’, 1 or ‘columns’}, optional
Axis to truncate. Truncates the index (rows) by default.
For Series this parameter is unused and defaults to 0.
copy : bool, default is True,
Return a copy of the truncated section.
Returns:
type of caller
The truncated Series or DataFrame.
Notes
If the index being truncated contains only datetime values,
before and after may be specified as strings instead of
Timestamps.
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 A B C1 a f k2 b g l3 c h m4 d i n5 e j o
>>> df.truncate(before=2,after=4) A B C2 b g l3 c h m4 d i n
The columns of a DataFrame can be truncated.
>>> df.truncate(before="A",after="B",axis="columns") A B1 a f2 b g3 c h4 d i5 e j
For Series, only rows can be truncated.
>>> df['A'].truncate(before=2,after=4)2 b3 c4 dName: A, dtype: object
The index values in truncate can be datetimes or string
dates.
Because the index is a DatetimeIndex containing only dates, we can
specify before and after as strings. They will be coerced to
Timestamps before truncation.
Note that truncate assumes a 0 value for any unspecified time
component (midnight). This differs from partial string slicing, which
returns any partially matching dates.