0

I am dealing with hourly data in which the date is stored in 4 different arrays, one for day, month, year, and hour. I am running a for-loop to rather store these dates as strings with this format: '01/01/1946 0'

My code looks something like this:

import numpy as np
from datetime import datetime as dt
import matplotlib.dates as mdates

for nn in range(nnn):
    y1 = int(yr[nn])
    m1 = int(mon[nn])
    d1 = int(day[nn])
    h1 = int(hr[nn])
    
    #In the the last string we are specifying the format
    datenow = dt.strptime(str(m1)+'/'+str(d1)+'/'+str(y1) + ' '+ str(h1) , '%m/%d/%Y %H').date()    
    ndaten = datenow.toordinal()
    
    allnewdate[nn] = ndaten

When I check allnewdate with mdates.num2str(allnewdates) it appears that all hours are defined as 0 at each point instead of parsing through the 23 hours of the day.

What might be wrong?

Madlad
  • 125
  • 12
  • The format you provided to `strptime` expects to see the individual fields seperated by dashes (`-`) and then a space that seperates the year from the hours, but the string you are parsing is seperated by forward slashes (`/`). – Paul M. Jun 20 '20 at 18:54
  • Why use strptime at all? You could simply use the integers to create the datetime object – FObersteiner Jun 20 '20 at 18:59
  • Thanks Paul, that was actually an error from typing it here. – Madlad Jun 20 '20 at 19:07
  • I was under the impression of using strptime to then obtain the date numbers which I will need for another analysis. – Madlad Jun 20 '20 at 19:12
  • maybe your actual problem is that the ordinal only represents date, not date and time, see e.g. [here](https://stackoverflow.com/questions/41029529/convert-datetime-to-ordinal-in-python-fails)? – FObersteiner Jun 20 '20 at 20:27

0 Answers0