Pandas Series 的 between_time 方法

PandasPandasBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

Pandas 的 between_time() 方法用于在 Pandas Series 对象中选择一天中指定时间范围内的值。它返回一个包含指定时间范围内值的新 Series 对象。如果索引不是 DatetimeIndex,则会引发 TypeError。

虚拟机提示

虚拟机启动完成后,点击左上角切换到 Notebook 标签页以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟,直到 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时可以询问 Labby。在实验结束后提供反馈,我们将及时为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) pandas(("Pandas")) -.-> pandas/DataSelectionGroup(["Data Selection"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) pandas(("Pandas")) -.-> pandas/AdvancedOperationsGroup(["Advanced Operations"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") pandas/DataSelectionGroup -.-> pandas/select_rows("Select Rows") python/FunctionsGroup -.-> python/arguments_return("Arguments and Return Values") python/FunctionsGroup -.-> python/default_arguments("Default Arguments") python/ModulesandPackagesGroup -.-> python/using_packages("Using Packages") pandas/AdvancedOperationsGroup -.-> pandas/time_series_analysis("Time Series Analysis") python/PythonStandardLibraryGroup -.-> python/date_time("Date and Time") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("Data Analysis") subgraph Lab Skills python/conditional_statements -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} pandas/select_rows -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} python/arguments_return -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} python/default_arguments -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} python/using_packages -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} pandas/time_series_analysis -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} python/date_time -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} python/data_collections -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} python/data_analysis -.-> lab-68748{{"Pandas Series 的 between_time 方法"}} end

创建一个带有 DatetimeIndex 的 Series

第一步是创建一个带有 DatetimeIndex 的 Pandas Series。可以通过使用 pd.date_range() 函数生成一个日期和时间范围,并在创建 Series 对象时将其作为 index 参数传递。

import pandas as pd

## 生成一个日期和时间范围
values = pd.date_range('2021-01-01', periods=5, freq='H')

## 创建一个带有 DatetimeIndex 的 Series
series = pd.Series([1, 2, 3, 4, 5], index=values)

## 打印 Series
print(series)

使用 between_time() 方法选择特定开始和结束时间之间的值

between_time() 方法可用于选择特定开始时间和结束时间之间的值。只需将开始时间和结束时间作为参数传递给该方法即可。该方法会返回一个新的 Series 对象,其中仅包含指定时间范围内的值。

## 选择 8:00 到 12:00 之间的值
selected_values = series.between_time('8:00', '12:00')

## 打印选中的值
print(selected_values)

自定义 include_startinclude_end 参数

默认情况下,between_time() 方法会在结果中包含开始时间和结束时间。然而,你可以通过将 include_startinclude_end 参数设置为 False 来自定义这一行为。如果你希望从结果中排除开始时间和结束时间,这会非常有用。

## 选择 8:00 到 12:00 之间的值,但不包含开始时间和结束时间
selected_values = series.between_time('8:00', '12:00', include_start=False, include_end=False)

## 打印选中的值
print(selected_values)

总结

Pandas 中的 between_time() 方法允许你在 Series 对象中选择一天中指定时间范围内的值。通过自定义 include_startinclude_end 参数,你可以控制是否在结果中包含开始时间和结束时间。此方法对于基于特定时间范围或间隔过滤数据非常有用。