简体   繁体   English

将年中的第几天 integer 转换为 Python 中的日期和月份整数

[英]Convert Day of Year integer to Day and Month integers in Python

I have an integer representing the day of the year from 1-365 in my array out at index 0. How do i convert this to get the month and day of month from this using Python and Pandas?我有一个 integer 表示我的数组out 1 到 365 的一年中的第几天,位于索引 0 处。如何使用 Python 和 Pandas 将其转换为从中获取月份和日期?

I have tried:我试过了:

out[0] = pd.to_datetime(out[0]).dt.strftime('%m-%d')

but I get an error saying AttributeError: 'Timestamp' object has no attribute 'dt'但我收到一条错误消息说AttributeError: 'Timestamp' object has no attribute 'dt'

You have to know if it's a leap year or not?你必须知道今年是不是闰年? If it's a leap year, February has 29 days, if not 28. Otherwise your conversion will not be correct.如果是闰年,2 月有 29 天,如果不是 28 天。否则你的转换将不正确。

Leap Years are any year that can be exactly divided by 4 (such as 2016, 2020, 2024, etc) except if it can be exactly divided by 100, then it isn't (such as 2100, 2200, etc) except if it can be exactly divided by 400, then it is (such as 2000, 2400)闰年是任何可以被 4 整除的年份(例如 2016、2020、2024 等),除非它可以被 100 整除,否则就不能(例如 2100、2200 等)除非它能被400整除则为(如2000、2400)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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