当前位置: 首页 > news >正文

【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)

参数详解
  1. offset:
    • 一个表示时间范围的字符串或 DateOffset 对象。
    • 例如:'3D' 表示 3 天,'1M' 表示 1 个月,'2Y' 表示 2 年。
    • 支持的偏移量单位包括:
      • D:天
      • M:月
      • Y:年
      • H:小时
      • Tmin:分钟
      • 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 年数据。

注意事项
  1. last() 方法仅适用于具有 DatetimeIndex 的 Series。
  2. 如果索引不是时间序列类型,会抛出 TypeError
  3. offset 参数必须是一个有效的日期偏移量字符串或 DateOffset 对象。

通过 last() 方法,可以方便地从时间序列数据中提取指定时间范围内的末尾数据,适用于快速分析和处理时间序列数据。

相关文章:

  • Docker镜像拉取失败解决方案
  • centos7配置rsyslog日志服务器
  • 【阮一峰】5.函数
  • C++:并发编程基础
  • 【前端ES】ECMAScript 2023 (ES14) 引入了多个新特性,简单介绍几个不为人知但却好用的方法
  • 华为交换机堆叠技术简介配置
  • .NET SixLabors.ImageSharp v1.0 图像实用程序控制台示例
  • PBR光照模型相关知识
  • Spring核心思想之—AOP(面向切面编程)
  • 【笔记】LLM|Ubuntu22服务器极简本地部署DeepSeek+联网使用方式
  • Windows 图形显示驱动开发-WDDM 2.0-GPU 虚拟地址
  • 蓝桥杯单片机基础部分——单片机介绍部分
  • 浅析前端监控与埋点:洞察用户行为,优化产品体验
  • GTP3 大模型
  • vue3项目axios最简单封装 - ajax请求封装
  • 深入解析 MySQL 数据删除操作:DELETE、TRUNCATE 与 DROP 的原理与选择
  • MySQL 之存储引擎(MySQL Storage Engine)
  • 软件内有离线模型,效果也很实用......
  • DeepSeek AI 视频创作完整指南:从注册到制作
  • 第一章——1.2 Java“白皮书”的关键术语
  • 宇数科技王兴兴:第一桶金来自上海,欢迎上海的年轻人加入
  • 国家主席习近平会见斯洛伐克总理菲佐
  • 圆桌丨中俄权威专家详解:两国携手维护战后国际秩序,捍卫国际公平正义
  • 专访|“甲亢哥”的操盘手,带NBA球星们玩转中国流量
  • 巴基斯坦军方称印度袭击已致26死46伤
  • 重庆动物园大熊猫被游客扔玻璃瓶,相同地方曾被扔可乐瓶