一年中 pandas 的日期时间等于多少? [英] What is the datetime equivalent for pandas dayofyear?
问题描述
我希望从 datetimes
列表中获取一年中某天的值。我可以使用方法 isocalendar
来获取星期数。但是一年中整数天的方法是什么?使用熊猫是 dayofyear ,但是如何使用 datetime
包获取此信息?
I'm looking to get an array of values of day of year from a list of datetimes
. I can get the week number, for example, using the method isocalendar
. But what's the method for an integer day of year? Using pandas this is dayofyear, but how do I get this using the datetime
package?
from datetime import timedelta, datetime
import numpy as np
import pandas as pd
from dateutil.relativedelta import *
datetimes = np.arange(
datetime(2012,1,1), datetime(2017,1,1), timedelta(days=1)
).astype(datetime)
week_value_by_day = np.array([datetime.isocalendar()[1] for datetime in datetimes])
最后,我想要一个数组使用此代码 np.array([datetime。
其中,< something>
给我一年中的整数,结果为 array([1、2、3、4、5、6..365])
。
At the end of the day, I want an array using this code np.array([datetime.<something> for datetime in datetimes])
where the <something>
gives me the integer day of year and results in array([1, 2, 3, 4, 5, 6..365])
.
推荐答案
尝试使用strftime:
Try using strftime:
np.array([int(i.strftime("%j")) for i in datetimes])
但是
np.array([datetime.timetuple().tm_yday for datetime in datetimes])
有效且速度更快。
这篇关于一年中 pandas 的日期时间等于多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!