如何從 Pandas 的日期時間列中提取月份和年份

Suraj Joshi 2023年1月30日 2020年6月9日
  1. pandas.Series.dt.year()pandas.Series.dt.month() 方法提取月份和年份
  2. strftime() 方法提取年份和月份
  3. pandas.DatetimeIndex.monthpandas.DatetimeIndex.year 提取年份和月份
如何從 Pandas 的日期時間列中提取月份和年份

我們可以分別使用 pandas.Series.dt.year()pandas.Series.dt.month() 方法從 Datetime 列中提取年份和月份。如果資料不是 Datetime 型別,則需要先將其轉換為 Datetime。我們還可以使用 pandas.DatetimeIndex.monthpandas.DatetimeIndex.yearstrftime() 方法提取年份和月份。

pandas.Series.dt.year()pandas.Series.dt.month() 方法提取月份和年份

應用於 Datetime 型別的 pandas.Series.dt.year()pandas.Series.dt.month() 方法分別返回系列物件中 Datetime 條目的年和月的 numpy 陣列。

import pandas as pd
import numpy as np
import datetime

list_of_dates = ['2019-11-20', '2020-01-02', '2020-02-05','2020-03-10','2020-04-16']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({'Joined date': pd.to_datetime(list_of_dates)},index=employees)

df['Year'] = df['Joined date'].dt.year 
df['Month'] = df['Joined date'].dt.month 
print(df)

輸出:

        Joined date  Year  Month
Hisila   2019-11-20  2019     11
Shristi  2020-01-02  2020      1
Zeppy    2020-02-05  2020      2
Alina    2020-03-10  2020      3
Jerry    2020-04-16  2020      4

但是,如果該列不是 Datetime 型別,則應首先使用 to_datetime() 方法將該列轉換為 Datetime 型別。

import pandas as pd
import numpy as np
import datetime

list_of_dates = ['11/20/2019', '01/02/2020', '02/05/2020','03/10/2020','04/16/2020']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({'Joined date': pd.to_datetime(list_of_dates)},index=employees)
df['Joined date']= pd.to_datetime(df['Joined date']) 

df['Year'] = df['Joined date'].dt.year 
df['Month'] = df['Joined date'].dt.month 
print(df)

輸出:

        Joined date  Year  Month
Hisila   2019-11-20  2019     11
Shristi  2020-01-02  2020      1
Zeppy    2020-02-05  2020      2
Alina    2020-03-10  2020      3
Jerry    2020-04-16  2020      4

strftime() 方法提取年份和月份

strftime() 方法使用 Datetime,將格式程式碼作為輸入,並返回表示輸出中指定的特定格式的字串。我們使用%Y%m 作為格式程式碼來提取年份和月份。

import pandas as pd
import numpy as np
import datetime

list_of_dates = ['2019-11-20', '2020-01-02', '2020-02-05','2020-03-10','2020-04-16']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({'Joined date': pd.to_datetime(list_of_dates)},index=employees)

df['year'] = df['Joined date'].dt.strftime('%Y')
df['month'] = df['Joined date'].dt.strftime('%m')

print(df)

輸出:

        Joined date  year month
Hisila   2019-11-20  2019    11
Shristi  2020-01-02  2020    01
Zeppy    2020-02-05  2020    02
Alina    2020-03-10  2020    03
Jerry    2020-04-16  2020    04

pandas.DatetimeIndex.monthpandas.DatetimeIndex.year 提取年份和月份

Datetime 列中提取月份和年份的另一種簡單方法是檢索 pandas.DatetimeIndex 物件的年份和月份屬性的值類。

import pandas as pd
import numpy as np
import datetime

list_of_dates = ['2019-11-20', '2020-01-02', '2020-02-05','2020-03-10','2020-04-16']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({'Joined date': pd.to_datetime(list_of_dates)},index=employees)

df['year'] = pd.DatetimeIndex(df['Joined date']).year
df['month'] = pd.DatetimeIndex(df['Joined date']).month

print(df)

輸出:

        Joined date  Year  Month
Hisila   2019-11-20  2019     11
Shristi  2020-01-02  2020      1
Zeppy    2020-02-05  2020      2
Alina    2020-03-10  2020      3
Jerry    2020-04-16  2020      4

pandas.DatetimeIndex 類是 datetime64 資料型別的不變型別 ndarray。它具有年,月,天等屬性。

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn