AttributeError: 'TimedeltaProperties' 对象在 Pandas 中没有属性 'years'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30950198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:30:32  来源:igfitidea点击:

AttributeError: 'TimedeltaProperties' object has no attribute 'years' in Pandas

pythondatetimepandas

提问by tumultous_rooster

In Pandas, why does a TimedeltaPropertiesobject have no attribute 'years'?

在 Pandas 中,为什么TimedeltaProperties对象没有属性“年”?

After all, the datetimeobject has this property.

毕竟datetime对象有这个属性。

It seems like a very natural thing for an object that is concerned with time to have. Especially if it already has an hours, seconds, etc attribute.

对于一个与时间有关的对象来说,这似乎是一件很自然的事情。特别是如果它已经有小时、秒等属性。

Is there a workaround so that my column, which is full of values like
10060 days,
can be converted to years? Or better yet, just converted to an integer representation for years?

有一种解决方法,使我的专栏,这是完全一样的值
10060 days
可以转换为年?或者更好的是,只是转换为整数表示多年?

回答by Anand S Kumar

TimedeltaPropertiesdoes not have yearor monthattributes because according to TimedeltaProperties source code. It is -

TimedeltaProperties没有yearmonth属性,因为根据 TimedeltaProperties源代码。这是 -

Accessor object for datetimelike properties of the Series values.

系列值的类似日期时间的属性的访问器对象。

But , monthsor yearshave no constant definition.

但是,months或者years没有常量定义。

1 monthcan take on different different number of days, based on the monthitself, like January -> 31 days , April -> 30 days , etc.

1 month可以采用不同的天数,根据month自身情况,例如一月 -> 31 天,四月 -> 30 天等。

1 monthcan take on different values based on the year as well (in case of February month) , if the year is 2004 , February has 29 days , if the year is 2003 February has 28 days, etc.

1 month也可以根据年份取不同的值(如果是二月),如果年份是 2004 年,二月有 29 天,如果年份是 2003 年二月有 28 天,等等。

Same is the case with years, it can take on different values based on which exact year it is, for example - if the year is 2003 , it has 365 days, if the year is 2004 it has 366 days.

与 的情况相同years,它可以根据具体年份采用不同的值,例如 - 如果年份是 2003 ,则有 365 天,如果年份是 2004 则有 366 天。

Hence, an requirement like - Convert 10060 daysto yearsis not accurate, which years?

因此,像 - Convert 10060 daysto这样的要求years是不准确的,哪个years

Like previously stated, the accurate amount of years those no. of days correspond to depend on the actual years those days represent.

如前所述,准确的年数没有。天数对应于这些天代表的实际年份。

回答by pableau

This workaround gets you closer.

这种变通办法让你离得更近了。

round((df["Accident Date"] - df["Iw Date Of Birth"]).dt.days / 365, 1)

round((df["Accident Date"] - df["Iw Date Of Birth"]).dt.days / 365, 1)