Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the number of the day in a year based on the actual dates using Pandas?

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']]

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.

Is there a simple way to do this in Pandas?

like image 568
finstats Avatar asked Oct 16 '25 00:10

finstats


1 Answers

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

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)
like image 190
behzad.nouri Avatar answered Oct 18 '25 15:10

behzad.nouri