如何使用Pandas根据实际日期查找一年中的天数? [英] How to find the number of the day in a year based on the actual dates using Pandas?

查看:333
本文介绍了如何使用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)

这篇关于如何使用Pandas根据实际日期查找一年中的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