Time series usually comes with a fixed frequency, for example, every microsecond, every second, every minute, and so on. These frequencies can be changed from one to another.
We can use the asfreq function to change frequencies, as shown in the following snippet:
In [131]: # Frequency conversion using asfreq ibmTS.asfreq('BM') Out[131]: 1959-06-30 448 1959-07-31 428 1959-08-31 425 1959-09-30 411 1959-10-30 411 1959-11-30 428 1959-12-31 439 1960-01-29 418 1960-02-29 419 1960-03-31 445 1960-04-29 453 1960-05-31 504 1960-06-30 522 Freq: BM, Name: closingPrice, dtype: float64
In this case, we just obtain the values corresponding to the last day of the month from the ibmTS time series. Here, bm stands for business month end frequency. For a list of all possible frequency aliases, go to http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases.
If we specify a frequency that is smaller than the granularity of the data, the gaps will be filled in with NaN values:
In [132]: ibmTS.asfreq('H') Out[132]: 1959-06-29 00:00:00 445 1959-06-29 01:00:00 NaN 1959-06-29 02:00:00 NaN 1959-06-29 03:00:00 NaN ... 1960-06-29 23:00:00 NaN 1960-06-30 00:00:00 522 Freq: H, Name: closingPrice, Length: 8809
We can also apply the asfreq method to the Period and PeriodIndex objects, similar to how we do it for the datetime and Timestamp objects. Period and PeriodIndex are introduced later and are used to represent time intervals.
The asfreq method accepts a method argument that allows you to forward fill (ffill) or back fill the gaps, similar to fillna:
In [140]: ibmTS.asfreq('H', method='ffill') Out[140]: 1959-06-29 00:00:00 445 1959-06-29 01:00:00 445 1959-06-29 02:00:00 445 1959-06-29 03:00:00 445 ... 1960-06-29 23:00:00 522 1960-06-30 00:00:00 522 Freq: H, Name: closingPrice, Length: 8809