Modular arithmetic in python to iterate a pandas dataframe
<p>Ok, I have a big dataframe such as:</p>

<pre><code>      hour    value
  0      0      1
  1      6      2
  2     12      3
  3     18      4
  4      0      5
  5      6      6
  6     12      7
  7     18      8
  8      6      9
  9     12     10
 10     18     11
 11     12     12
 12     18     13
 13      0     14
</code></pre>

<p>Let's don't get lost here. The column <code>hour</code> represents the hours of the day, from 6 to 6 hours. Column <code>values</code> is well, exactly that, here the values are as an example, not the actual ones.</p>

<p>If you look closely to the <code>hour</code> column, you can see that there are hours missing. For instance, there is a gap between rows 7 and 8 (the value of hour 0 is missing). There are also bigger gaps, such as in  between rows 10 and 11 (hours 00 and 06).</p>

<p><strong>What do I need?</strong> I would like to check when an hour (and of course) a value is missing, and complete the dataframe inserting a row there with the corresponding hour and a <code>np.nan</code> as value.</p>

<p><strong>What have I thought?</strong> I think this would be easily solved using <em>modular arithmetic</em>, in this case with mod 24, such as when <code>18 + 6 = 24 = 0 mod 24</code>. So initializing the counter to zero and adding 6 <em>with the caveat that the counter is defined in modular arithmetic mod 24</em> you can verify if each <code>hour</code> is the corresponding hour, and if not, insert a new row with the corresponding hour and with <code>np.nan</code> as value.</p>

<p>I don't know how to do the implementation of modular arithmetic in python to iterate a dataframe column.</p>

<p>Thank you very much.</p>