A summary of Time Series-related objects

The following table gives a summary of Time Series-related objects:

Object

Summary

datetime.datetime

This is a Standard Python datetime class

Timestamp

This is a pandas class derived from datetime.datetime

DatetimeIndex

This is a pandas class and is implemented as an immutable numpy.ndarray of the Timestamp/datetime objects

Period

This is a pandas class representing a time period

PeriodIndex

This is a pandas class and is implemented as an immutable numpy.ndarray of Period objects

timedelta

This is a Python class expressing the difference between two datetime.datetime instances. It is implemented as datetime.timedelta

relativedelta

Implemented as dateutil.relativedelta. dateutil is an extension to the standard Python datetime module. It provides extra functionality such as timedeltas that are expressed in units larger than 1 day.

DateOffset

This is a pandas class representing a regular frequency increment. It has similar functionalty to dateutil.relativedelta.

Plotting using matplotlib

This section provides a brief introduction to plotting in pandas using matplotlib. The matplotlib api is imported using the standard convention, as shown in the following command:

In [1]: import matplotlib.pyplot as plt

Series and DataFrame have a plot method, which is simply a wrapper around plt.plot. Here, we will examine how we can do a simple plot of a sine and cosine function. Suppose we wished to plot the following functions over the interval pi to pi:

  • f(x) = cos(x) + sin (x)
  • g(x) = cos (x) - sin (x)

This gives the following interval:

In [51]: import numpy as np
In [52]: X = np.linspace(-np.pi, np.pi, 256,endpoint=True)

In [54]: f,g = np.cos(X)+np.sin(X), np.sin(X)-np.cos(X)
In [61]: f_ser=pd.Series(f)
         g_ser=pd.Series(g)


In [31]: plotDF=pd.concat([f_ser,g_ser],axis=1)
         plotDF.index=X
         plotDF.columns=['sin(x)+cos(x)','sin(x)-cos(x)']
         plotDF.head()
Out[31]:  sin(x)+cos(x)  sin(x)-cos(x)
-3.141593  -1.000000   1.000000
-3.116953  -1.024334   0.975059
-3.092313  -1.048046   0.949526
-3.067673  -1.071122   0.923417
-3.043033  -1.093547   0.896747
5 rows × 2 columns

We can now plot the DataFrame using the plot() command and the plt.show() command to display it:

In [94]: plotDF.plot()
         plt.show()

We can apply a title to the plot as follows:
In [95]: plotDF.columns=['f(x)','g(x)']
         plotDF.plot(title='Plot of f(x)=sin(x)+cos(x), 
 g(x)=sinx(x)-cos(x)')
         plt.show()

The following is the output of the preceding command:

Plotting using matplotlib

We can also plot the two series (functions) separately in different subplots using the following command:

In [96]: plotDF.plot(subplots=True, figsize=(6,6))
         plt.show()

The following is the output of the preceding command:

Plotting using matplotlib

There is a lot more to using the plotting functionality of matplotlib within pandas. For more information, take a look at the documentation at http://pandas.pydata.org/pandas-docs/dev/visualization.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset