Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.truncate() function is used to 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.
Syntax: Series.truncate(before=None, after=None, axis=None, copy=True)
Parameter :
before : Truncate all rows before this index value.
after : Truncate all rows after this index value.
axis : Axis to truncate. Truncates the index (rows) by default.
copy : Return a copy of the truncated section.
Returns : truncated Series or DataFrame.
Example #1: Use Series.truncate() function to truncate some data from the series prior to a given date.
import pandas as pd
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W',
periods = 6, tz = 'Europe/Berlin')
sr.index = didx
print(sr)
|
Output :

Now we will use Series.truncate() function to truncate data which are prior to ‘2014-08-17 10:00:00+02:00’ in the given Series object.
sr.truncate(before = '2014-08-17 10:00:00 + 02:00')
|
Output :

As we can see in the output, the Series.truncate() function has successfully truncated all data prior to the mentioned date.
Example #2: Use Series.truncate() function to truncate some data from the series prior to a given index label and after a given index label.
import pandas as pd
sr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])
print(sr)
|
Output :

Now we will use Series.truncate() function to truncate data which are prior to the 1st index label and after the 3rd index label in the given Series object.
sr.truncate(before = 1, after = 3)
|
Output :

As we can see in the output, the Series.truncate() function has successfully truncated all data prior to the mentioned index label and after the mentioned index label.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
05 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...