@@ -7476,65 +7476,113 @@ def tshift(self, periods=1, freq=None, axis=0):
7476
7476
7477
7477
def truncate (self , before = None , after = None , axis = None , copy = True ):
7478
7478
"""
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.
7482
7483
7483
7484
Parameters
7484
7485
----------
7485
7486
before : date, string, int
7486
- Truncate all rows before this index value
7487
+ Truncate all rows before this index value.
7487
7488
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.
7496
7492
copy : boolean, default is True,
7497
- return a copy of the truncated section
7493
+ Return a copy of the truncated section.
7498
7494
7499
7495
Returns
7500
7496
-------
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.
7502
7510
7503
7511
Examples
7504
7512
--------
7505
7513
>>> df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'],
7506
7514
... 'B': ['f', 'g', 'h', 'i', 'j'],
7507
7515
... 'C': ['k', 'l', 'm', 'n', 'o']},
7508
7516
... 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
+
7509
7525
>>> df.truncate(before=2, after=4)
7510
7526
A B C
7511
7527
2 b g l
7512
7528
3 c h m
7513
7529
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
7523
7530
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.
7528
7532
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.
7529
7551
>>> dates = pd.date_range('2016-01-01', '2016-02-01', freq='s')
7530
7552
>>> 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
+
7531
7574
>>> df.truncate('2016-01-05', '2016-01-10').tail()
7532
7575
A
7533
7576
2016-01-09 23:59:56 1
7534
7577
2016-01-09 23:59:57 1
7535
7578
2016-01-09 23:59:58 1
7536
7579
2016-01-09 23:59:59 1
7537
7580
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
+
7538
7586
>>> df.loc['2016-01-05':'2016-01-10', :].tail()
7539
7587
A
7540
7588
2016-01-10 23:59:55 1
0 commit comments