friendlysam.parts.Storage.step_time

Storage.step_time(index, num_steps)

A function for stepping forward or backward in time.

A Part (or subclass) instance may use any logic for stepping in time. To change time stepping, you may have to change time_step or override step_time().

Parameters:
  • index (any object) – The index to step from.
  • num_steps (int) – The number of steps to take.
Returns:

the new time, num_steps away from index.

Examples

If your model is indexed in evenly spaced integers, the default implementation is enough. A step is taken as follows:

def step_time(self, index, num_steps):
    return index + self.time_unit * num_steps

The default time_unit is 1, so time stepping is done by adding an integer.

>>> part = Part()
>>> part.step_time(3, -2)
1
>>> part.time_unit = 10
>>> part.step_time(3, 2)
23

Let’s assume your model is indexed with pandas.Timestamp in 2-hour increments, then it’s still sufficient to change the time unit:

>>> from pandas import Timestamp, Timedelta
>>> part = Part()
>>> part.time_unit = Timedelta('2h')
>>> part.step_time(Timestamp('2015-06-10 16:00'), 1)
Timestamp('2015-06-10 18:00:00')
>>> part.step_time(Timestamp('2015-06-10 16:00'), -3)
Timestamp('2015-06-10 10:00:00')

If your model is indexed with something more complicated, you may have to change the step_time() method. For example, assume a model is indexed with two-element tuples where the first element is an integer representing time. Then override the step_time() as follows:

>>> def my_step_time(index, step):
...     t, other = index
...     t += step
...     return (t, other)
...
>>> part = Part()
>>> part.step_time = my_step_time
>>> part.step_time((1, 'ABC'), 2)
(3, 'ABC')