Swaggy A Swaggy A - 2 months ago 16
Python Question

Convert DateTime to sequential day of the year

I have a dataframe that contains a series of dates, e.g.:

0 2014-06-17
1 2014-05-05
2 2014-01-07
3 2014-06-29
4 2014-03-15
5 2014-06-06
7 2014-01-29


Now, I need a way to convert these dates to the sequential day of the year, e.g.

0 168
1 125
2 7
3 180
4 74
5 157
7 29


All the values are within the same year.
The opposite problem has been asked many times - converting the sequential day of the year to a date, but I need to do the opposite. Is there an easy way to do this with Pandas? Thanks!

EDIT: Answered by piRSquared. Thank you!

Answer

Option 1
time-date-components
Link from @root
Use dt.dayofyear

df.iloc[:, 0].dt.dayofyear

0    168
1    125
2      7
3    180
4     74
5    157
7     29
Name: 1, dtype: int64

Option 2
strftime.org
Use dt.strftime('%-j')

df.iloc[:, 0].dt.strftime('%-j')


0    168
1    125
2      7
3    180
4     74
5    157
7     29
Name: 1, dtype: object
Comments