【Pandas】pandas Series last
Pandas2.2 Series
Computations descriptive stats
| 方法 | 描述 | 
|---|---|
| Series.align(other[, join, axis, level, …]) | 用于将两个 Series 对齐,使其具有相同的索引 | 
| Series.case_when(caselist) | 用于根据条件列表对 Series 中的元素进行条件判断并返回相应的值 | 
| Series.drop([labels, axis, index, columns, …]) | 用于从 Series 中删除指定的行或列(对于 Series 来说,通常是删除行) | 
| Series.droplevel(level[, axis]) | 用于从多层索引(MultiIndex)的 Series 中删除指定的索引层级 | 
| Series.drop_duplicates(*[, keep, inplace, …]) | 用于从 Series 中删除重复的值 | 
Series.duplicated([keep]) | 用于检测 Series 中的重复值 | 
| Series.equals(other) | 用于比较两个 Series 对象是否完全相等的方法 | 
| Series.first(offset) | 用于根据日期偏移量(offset)选择 Series 中时间序列数据的初始部分 | 
Series.head([n]) | 用于返回 Series 的前 n 个元素 | 
| Series.idxmax([axis, skipna]) | 用于返回 Series 中最大值的索引 | 
| Series.idxmin([axis, skipna]) | 用于返回 Series 中最小值的索引 | 
| Series.isin(values) | 用于检查 Series 中的每个元素是否存在于给定的值集合 values 中 | 
| Series.last(offset) | 用于根据日期偏移量(offset)选择 Series 中时间序列数据的末尾部分 | 
pandas.Series.last
pandas.Series.last(offset) 是 Pandas 库中的一个方法,用于根据日期偏移量(offset)选择 Series 中时间序列数据的末尾部分。它与 first() 方法类似,但方向相反,能够快速提取指定时间范围内的数据。
方法签名
Series.last(offset)
 
参数详解
offset:- 一个表示时间范围的字符串或 
DateOffset对象。 - 例如:
'3D'表示 3 天,'1M'表示 1 个月,'2Y'表示 2 年。 - 支持的偏移量单位包括: 
    
D:天M:月Y:年H:小时T或min:分钟S:秒
 - 默认值:无(必须提供)。
 
- 一个表示时间范围的字符串或 
 
返回值
- 返回一个新的 Series,包含从结束时间向前推算指定偏移量范围内的数据。
 
示例及结果
示例 1:按天偏移量选择数据
import pandas as pd
# 创建一个时间序列 Series
dates = pd.date_range('2023-10-01', periods=10, freq='D')
s = pd.Series(range(10), index=dates)
print("原 Series:")
print(s)
# 选择最后 3 天的数据
result = s.last('3D')
print("\n最后 3 天的数据:")
print(result)
 
结果:
原 Series:
2023-10-01    0
2023-10-02    1
2023-10-03    2
2023-10-04    3
2023-10-05    4
2023-10-06    5
2023-10-07    6
2023-10-08    7
2023-10-09    8
2023-10-10    9
Freq: D, dtype: int64
最后 3 天的数据:
2023-10-08    7
2023-10-09    8
2023-10-10    9
Freq: D, dtype: int64
 
解释:
last('3D')选择了从结束时间2023-10-10向前推算的 3 天数据。
示例 2:按月偏移量选择数据
import pandas as pd
# 创建一个时间序列 Series
dates = pd.date_range('2023-01-01', periods=12, freq='M')
s = pd.Series(range(12), index=dates)
print("原 Series:")
print(s)
# 选择最后 2 个月的数据
result = s.last('2M')
print("\n最后 2 个月的数据:")
print(result)
 
结果:
原 Series:
2023-01-31     0
2023-02-28     1
2023-03-31     2
2023-04-30     3
2023-05-31     4
2023-06-30     5
2023-07-31     6
2023-08-31     7
2023-09-30     8
2023-10-31     9
2023-11-30    10
2023-12-31    11
Freq: ME, dtype: int64
最后 2 个月的数据:
2023-11-30    10
2023-12-31    11
Freq: ME, dtype: int64
 
解释:
last('2M')选择了从结束时间2023-12-31向前推算的 2 个月数据。
示例 3:按小时偏移量选择数据
import pandas as pd
# 创建一个时间序列 Series
dates = pd.date_range('2023-10-01 00:00', periods=24, freq='H')
s = pd.Series(range(24), index=dates)
print("原 Series:")
print(s)
# 选择最后 6 小时的数据
result = s.last('6H')
print("\n最后 6 小时的数据:")
print(result)
 
结果:
原 Series:
2023-10-01 00:00:00     0
2023-10-01 01:00:00     1
2023-10-01 02:00:00     2
2023-10-01 03:00:00     3
2023-10-01 04:00:00     4
2023-10-01 05:00:00     5
2023-10-01 06:00:00     6
2023-10-01 07:00:00     7
2023-10-01 08:00:00     8
2023-10-01 09:00:00     9
2023-10-01 10:00:00    10
2023-10-01 11:00:00    11
2023-10-01 12:00:00    12
2023-10-01 13:00:00    13
2023-10-01 14:00:00    14
2023-10-01 15:00:00    15
2023-10-01 16:00:00    16
2023-10-01 17:00:00    17
2023-10-01 18:00:00    18
2023-10-01 19:00:00    19
2023-10-01 20:00:00    20
2023-10-01 21:00:00    21
2023-10-01 22:00:00    22
2023-10-01 23:00:00    23
Freq: h, dtype: int64
最后 6 小时的数据:
2023-10-01 18:00:00    18
2023-10-01 19:00:00    19
2023-10-01 20:00:00    20
2023-10-01 21:00:00    21
2023-10-01 22:00:00    22
2023-10-01 23:00:00    23
Freq: h, dtype: int64
 
解释:
last('6H')选择了从结束时间2023-10-01 23:00:00向前推算的 6 小时数据。
示例 4:按年偏移量选择数据
import pandas as pd
# 创建一个时间序列 Series
dates = pd.date_range('2020-01-01', periods=5, freq='Y')
s = pd.Series(range(5), index=dates)
print("原 Series:")
print(s)
# 选择最后 2 年的数据
result = s.last('2Y')
print("\n最后 2 年的数据:")
print(result)
 
结果:
原 Series:
2020-12-31    0
2021-12-31    1
2022-12-31    2
2023-12-31    3
2024-12-31    4
Freq: YE-DEC, dtype: int64
最后 2 年的数据:
2023-12-31    3
2024-12-31    4
Freq: YE-DEC, dtype: int64
 
解释:
last('2Y')选择了从结束时间2024-12-31向前推算的 2 年数据。
注意事项
last()方法仅适用于具有DatetimeIndex的 Series。- 如果索引不是时间序列类型,会抛出 
TypeError。 offset参数必须是一个有效的日期偏移量字符串或DateOffset对象。
通过 last() 方法,可以方便地从时间序列数据中提取指定时间范围内的末尾数据,适用于快速分析和处理时间序列数据。
