如何使用Pandas根据实际日期查找一年中的天数?

问题描述:

我的数据框data的日期变量dateOpen具有以下格式date_format = "%Y-%m-%d %H:%M:%S.%f",我想创建一个名为openDay的新列,该列是基于一年365天的天数.我尝试应用以下内容

My data frame data has a date variable dateOpen with the following format date_format = "%Y-%m-%d %H:%M:%S.%f" and I would like to have a new column called openDay which is the day number based on 365 days a year. I tried applying the following

data['dateOpen'] = [datetime.strptime(dt, date_format) for dt in data['dateOpen']]
data['openDay'] = [dt.day for dt in data['dateOpen']]

但是,我有一个月中的某一天.例如,如果日期为2013-02-21 10:12:14.3,则上面的公式将返回21.但是,我希望它返回52,即从1月起的31天加上从2月起的21天.

however, I get the day in the month. For example if the date was 2013-02-21 10:12:14.3 then the above formula would return 21. However, I want it to return 52 which is 31 days from January plus the 21 days from February.

在Pandas中有一种简单的方法吗?

Is there a simple way to do this in Pandas?

最新大熊猫上,您可以使用

On latest pandas you can use date-time properties:

>>> ts = pd.Series(pd.to_datetime(['2013-02-21 10:12:14.3']))
>>> ts
0   2013-02-21 10:12:14.300000
dtype: datetime64[ns]
>>> ts.dt.dayofyear
0    52
dtype: int64

在旧版本中,您可能可以转换为DatetimeIndex然后使用.dayofyear属性:

On older versions, you may be able to convert to a DatetimeIndex and then use .dayofyear property:

>>> pd.Index(ts).dayofyear  # may work
array([52], dtype=int32)