day of Year values starting from a particular date

删除回忆录丶 提交于 2021-02-05 06:40:55

问题


I have a dataframe with a date column. The duration is 365 days starting from 02/11/2017 and ending at 01/11/2018.

 Date
    02/11/2017
    03/11/2017
    05/11/2017
    .
    .
    01/11/2018

I want to add an adjacent column called Day_Of_Year as follows:

Date              Day_Of_Year
02/11/2017           1
03/11/2017           2
05/11/2017           4
.
.
01/11/2018          365

I apologize if it's a very basic question, but unfortunately I haven't been able to start with this.

I could use datetime(), but that would return values such as 1 for 1st january, 2 for 2nd january and so on.. irrespective of the year. So, that wouldn't work for me.


回答1:


First convert column to_datetime and then subtract datetime, convert to days and add 1:

df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y')
df['Day_Of_Year'] = df['Date'].sub(pd.Timestamp('2017-11-02')).dt.days + 1
print (df)
         Date  Day_Of_Year
0  02/11/2017            1
1  03/11/2017            2
2  05/11/2017            4
3  01/11/2018          365

Or subtract by first value of column:

df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y')
df['Day_Of_Year'] = df['Date'].sub(df['Date'].iat[0]).dt.days + 1

print (df)
        Date  Day_Of_Year
0 2017-11-02            1
1 2017-11-03            2
2 2017-11-05            4
3 2018-11-01          365



回答2:


Using strftime with '%j'

s=pd.to_datetime(df.Date,dayfirst=True).dt.strftime('%j').astype(int)
s-s.iloc[0]
Out[750]: 
0    0
1    1
2    3
Name: Date, dtype: int32
#df['new']=s-s.iloc[0]



回答3:


Python has dayofyear. So put your column in the right format with pd.to_datetime and then apply Series.dt.dayofyear. Lastly, use some modulo arithmetic to find everything in terms of your original date

df['Date'] =  pd.to_datetime(df['Date'], format='%d/%m/%Y')
df['day of year'] = df['Date'].dt.dayofyear - df['Date'].dt.dayofyear[0] + 1
df['day of year'] = df['day of year'] + 365*((365 - df['day of year']) // 365)

Output

       Date       day of year
0 2017-11-02                1
1 2017-11-03                2
2 2017-11-05                4
3 2018-11-01              365

But I'm doing essentially the same as Jezrael in more lines of code, so my vote goes to her/him



来源:https://stackoverflow.com/questions/53595993/day-of-year-values-starting-from-a-particular-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!