當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas Series.at_time()用法及代碼示例

Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多方法來執行涉及索引的操作。

Pandas Series.at_time()函數用於在一天中的特定時間(例如上午9:30)選擇給定係列對象中的值。

用法: Series.at_time(time, asof=False, axis=None)

參數:
time:datetime.time或字符串
axis:{0或“索引”,1或“列”},默認0

返回:values_at_time:與調用方類型相同

範例1:采用Series.at_time()函數可在一天中的特定時間返回給定係列對象中的值。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None]) 
  
# Create the Index 
index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='H') 
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.at_time()函數可在一天中的特定時間返回給定係列對象中的值。

# return values at particular time of the day 
result = sr.at_time(time = '13:45:00') 
  
# Print the result 
print(result)

輸出:

正如我們在輸出中看到的,Series.at_time()函數已成功返回給定係列對象中當天特定時間的值。

範例2:采用Series.at_time()函數可在一天中的特定時間返回給定係列對象中的值。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None]) 
  
# Create the Index 
# apply monthly frequency 
index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='M') 
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.at_time()函數可在一天中的特定時間返回給定係列對象中的值。

# return values at particular time of the day 
result = sr.at_time(time = '08:45:00') 
  
# Print the result 
print(result)

輸出:


正如我們在輸出中看到的,Series.at_time()函數已成功返回給定係列對象中當天特定時間的值。係列對象中的所有值都已返回,因為它們的時間值等於經過的時間。



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.at_time()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。