如何从 Pandas 的日期时间列中提取月份和年份
Suraj Joshi
2023年1月30日
Pandas
Pandas Datetime
-
pandas.Series.dt.year()和pandas.Series.dt.month()方法提取月份和年份 -
strftime()方法提取年份和月份 -
pandas.DatetimeIndex.month和pandas.DatetimeIndex.year提取年份和月份
我们可以分别使用 pandas.Series.dt.year() 和 pandas.Series.dt.month() 方法从 Datetime 列中提取年份和月份。如果数据不是 Datetime 类型,则需要先将其转换为 Datetime。我们还可以使用 pandas.DatetimeIndex.month 和 pandas.DatetimeIndex.year 和 strftime() 方法提取年份和月份。
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.month 和 pandas.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。它具有年,月,天等属性。
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Suraj Joshi
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn