[英]Lambda Apply to find difference between two dates
I'm trying to use apply method with lambda to find the months between two dates.我正在尝试对 lambda 使用 apply 方法来查找两个日期之间的月份。 I'm currently getting a attribute error:
我目前遇到属性错误:
AttributeError: 'datetime.date' object has no attribute 'dt'
AttributeError: 'datetime.date' object 没有属性 'dt'
My upfront conversion:我的前期转换:
df['date1'] = pd.to_datetime(df['date1'], errors='ignore', infer_datetime_format=True)
df['date2'] = pd.to_datetime(df['date2'], errors='ignore', infer_datetime_format=True)
Here is my block:这是我的街区:
df['Duration (Months)'] = df.apply(lambda x: x["Date1"].dt.to_period('M').astype(int) - x["Date2"].dt.to_period('M').astype(int), axis=1)
Second attempt:第二次尝试:
df['Duration (Months)'] = df['date1'].dt.to_period('M').astype(int) - df['date2'].dt.to_period('M').astype(int)
Any thoughts on where I'm going wrong?关于我哪里出错的任何想法?
From the documentation :从文档中:
Series has an accessor to succinctly return datetime like properties for the values of the Series, if it is a datetime/period like Series.
Series有一个访问器,可以简洁地返回类似于 Series 值的日期时间属性,如果它是类似于 Series 的日期时间/时间段。 This will return a Series, indexed like the existing Series.
这将返回一个系列,索引与现有系列相同。
So there is no need to use the .dt
accessor when calling pandas.Series.apply
because this one access to each element (that is already a datetime
) individually.因此,在调用
pandas.Series.apply
时无需使用.dt
访问器,因为它可以单独访问每个元素(已经是datetime
)。 Hence the errors below (depending on the type of your Series):因此出现以下错误(取决于您系列的类型):
AttributeError: 'datetime.date' object has no attribute 'dt'
AttributeError: 'Timestamp' object has no attribute 'dt'
Try this instead:试试这个:
(df.apply(lambda x: x["date1"].to_period('M') - x["date2"].to_period('M'), axis=1))
Or with a vectorial code:或者使用矢量代码:
(df["date1"].dt.to_period('M') - df["date2"].dt.to_period("M")) #here, we needed the .dt accessor
0 <0 * MonthEnds>
1 <-1 * MonthEnd>
2 <6 * MonthEnds>
dtype: object
This will return a pandas.tseries.offsets.DateOffset
.这将返回
pandas.tseries.offsets.DateOffset
。 Therefore to cast a number/int, you can use operator.attrgetter
to get the name as an attribute:因此,要转换一个数字/整数,您可以使用
operator.attrgetter
来获取名称作为属性:
from operator import attrgetter
(df["date1"].dt.to_period('M') - df["date2"].dt.to_period("M")).apply(attrgetter("n"))
0 0
1 -1
2 6
dtype: int64
Used input:使用的输入:
date1 date2
0 2022-01-13 2022-01-01
1 2022-02-05 2022-03-06
2 2022-10-14 2022-04-09
date1 datetime64[ns]
date2 datetime64[ns]
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.