Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.truncate#

Series.truncate(before=None,after=None,axis=None,copy=None)[source]#

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

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

Parameters:
beforedate, str, int

Truncate all rows before this index value.

afterdate, 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.ForSeries this parameter is unused and defaults to 0.

copybool, default is True,

Return a copy of the truncated section.

Note

Thecopy keyword will change behavior in pandas 3.0.Copy-on-Writewill be enabled by default, which means that all methods with acopy keyword will use a lazy copy mechanism to defer the copy andignore thecopy keyword. Thecopy keyword will be removed in afuture version of pandas.

You can already get the future behavior and improvements throughenabling copy on writepd.options.mode.copy_on_write=True

Returns:
type of caller

The truncated Series or DataFrame.

See also

DataFrame.loc

Select a subset of a DataFrame by label.

DataFrame.iloc

Select a subset of a DataFrame by position.

Notes

If the index being truncated contains only datetime values,before andafter may be specified as strings instead ofTimestamps.

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 intruncate can be datetimes or stringdates.

>>>dates=pd.date_range('2016-01-01','2016-02-01',freq='s')>>>df=pd.DataFrame(index=dates,data={'A':1})>>>df.tail()                     A2016-01-31 23:59:56  12016-01-31 23:59:57  12016-01-31 23:59:58  12016-01-31 23:59:59  12016-02-01 00:00:00  1
>>>df.truncate(before=pd.Timestamp('2016-01-05'),...after=pd.Timestamp('2016-01-10')).tail()                     A2016-01-09 23:59:56  12016-01-09 23:59:57  12016-01-09 23:59:58  12016-01-09 23:59:59  12016-01-10 00:00:00  1

Because the index is a DatetimeIndex containing only dates, we canspecifybefore andafter as strings. They will be coerced toTimestamps before truncation.

>>>df.truncate('2016-01-05','2016-01-10').tail()                     A2016-01-09 23:59:56  12016-01-09 23:59:57  12016-01-09 23:59:58  12016-01-09 23:59:59  12016-01-10 00:00:00  1

Note thattruncate assumes a 0 value for any unspecified timecomponent (midnight). This differs from partial string slicing, whichreturns any partially matching dates.

>>>df.loc['2016-01-05':'2016-01-10',:].tail()                     A2016-01-10 23:59:55  12016-01-10 23:59:56  12016-01-10 23:59:57  12016-01-10 23:59:58  12016-01-10 23:59:59  1

[8]ページ先頭

©2009-2025 Movatter.jp