Series.dt可用於以datetimelike的形式訪問序列的值並返回幾個屬性。 Pandas  Series.dt.month屬性返回一個numpy數組,其中包含給定係列對象的基礎數據中的datetime的月份。
用法: Series.dt.month
參數:沒有
返回:numpy數組
範例1:采用Series.dt.month屬性以在給定Series對象的基礎數據中返回日期時間的月份。
# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', 
                '2010-4-22 09:25', '2019-11-8 02:22']) 
  
# Creating the index 
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] 
  
# set the index 
sr.index = idx 
  
# Convert the underlying data to datetime  
sr = pd.to_datetime(sr) 
  
# Print the series 
print(sr)輸出:

現在我們將使用Series.dt.month屬性以在給定Series對象的基礎數據中返回日期時間的月份。
# return the month 
result = sr.dt.month 
  
# print the result 
print(result)輸出:

正如我們在輸出中看到的,Series.dt.month屬性已成功訪問並返回給定係列對象的基礎數據中日期時間的月份。
範例2:采用Series.dt.month屬性以在給定Series對象的基礎數據中返回日期時間的月份。
# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series(pd.date_range('2012-12-12 12:12', 
                       periods = 5, freq = 'H')) 
  
# Creating the index 
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] 
  
# set the index 
sr.index = idx 
  
# Print the series 
print(sr)輸出:

現在我們將使用Series.dt.month屬性以在給定Series對象的基礎數據中返回日期時間的月份。
# return the month 
result = sr.dt.month 
  
# print the result 
print(result)輸出:

正如我們在輸出中看到的,Series.dt.month屬性已成功訪問並返回給定係列對象的基礎數據中日期時間的月份。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Timestamp.tz用法及代碼示例
- Python Pandas Series.str.contains()用法及代碼示例
- Python Pandas dataframe.std()用法及代碼示例
- Python Pandas Timestamp.dst用法及代碼示例
- Python Pandas dataframe.sem()用法及代碼示例
- Python Pandas DataFrame.ix[ ]用法及代碼示例
- Python Pandas.Categorical()用法及代碼示例
- Python Pandas.apply()用法及代碼示例
- Python Pandas TimedeltaIndex.contains用法及代碼示例
- Python Pandas Timestamp.now用法及代碼示例
- Python Pandas Series.str.pad()用法及代碼示例
- Python Pandas Series.take()用法及代碼示例
- Python Pandas dataframe.all()用法及代碼示例
- Python Pandas series.str.get()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.dt.month。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
