繁体   English   中英

Lambda 申请求两个日期的差异

[英]Lambda Apply to find difference between two dates

我正在尝试对 lambda 使用 apply 方法来查找两个日期之间的月份。 我目前遇到属性错误:

AttributeError: 'datetime.date' object 没有属性 'dt'

我的前期转换:

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)

这是我的街区:

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)

第二次尝试:

df['Duration (Months)'] = df['date1'].dt.to_period('M').astype(int) - df['date2'].dt.to_period('M').astype(int)

关于我哪里出错的任何想法?

文档中:

Series有一个访问器,可以简洁地返回类似于 Series 值的日期时间属性,如果它是类似于 Series 的日期时间/时间段。 这将返回一个系列,索引与现有系列相同。

因此,在调用pandas.Series.apply时无需使用.dt访问器,因为它可以单独访问每个元素(已经是datetime )。 因此出现以下错误(取决于您系列的类型):

AttributeError: 'datetime.date' object has no attribute 'dt'
AttributeError: 'Timestamp' object has no attribute 'dt'

试试这个:

(df.apply(lambda x: x["date1"].to_period('M') - x["date2"].to_period('M'), axis=1))

或者使用矢量代码:

(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

这将返回pandas.tseries.offsets.DateOffset 因此,要转换一个数字/整数,您可以使用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

使用的输入:

       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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM