pandas 1.4.2

NotesParametersReturns
truncate(self: 'NDFrameT', before=None, after=None, axis=None, copy: 'bool_t' = True) -> 'NDFrameT'

This is a useful shorthand for boolean indexing based on index values above or below certain thresholds.

Notes

If the index being truncated contains only datetime values, :None:None:`before` and :None:None:`after` may be specified as strings instead of Timestamps.

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.

copy : bool, default is True,

Return a copy of the truncated section.

Returns

type of caller

The truncated Series or DataFrame.

Truncate a Series or DataFrame before and after some index value.

See Also

DataFrame.iloc

Select a subset of a DataFrame by position.

DataFrame.loc

Select a subset of a DataFrame by label.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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 C 1 a f k 2 b g l 3 c h m 4 d i n 5 e j o
This example is valid syntax, but we were not able to check execution
>>> df.truncate(before=2, after=4)
   A  B  C
2  b  g  l
3  c  h  m
4  d  i  n

The columns of a DataFrame can be truncated.

This example is valid syntax, but we were not able to check execution
>>> df.truncate(before="A", after="B", axis="columns")
   A  B
1  a  f
2  b  g
3  c  h
4  d  i
5  e  j

For Series, only rows can be truncated.

This example is valid syntax, but we were not able to check execution
>>> df['A'].truncate(before=2, after=4)
2    b
3    c
4    d
Name: A, dtype: object

The index values in truncate can be datetimes or string dates.

This example is valid syntax, but we were not able to check execution
>>> dates = pd.date_range('2016-01-01', '2016-02-01', freq='s')
... df = pd.DataFrame(index=dates, data={'A': 1})
... df.tail() A 2016-01-31 23:59:56 1 2016-01-31 23:59:57 1 2016-01-31 23:59:58 1 2016-01-31 23:59:59 1 2016-02-01 00:00:00 1
This example is valid syntax, but we were not able to check execution
>>> df.truncate(before=pd.Timestamp('2016-01-05'),
...  after=pd.Timestamp('2016-01-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

Because the index is a DatetimeIndex containing only dates, we can specify :None:None:`before` and :None:None:`after` as strings. They will be coerced to Timestamps before truncation.

This example is valid syntax, but we were not able to check execution
>>> df.truncate('2016-01-05', '2016-01-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

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.

This example is valid syntax, but we were not able to check execution
>>> df.loc['2016-01-05':'2016-01-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
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#9590
type: <class 'function'>
Commit: