3
votes

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?

3

3 Answers

6
votes

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)
2
votes

Not sure if there's a pandas builtin, but in Python, you can get the "Julian" day, eg:

data['openDay'] = [int(format(dt, '%j')) for dt in data['dateOpen']]

Example:

>>> from datetime import datetime
>>> int(format(datetime(2013,2,21), '%j'))
52
0
votes
#To find number of days in this year sofar

from datetime import datetime
from datetime import date
today = date.today()
print("Today's date:", today)
print(int(format(today, '%j')))

Today's date: 2020-03-26
86