Pandas Series: truncate() function
Series-truncate() function
The 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(self, before=None, after=None, axis=None, copy=True)
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
before | Truncate all rows before this index value. | date, string, int | Required |
after | WTruncate all rows after this index value. | date, string, int | Required |
axis | Axis to truncate. Truncates the index (rows) by default. | {0 or ‘index’, 1 or ‘columns’} | optional |
copy | Return a copy of the truncated section. | boolean Default Value: True |
optional |
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.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
df
Output:
X Y Z 1 j o t 2 k p u 3 l q v 4 m r w 5 n s x
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
df.truncate(before=2, after=4)
Output:
X Y Z 2 k p u 3 l q v 4 m r w
Example - The columns of a DataFrame can be truncated:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
df.truncate(before="X", after="Y", axis="columns")
Output:
X Y 1 j o 2 k p 3 l q 4 m r 5 n s
Example - For Series, only rows can be truncated:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
df['X'].truncate(before=2, after=4)
Output:
2 k 3 l 4 m Name: X, dtype: object
Example - The index values in truncate can be datetimes or string dates:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
dates = pd.date_range('2019-01-01', '2019-02-01', freq='s')
df = pd.DataFrame(index=dates, data={'X': 1})
df.tail()
Output:
X 2019-01-31 23:59:56 1 2019-01-31 23:59:57 1 2019-01-31 23:59:58 1 2019-01-31 23:59:59 1 2019-02-01 00:00:00 1
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
dates = pd.date_range('2019-01-01', '2019-02-01', freq='s')
df = pd.DataFrame(index=dates, data={'X': 1})
df.truncate(before=pd.Timestamp('2019-01-05'),
after=pd.Timestamp('2019-01-10')).tail()
Output:
X 2019-01-09 23:59:56 1 2019-01-09 23:59:57 1 2019-01-09 23:59:58 1 2019-01-09 23:59:59 1 2019-01-10 00:00:00 1
Because the index is a DatetimeIndex containing only dates, we can specify before and after as strings.
Example - They will be coerced to Timestamps before truncation:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
dates = pd.date_range('2019-01-01', '2019-02-01', freq='s')
df = pd.DataFrame(index=dates, data={'X': 1})
df.truncate('2019-01-05', '2019-01-10').tail()
Output:
X 2019-01-09 23:59:56 1 2019-01-09 23:59:57 1 2019-01-09 23:59:58 1 2019-01-09 23:59:59 1 2019-01-10 00:00:00 1
Note: Truncate assumes a 0 value for any unspecified time component (midnight). This differs from partial string slicing, which returns any partially matching dates.
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({'X': ['j', 'k', 'l', 'm', 'n'],
'Y': ['o', 'p', 'q', 'r', 's'],
'Z': ['t', 'u', 'v', 'w', 'x']},
index=[1, 2, 3, 4, 5])
dates = pd.date_range('2019-01-01', '2019-02-01', freq='s')
df = pd.DataFrame(index=dates, data={'X': 1})
df.loc['2019-01-05':'2019-01-10', :].tail()
Output:
X 2019-01-10 23:59:55 1 2019-01-10 23:59:56 1 2019-01-10 23:59:57 1 2019-01-10 23:59:58 1 2019-01-10 23:59:59 1
Previous: Series-tail() function
Next: Series-where() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-truncate.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics