Table Of Contents

Previous topic

Date objects

Next topic

scikits.timeseries.DateArray.base

This Page

DateArray objects

class DateArray(dates=None, freq=None, copy=None)

Based on numpy.ndarray.

Defines a ndarray of dates, as ordinals. The dates parameter must then be a sequence of Date objects or a sequence of integers. The freq parameter must be a valid frequency specification, as listed in the Frequency constants section.

When viewed globally (array-wise), a DateArray object behaves as an ndarray of integers. When viewed element-wise, DateArray behaves as a standard sequence of Date objects.

Thus, a test such as:

>>> DateArray(...) == value

is valid only if value is an integer, not a Date object. In that case, the test returns a boolean ndarray that has the same shape as the DateArray object.

However, a loop such as :

>>> for d in DateArray(...):
accesses the array element by element, and at each iteration, d is a
Date object.

The only requirement for the Date elements of a DateArray is that they must have the same frequency. Otherwise, they do not have to be in chronological order nor to be regularly spaced, and duplicated entries are also permitted.

Construction

A DateArray object can be created simply by calling the class with the required inputs. However, the recommended method is to use the date_array factory function.

date_array(dlist=None, start_date=None, end_date=None, length=None, freq=None, autosort=False)

Factory function for constructing a DateArray.

Parameters:

dlist : {sequence, DateArray}, optional

If not None, dlist must be any of these possibilities:

  • an existing DateArray object;
  • a sequence of Date objects with the same frequency;
  • a sequence of datetime.datetime objects;
  • a sequence of dates in string format;
  • a sequence of integers corresponding to the representation of Date objects.

In any of the last four possibilities, the freq parameter must also be given.

start_date : {var}, optional

First date of a continuous DateArray. This parameter is used only if dlist is None. In that case, one of the end_date or the length parameters must be given.

end_date : {var}, optional

Last date of the output. Use this parameter in combination with start_date to create a continuous DateArray.

length : {int}, optional

Length of the output. Use this parameter in combination with start_date to create a continuous DateArray.

autosort : {True, False}, optional

Whether the input dates must be sorted in chronological order.

Returns:

output : DateArray object.

Notes

  • When the input is a list of dates, the dates are not sorted. Use autosort = True to sort the dates by chronological order.
  • If start_date is a Date object and freq is None, the frequency of the output is start_date.freq.

Manipulating DateArray objects

Accessing elements

As subclasses of ndarray, DateArray objects follow the same rules for accessing elements through indexing. In addition, DateArray objects can be indexed with one or several Date objects.

When a single element of a DateArray is accessed, the result is a Date object with the same frequency as the input. Otherwise, the result is a DateArray with the same frequency as the input. Note that when using a slice to access specific elements of a DateArray, the result is always a DateArray.

>>> dates = ts.date_array(start_date=ts.Date('M', '2001-01'), length=36)
>>> # Accessing a single element with an integer
>>> dates[0]
<M : Jan-2001>
>>> # Accessing a single element with a Date object
>>> dates[ts.Date('M', '2002-01')]
<M : Jan-2002>
>>> # Using a slice to access a single element : the result is a DateArray
>>> dates[-1:]
DateArray([Dec-2003],
          freq='M')
>>> # Accessing multiple elements with a list of integers
>>> dates[[0, 12, 24]]
DateArray([Jan-2001, Jan-2002, Jan-2003],
          freq='M')
>>> # Accessing multiple elements with a list of Date objects
>>> dates[[ts.Date('M', '2002-01'), ts.Date('M', '2003-01')]]
DateArray([Jan-2002, Jan-2003],
          freq='M')

Operations

Arithmetic operations

Arithmetic operations on DateArray objects are limited to additions and subtractions. Any other arithmetic operation will raise a ArithmeticDateError exception.

Adding (subtracting) a scalar or a sequence of scalars to (from) a DateArray returns a DateArray with the same frequency. The shapes of the inputs must be compatible, as described in the broadcasting section of the Numpy documentation.

