Skip to Content
Python for Excel (Chinese Edition), 2nd Edition
book

Python for Excel (Chinese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
5h 50m
Chinese
O'Reilly Media, Inc.
Content preview from Python for Excel (Chinese Edition), 2nd Edition

第 7 章. 使用 pandas进行时间 序列分析

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

时间序列( )是一系列沿时间轴排列的数据点,在许多不同场景中都起着核心作用:交易员利用历史股价计算风险指标;天气预报则基于传感器测得的温度、湿度和气压等数据生成的时间序列;而数字营销部门则依赖网页生成的时间序列(例如每小时的页面访问来源和数量),并据此对营销活动得出结论。

时间序列分析是推动数据科学家和分析师开始使用 pandas 的主要动力之一。以下几点总结了这一转变背后的部分原因:

大型数据集

时间序列数据 很快就会超过Excel每张工作表约100万行的限制。例如,如果您处理的是基于Tick级别的盘中股价数据,通常需要处理数十万条记录——而且是针对每只股票的每一天!

日期与时间

正如我们在第 3 章所见,在处理时间序列的核心——日期和时间方面,Excel 存在诸多局限。 其中包括缺乏时区支持,以及数值格式仅限于毫秒精度。pandas 支持时区,并使用 NumPy 的datetime64[us]数据类型,其精度为微秒(“us”)。不过,如果数据有此需求,pandas 也会通过datetime64[ns] 提供纳秒级精度。

功能缺失

Excel 甚至缺乏高效处理时间序列数据的基本工具。例如,若要将日度时间序列转换为月度时间序列,尽管这是一项非常常见的任务,却没有简便的方法可实现。

DataFrame 允许您使用各种基于时间的索引:DatetimeIndex是最常见的类型,它表示带有时间戳的索引。其他索引类型,如PeriodIndex,则基于小时或月等时间间隔。不过在本章中,我们仅关注DatetimeIndex,我将在此对其进行更详细的介绍。

DatetimeIndex

在本节中,我们将学习DatetimeIndex 的基础知识,包括如何构建 DatetimeIndex、如何将文本转换为时间戳,以及如何将Datetime​Index过滤为特定时间范围。

创建 DatetimeIndex

若要手动构建DatetimeIndex,pandas提供了date_range。该函数接受起始日期、频率,以及周期数或结束日期:

In [1]: # Start by importing the packages we use in this chapter
        import numpy as np
        import pandas as pd
        import seaborn ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习勒索软件响应和恢复 (Chinese Edition)

学习勒索软件响应和恢复 (Chinese Edition)

W. Curtis Preston, Michael Saylor
Prometheus:快速入门,第二版

Prometheus:快速入门,第二版

Julien Pivotto, Brian Brazil

Publisher Resources

ISBN: 0642572396008