Daru’s (Data Analysis in RUby) latest release (0.2.0) brings in a host of new features, most important among them being time series manipulation functionality. In this post, we will go over the date offsets that daru offers, which can be used for creating date indexes of specific intervals. The offsets offer a host of options for easy creation of different intervals and even work with standalone DateTime objects to increase or decrease time.
Offset classes and behaviour
The date offsets are contained in the
Daru::Offsets sub-module. A number of classes are offered, each of which implements business logic for advancing or retracting date times by a specific interval.
To demonstrate with a quick example:
1 2 3 4 5
As you can see in the above example, an hour was added to the time specified by DateTime and returned. All the offset classes work in a similar manner. Following offset classes are available to users:
|Daru::DateOffset||Generic offset class|
|Week||One Week. Can be anchored on any week of the day.|
|MonthBegin||Calendar Month Begin.|
|MonthEnd||Calendar Month End.|
|YearBegin||Calendar Year Begin.|
|YearEnd||Calendar Year End.|
The generic Daru::DateOffset class is used for creating a generic offset by passing the number of intervals you want as the value for a key that describes the type of interval. For example to create an offset of 3 days, you pass the option
days: 3 into the Daru::Offset constructor.
1 2 3 4 5
On a similar note, the DateOffset class constructor can accept the options
:years. Optionally, specifying the
:n option will tell DateOffset to apply a particular offset more than once. To elaborate:
1 2 3 4 5
The specialized offset classes like MonthBegin, YearEnd, etc. all reside inside the
Daru::Offsets namespace and can be used by simply calling
.new on them. All accept an optional Integer argument that works like the
:n option for Daru::DateOffset, i.e it applies the offset multiple times.
To elaborate, consider the YearEnd offset. This offsets the date to the nearest year end after itself:
1 2 3 4 5 6 7 8 9 10 11
Of special note is the
Week offset. This offset can be ‘anchored’ to any week of the day that you specify. When this is done, the DateTime that is being offset will be offset to that day of the week.
For example, to anchor the Week offset to a Wednesday, pass ‘3’ as a value to the
1 2 3 4 5 6 7 8 9
Likewise, the Week offset can be anchored on any day of the week, by simplying specifying the
:weekday option. Indexing for days of the week starts from 0 for Sunday and goes on 6 for Saturday.
Offset string aliases
The most obvious use of date offsets is for creating
DateTimeIndex objects with a fixed time interval between each date index. To make creation of indexes easy, each of the offset classes have been linked to certain string alaises, which can directly passed to the DateTimeIndex class.
For example, to create a DateTimeIndex of 100 periods with a frequency of 1 hour between each period:
1 2 3 4 5
Likewise all of the above listed offsets can be aliased using strings, which can be used for specifying the offset in a DateTimeIndex index. The string aliases of each offset class are as follows:
|Alias String||Offset Class / Description|
|‘W’||Default Week. Anchored on SUN.|
|‘W-SUN’||Week anchored on sunday|
|‘W-MON’||Week anchored on monday|
|‘W-TUE’||Week anchored on tuesday|
|‘W-WED’||Week anchored on wednesday|
|‘W-THU’||Week anchored on thursday|
|‘W-FRI’||Week anchored on friday|
|‘W-SAT’||Week anchored on saturday|
See this notebook on daru’s time series functions in order to get a good overview of daru’s time series manipulation functionality.