Other operations

Arithmetic and statistical operations can be applied, just like for a NumPy array. Such operations take place in a vectorized mode in a Series, just as in NumPy arrays, and do not require to be looped through:

# Mean of entire series
In [34]: np.mean(stockPriceSeries)
Out[34]: 437.27200000000005

# Standard deviation of entire series
In [35]: np.std(stockPriceSeries)
Out[35]: 417.4446361087899

Elementwise operations can also be performed on a Series:

In [36]: ser
Out[36]:
0 3.063921
1 0.097450
2 -1.660367
3 -1.221308
4 -0.948873
5 0.454462
6 0.586824
dtype: float64

In [37]: ser * ser
Out[37]:
0 9.387611
1 0.009496
2 2.756819
3 1.491593
4 0.900359
5 0.206535
6 0.344362
dtype: float64

An important feature of a Series is that data is automatically aligned based on the label:

In [40]: ser[1:]
Out[40]:
1 0.097450
2 -1.660367
3 -1.221308
4 -0.948873
5 0.454462
6 0.586824
dtype: float64
In [41]: ser[1:] + ser[:-2]
Out[41]:
0 NaN
1 0.194899
2 -3.320734
3 -2.442616
4 -1.897745
5 NaN
6 NaN
dtype: float64

Thus, we can see that for non-matching labels, NaN is inserted. The default behavior is that the union of the indexes is produced for unaligned Series structures. This is preferable as information is preserved rather than lost. We will handle missing values in pandas in a later chapter of the book.

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

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