python - How to check if the signs of a Series conform to a given string of signs? -
python - How to check if the signs of a Series conform to a given string of signs? -
for illustration have series below,
ts = pd.series([-1,-2.4,5,6,7, -4, -8])
i know if there pythonic way check signs of ts
against list of signs, such as,
sign = '++++---' # returns false
while
sign = '--+++--' # returns true
this solution requires numpy functions, since using pandas info series, not issue you.
import pandas pd import numpy np values = pd.series([-1, -2.4, 5, 6, 7, -4, -8, 0]) sign_str = "--+++--0" sign_map = { "+" : 1, "0" : 0, "-" : -1 } expected_signs = list(map(sign_map.get, sign_str)) observed_signs = np.sign(values) np.all(expected_signs == observed_signs)
python pandas series
Comments
Post a Comment