pythonpandasdatetimedays

Create a Pandas Dataframe Date Column to Day of Year


I know this should be easy but for some reason, I cannot get the result that I need. I have data that looks like this where 'raw_time' is read into a df in the date format yyyy-mm-dd hh:mm:ss. It looks like this:

dfdates =

1429029   1992-01-03 02:00:00
1429030   1992-01-03 01:00:00
1429031   1992-01-03 00:00:00
1429032   1992-01-02 23:00:00
1429033   1992-01-02 22:00:00
1429034   1992-01-02 21:00:00
1429035   1992-01-02 20:00:00
1429036   1992-01-02 19:00:00
1429037   1992-01-02 18:00:00
1429038   1992-01-02 17:00:00
1429039   1992-01-02 16:00:00
1429040   1992-01-02 15:00:00
1429041   1992-01-02 14:00:00
1429042   1992-01-02 13:00:00
1429043   1992-01-02 12:00:00
1429044   1992-01-02 11:00:00

I just need to convert each row to day of year. So the result in a new df would look like:

df_doy:

index     day_of_year
1429029   3
1429030   3
1429031   3
1429032   2
1429033   2
1429034   2
1429035   2
1429036   2
1429037   2
1429038   2
1429039   2
1429040   2
1429041   2
1429042   2
1429043   2
1429044   2

thank you,


Solution

  • We have

    df['day_of_year'] = pd.to_datetime(df[col]).dt.dayofyear
    

    Or just output the day

    df['day_of_year'] = pd.to_datetime(df[1]).dt.day