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


Python Pandas dataframe.between_time()用法及代碼示例


Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。

Pandas dataframe.between_time()用於選擇一天中特定時間(例如9:00-9:30 AM)之間的值。不像dataframe.at_time()函數,此函數提取時間範圍內的值。此函數僅用於時間序列數據。 DataFrame 的索引必須是DatetimeIndex才能使用此函數。

用法: DataFrame.between_time(start_time, end_time, include_start=True, include_end=True)

參數:
start_time:datetime.time或字符串
end_time:datetime.time或字符串
include_start:布爾值,默認為True
include_end:布爾值,默認為True

返回:values_between_time:調用者類型

注意: between_time()當 DataFrame 的索引不是DatetimeIndex時,函數引發異常

範例1:采用between_time()函數查找給定時間間隔之間的值。

# importing pandas as pd 
import pandas as pd 
  
# Creating row index values for dataframe 
# Taken time frequency to be of 30 minutes interval 
# Generating eight index value using "period = 8" parameter 
ind = pd.date_range('01/01/2000', periods = 8, freq ='30T') 
  
# Creating a dataframe with 2 columns 
# using "ind" as the index for dataframe 
  
df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 6, 7, 8], 
                   "B":[10, 20, 30, 40, 50, 60, 70, 80]}, 
                                             index = ind) 
  
# Printing the dataframe 
df

現在,我們查詢“02:00”到“03:30”之間的時間

# Find the row values between time "02:00" to "03:30" 
df.between_time('02:00', '03:30')

輸出:


範例2:采用between_time()函數可查找給定時間間隔之間的值,但不包括開始時間和結束時間。

# importing pandas as pd 
import pandas as pd 
  
# Creating row index values for our data frame 
# Taken time frequency to be of 30 minutes interval 
# Generating eight index value using "period = 8" parameter 
ind = pd.date_range('01/01/2000', periods = 8, freq ='30T') 
  
# Creating a dataframe with 2 columns 
# using "ind" as the index for our dataframe 
  
df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 6, 7, 8], 
                   "B":[10, 20, 30, 40, 50, 60, 70, 80]}, 
                                             index = ind) 
  
# query for time between "02:00" to "03:30" with 
# both the start and end time values being excluded 
df.between_time('02:00', '03:30', include_start = False, 
                                    include_end = False)

輸出:

請注意,與開始時間和結束時間相對應的值未包含在由between_time()函數。



相關用法


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