-1

I have an integer representing the day of the year from 1-365 in my array out at index 0. How do i convert this to get the month and day of month from this using Python and Pandas?

I have tried:

out[0] = pd.to_datetime(out[0]).dt.strftime('%m-%d')

but I get an error saying AttributeError: 'Timestamp' object has no attribute 'dt'

  • 2
    so what's the year? think leap years... – FObersteiner Jan 13 '21 at 15:36
  • 1
    Does this answer your question? [Python Question: Year and Day of Year to date?](https://stackoverflow.com/questions/2427555/python-question-year-and-day-of-year-to-date) – buran Jan 13 '21 at 15:50
  • You have to add an starting year/date and delete the .dt for your timestamp. My solution would be this ```pd.to_datetime(out[0]-1 ,unit='D',origin=pd.Timestamp('2021-01-01')).strftime('%m-%d')``` – campy Jan 13 '21 at 16:10
  • cheers @campy your solution worked perfectly! – mynameJeff55 Jan 13 '21 at 17:41

1 Answers1

0

You have to know if it's a leap year or not? If it's a leap year, February has 29 days, if not 28. Otherwise your conversion will not be correct.

Leap Years are any year that can be exactly divided by 4 (such as 2016, 2020, 2024, etc) except if it can be exactly divided by 100, then it isn't (such as 2100, 2200, etc) except if it can be exactly divided by 400, then it is (such as 2000, 2400)

TkrA
  • 439
  • 1
  • 5
  • 19