Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
sgn function

I've two series.

I want to multiply them together (easy), but I want to make the answer negative depending on the sign of the value in one of the series.

ie x = a * b * sgn (b)

What's the technique for doing this?

N

1 response

Maybe something along these lines would get you closer...

> python  
Python 2.7.10 (default, Nov  7 2015, 11:34:05)  
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import pandas as pd  
>>> import numpy as np  
>>> li1=[-2,+3,-4,+5]  
>>> li2=[+3,+4,-5,-6]  
>>> se1=pd.Series(li1)  
>>> se2=pd.Series(li2)  
>>> se3=np.abs(se1)*np.abs(se2)*np.copysign(1,se1)  
>>> se3  
0    -6  
1    12  
2   -20  
3    30