Adding (subtracting) two DateArray or a DateArray and a Date is possible only if the two objects have the same frequency. The result is then a ndarray. If the inputs do not share the same frequency, a FrequencyDateError exception is raised.

Comparison operations

All the comparison operations involving the standard Python operators (==, <, >, <=, >=, !=) or their method equivalents are supported between a DateArray and:

  1. a scalar,
  2. a sequence (or array) of scalars,
  3. a Date object or
  4. another DateArray.

In the last two cases, the objects must have the same frequency, or a FrequencyDateError is raised. If the other input is a sequence, its shape must be compatible with the shape of the DateArray.

In every case, the result is then an array of booleans, with the same shape as the input.

In addition, it is possible to check whether a single Date object or its equivalent integer value is contained in a DateArray with the in operator or its method equivalent DateArray.__contains__.

Attributes

Because DateArray objects are subclasses of ndarray, they inherit all its attributes, as describe in the Numpy documentation.

In addition, they have the specific following attributes.

Frequency information

DateArray.freq
Returns the frequency of the series, as an integer. This attribute is read-only.
DateArray.freqstr
Returns the frequency of the series, as a string. This attribute is read-only.

Date information

The following attributes give some information about the dates. They are read-only.

Name Description Range
Integer ndarrays, with the same size as the instance.
DateArray.year
DateArray.years
Year ...
DateArray.qyear
Quarter Year (1) ...
DateArray.quarter
DateArray.quarters
Quarter [1-4]
DateArray.month
DateArray.months
Month [1-12]
DateArray.week
DateArray.weeks
Week [1-53]
DateArray.day
DateArray.days
Day of the month [1-31]
DateArray.day_of_week
DateArray.weekdays
Day of the week, starting Monday [0-6]
DateArray.day_of_year
DateArray.yeardays
Day of the year, starting Jan-01 [1-366]
DateArray.hour
DateArray.hours
Hour [00-23]
DateArray.minute
DateArray.minutes
Minute [00-59]
DateArray.second
DateArray.seconds
Seconds [00-59]
Single Date objects, with the same frequency as the instance
DateArray.start_date
First date of the series (in chronological order).  
DateArray.end_date
Last date of the series (in chronological order).  

Private Attributes

Warning

The following attributes are private and are not meant to be used directly by a user. They are reported here for the sake of completeness.

_cachedinfo

The _cachedinfo attribute is a directory storing some information about the instance. It has the following keys:

'chronidx' : {None, ndarray}
If not None, a ndarray of integers corresponding to the indices sorting the instance in chronological order. If the DateArray is already sorted chronologically, then _cachedinfo[‘chronidx’] = np.array([], dtype=int)
'full' : {True, False}
Whether there are no missing dates in the series.
'hasdups' : {True, False}
Whether there are duplicating dates in the series.
'ischrono' : {True, False}
Whether the instance is sorted in chronological order.
'steps' : {ndarray}
Time steps between consecutive dates (in chronological order).

'toobj' : {None, ndarray}

'toord' : {None, ndarray}
ndarray of integers corresponding to the Gregorian proleptic dates.
'tostr' : {None, ndarray}
ndarray of strings corresponding to the string representations of the dates.

Methods

Because DateArray objects are subclasses of ndarray, they inherit their methods, as described in the Numpy documentation.

In addition, they accept the following specific methods.

Information methods

The following methods give some information about the distribution of dates. They do not need any argument.

DateArray.get_steps()
Returns the time steps between consecutive dates, in the same unit as the instance frequency.
DateArray.has_missing_dates()
Returns whether the instance has missing dates.
DateArray.has_duplicated_dates()
Returns whether the instance has duplicated dates.
DateArray.is_full()
Returns whether the instance has no missing dates.
DateArray.is_valid()
Returns whether the instance is valid (that there are no missing nor duplicated dates).
Date.is_chronological()
Returns whether the instance is sorted in chronological order.

Indexing methods

These methods allow the user to access specific elements of a DateArray by directly giving a date instead of an integer.

DateArray.date_to_index
DateArray.find_dates

Conversion methods

... to another frequency

DateArray.asfreq