本文介绍了两个日期之间的年数列='Timedelta'对象没有属性'item'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找两个日期到小数点后四个位之间的年数.我的数据:

Looking for the number of years between two dates to 4 decimal places. My Data:

df_Years = df[
            df['state'].str.contains('Done')

            ][[  
               'maturity_date'
              ]].copy()

df_Years['maturity_date'] = pd.to_datetime(df_Date['maturity_date'])
df_Years['Today'] = pd.to_datetime('today') 
display(df_Years.head(6))


maturity_date   Today
13  2022-12-15  2018-03-21
81  2028-02-15  2018-03-21
82  2045-12-01  2018-03-21
100 2025-08-18  2018-03-21
115 2019-01-16  2018-03-21
116 2018-12-21  2018-03-21

display(df_Years.dtypes)

maturity_date    datetime64[ns]
Today            datetime64[ns]
dtype: object
#Dataframe types
df_Years['Year_To_Maturity'] = df_Years['maturity_date'] - df_Years['Today']
df_Years['Year_To_Maturity'] = df_Years['Year_To_Maturity'].apply(lambda x: float(x.item().days)/365)
AttributeError: 'Timedelta' object has no attribute 'item'
df_Years['Year_To_Maturity'] = df_Years['maturity_date'] - df_Years['Today']
df_Years['Year_To_Maturity'] = df_Years['Year_To_Maturity'].apply(lambda x: float(x.item().days)/365)
maturity_date   Today   Year_To_Maturity
13  2022-12-15  2018-03-21  <map object at 0x00000000143F9C88>
81  2028-02-15  2018-03-21  <map object at 0x00000000143F9C88>
82  2045-12-01  2018-03-21  <map object at 0x00000000143F9C88>
100 2025-08-18  2018-03-21  <map object at 0x00000000143F9C88>
115 2019-01-16  2018-03-21  <map object at 0x00000000143F9C88>
116 2018-12-21  2018-03-21  <map object at 0x00000000143F9C88>

推荐答案

我认为您需要 sub 进行减法,通过 dt.days ,除以 div 和最后一个 round :

I think you need sub for subtract, convert timedeltas to days by dt.days, divide by div and last round:

df_Years['Year_To_Maturity'] = (df_Years['maturity_date'].sub(df_Years['Today'])
                                                         .dt.days
                                                         .div(365)
                                                         .round(4))
print (df_Years)
  maturity_date      Today  Year_To_Maturity
0    2022-12-15 2018-03-21            4.7397
1    2028-02-15 2018-03-21            9.9123
2    2045-12-01 2018-03-21           27.7178
3    2025-08-18 2018-03-21            7.4164
4    2019-01-16 2018-03-21            0.8247
5    2018-12-21 2018-03-21            0.7534

感谢@pir提供更好的解决方案:

df_Years['Year_To_Maturity'] = (df_Years['maturity_date'].sub(df_Years['Today'])
                                                         .dt.days
                                                         .div(365.25)
                                                         .round(4))
print (df_Years)
  maturity_date      Today  Year_To_Maturity
0    2022-12-15 2018-03-21            4.7365
1    2028-02-15 2018-03-21            9.9055
2    2045-12-01 2018-03-21           27.6988
3    2025-08-18 2018-03-21            7.4114
4    2019-01-16 2018-03-21            0.8241
5    2018-12-21 2018-03-21            0.7529

这篇关于两个日期之间的年数列='Timedelta'对象没有属性'item'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:36