15. Operators

Series, Frame, and Index, as well as their derived classes, provide support for the full range of operators available with NumPy. In addition, Series and Frame feature index-alignment and automatic index expansion when both operands are StaticFrame objects.

15.1. Index

Index operators operate on the Index labels. In all cases, an immutable NumPy array is returned rather than a new Index instance.

15.1.1. Unary Operators

Index.__abs__()

Same as abs(a).

Index.__invert__()

Same as ~a.

Index.__neg__()

Same as -a.

Index.__pos__()

Same as +a.

15.1.2. Binary Operators

Index.__add__(other)

Same as a + b.

Index.__and__(other)

Same as a & b.

Index.__eq__(other)

Same as a == b.

Index.__floordiv__(other)

Same as a // b.

Index.__ge__(other)

Same as a >= b.

Index.__gt__(other)

Same as a > b.

Index.__le__(other)

Same as a <= b.

Index.__lshift__(other)

Same as a << b.

Index.__lt__(other)

Same as a < b.

Index.__matmul__(other)

Same as a @ b.

Index.__mod__(other)

Same as a % b.

Index.__mul__(other)

Same as a * b.

Index.__ne__(other)

Same as a != b.

Index.__or__(other)

Same as a | b.

Index.__pow__(other)

Same as a ** b.

Index.__rshift__(other)

Same as a >> b.

Index.__sub__(other)

Same as a - b.

Index.__truediv__(other)

Same as a / b.

Index.__xor__(other)

Same as a ^ b.

15.2. Series

Series operators operate on the Series values. In all cases, a new Series is returned. Operations on two Series always return a new Series with a union Index.

15.2.1. Unary Operators

Series.__abs__()

Same as abs(a).

Series.__invert__()

Same as ~a.

Series.__neg__()

Same as -a.

Series.__pos__()

Same as +a.

15.2.2. Binary Operators

Series.__add__(other)

Same as a + b.

Series.__and__(other)

Same as a & b.

Series.__eq__(other)

Same as a == b.

Series.__floordiv__(other)

Same as a // b.

Series.__ge__(other)

Same as a >= b.

Series.__gt__(other)

Same as a > b.

Series.__le__(other)

Same as a <= b.

Series.__lshift__(other)

Same as a << b.

Series.__lt__(other)

Same as a < b.

Series.__matmul__(other)

Same as a @ b.

Series.__mod__(other)

Same as a % b.

Series.__mul__(other)

Same as a * b.

Series.__ne__(other)

Same as a != b.

Series.__or__(other)

Same as a | b.

Series.__pow__(other)

Same as a ** b.

Series.__rshift__(other)

Same as a >> b.

Series.__sub__(other)

Same as a - b.

Series.__truediv__(other)

Same as a / b.

Series.__xor__(other)

Same as a ^ b.

15.2.3. Examples

>>> s = sf.Series.from_items((('Venus', 108.2), ('Earth', 149.6), ('Saturn', 1433.5)))
>>> s
<Series>
<Index>
Venus    108.2
Earth    149.6
Saturn   1433.5
<<U6>    <float64>

>>> abs(s - s['Earth'])
<Series>
<Index>
Venus    41.39999999999999
Earth    0.0
Saturn   1283.9
<<U6>    <float64>

>>> s > s['Earth']
<Series>
<Index>
Venus    False
Earth    False
Saturn   True
<<U6>    <bool>

>>> s / s['Earth']
<Series>
<Index>
Venus    0.7232620320855615
Earth    1.0
Saturn   9.582219251336898
<<U6>    <float64>

>>> s1 = sf.Series((1, 2), index=('Earth', 'Mars'))
>>> s2 = sf.Series((2, 0), index=('Mars', 'Mercury'))
>>> s1 * s2
<Series>
<Index>
Earth    nan
Mars     4.0
Mercury  nan
<<U7>    <float64>

>>> s1 == s2
<Series>
<Index>
Earth    False
Mars     True
Mercury  False
<<U7>    <bool>

15.3. Frame

Frame operators operate on the Frame values. In all cases, a new Frame is returned.

15.3.1. Unary Operators

Frame.__abs__()

Same as abs(a).

Frame.__invert__()

Same as ~a.

Frame.__neg__()

Same as -a.

Frame.__pos__()

Same as +a.

15.3.2. Binary Operators

Frame.__add__(other)

Same as a + b.

Frame.__and__(other)

Same as a & b.

Frame.__eq__(other)

Same as a == b.

Frame.__floordiv__(other)

Same as a // b.

Frame.__ge__(other)

Same as a >= b.

Frame.__gt__(other)

Same as a > b.

Frame.__le__(other)

Same as a <= b.

Frame.__lshift__(other)

Same as a << b.

Frame.__lt__(other)

Same as a < b.

Frame.__matmul__(other)

Same as a @ b.

Frame.__mod__(other)

Same as a % b.

Frame.__mul__(other)

Same as a * b.

Frame.__ne__(other)

Same as a != b.

Frame.__or__(other)

Same as a | b.

Frame.__pow__(other)

Same as a ** b.

Frame.__rshift__(other)

Same as a >> b.

Frame.__sub__(other)

Same as a - b.

Frame.__truediv__(other)

Same as a / b.

Frame.__xor__(other)

Same as a ^ b.

Deviations from Pandas

For consistency in operator application and to insure index alignment, all operators return an union index when both opperrands are StaticFrame containers. This deviates from Pandas, where in some versions equality operators did not align on a union index, and behaved differently than other operators.

15.3.3. Examples

>>> f = sf.Frame.from_dict(dict(diameter=(12756, 6792, 142984), mass=(5.97, 0.642, 1898)), index=('Earth', 'Mars', 'Jupiter'), dtypes=dict(diameter=np.int64))
>>> f
<Frame>
<Index> diameter mass      <<U8>
<Index>
Earth   12756    5.97
Mars    6792     0.642
Jupiter 142984   1898.0
<<U7>   <int64>  <float64>

>>> f / f.loc['Earth']
<Frame>
<Index> diameter           mass                <<U8>
<Index>
Earth   1.0                1.0
Mars    0.5324553151458138 0.10753768844221107
Jupiter 11.209156475384132 317.92294807370183
<<U7>   <float64>          <float64>