1

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,

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming dfdates columns are ["index", "date"], you can use dt.dayofyear this way :

df_doy = dfdates.assign(day_of_year = pd.to_datetime(dfdates.pop("date")).dt.dayofyear)

Output :

print(df_doy)

      index  day_of_year
0   1429029            3
1   1429030            3
2   1429031            3
3   1429032            2
4   1429033            2
..      ...          ...
11  1429040            2
12  1429041            2
13  1429042            2
14  1429043            2
15  1429044            2

[16 rows x 2 columns]

Comments

0

Looks like there is a day_of_year variable in Period.

https://pandas.pydata.org/docs/reference/api/pandas.Period.dayofyear.html

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.