Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The constructor for <a href="http://docs.python.org/2/library/datetime.html#datetime.datetime" rel="nofollow"><code>datetime</code></a> objects takes a year, month, and day, and optional hour, minute, etc. So, you're passing the day as the month, the hour as the day, and nothing as the hour.</p> <p>On top of that, <code>day</code> has to be a day of the month, not a day of the year, and <code>hour</code> has to be an integer (with separate minutes, seconds, and microseconds if appropriate).</p> <p>One easy way around this is to create a <code>datetime</code> with the start of the year, and add the days and hours on with <code>timedelta</code>.</p> <p>While we're at it, you can make the code a bit cleaner by iterating directly over the arrays instead of over <code>range(len(…))</code>.</p> <pre><code>for y, d, h in zip(year, day, hour): d0 = datetime.datetime(y, 1, 1, tzdata=pytz.UTC) dt.append(d0 + datetime.timedelta(days=d, hours=h)) </code></pre> <hr> <p>As a side note, I'd name the list variables <code>years</code>, <code>days</code>, <code>hours</code>, and <code>dts</code>, so I could name the individual iteration values <code>year</code>, <code>day</code>, <code>hour</code>, and <code>dt</code>. And, while we're at it, I'd write this as a helper function and a list comprehension rather than a <code>for</code> loop and a complicated body. And put the imports at the time. Like this:</p> <pre><code>import datetime import pytz years = [2012,2012,2012] days = [2,3,4] #day of year hours = [12,12,12] def make_date(year, day, hour, tzdata=pytz.UTC): dt = datetime.datetime(year, 1, 1, tzdata=tzdata) return dt + datetime.timedelta(days=day, hours=hour) dts = [make_date(year, day, hour) for year, day, hour in zip(years, days, hours)] </code></pre> <hr> <p>The above works for your original question, where <code>years</code> and <code>days</code> are lists of integers and <code>hours</code> is a list of floats (as in your description) or integers (as in your sample code). It will also work if <code>days</code> is a list of floats, but not <code>years</code>.</p> <p>However, from a comment, it sounds like these are actually numpy arrays of <code>int64</code> values. Although you call them floats, <code>int64</code> is not a floating-point type, it's an integer type. And you can convert an <code>int64</code> to a plain Python <code>int</code> with no loss of range or precision.</p> <p>So, because the <code>datetime</code> and <code>timedelta</code> constructors won't accept <code>int64</code> values, just convert each one into an <code>int</code>:</p> <pre><code>def make_date(year, day, hour, tzdata=pytz.UTC): dt = datetime.datetime(int(year), 1, 1, tzdata=tzdata) return dt + datetime.timedelta(days=int(day), hours=int(hour)) </code></pre> <p>If your hours were actually of a floating-point type as you originally claimed, like <code>float64</code>, you'd want to use <code>float(hour)</code> instead of <code>int(hour)</code>.</p> <hr> <p>One last thing: From your description, I guessed that your days were 0-based (and your hours, but that's not likely to be wrong). If your days are actually 1-based, you're obviously going to need to do <code>days=int(day)-1</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload