1

I have a dataframe with the following information:

    ticker       date      close       gap
0      BHP 1981-07-31   0.945416 -0.199458
1      BHP 1981-08-31   0.919463 -0.235930
2      BHP 1981-09-30   0.760040 -0.434985
3      BHP 1981-10-30   0.711842 -0.509136
4      BHP 1981-11-30   0.778578 -0.428161
..     ...        ...        ...       ...
460    BHP 2019-11-29  38.230000  0.472563
461    BHP 2019-12-31  38.920000  0.463312
462    BHP 2020-01-31  39.400000  0.459691
463    BHP 2020-02-28  33.600000  0.627567
464    BHP 2020-03-31  28.980000  0.784124

I developed the following code to find where the rows are when it crosses 0:

zero_crossings =np.where(np.diff(np.sign(BHP_data['gap'])))[0]

This returns:

array([ 52,  54,  57,  75,  79,  86,  93, 194, 220, 221, 234, 235, 236,
       238, 245, 248, 277, 379, 381, 382, 383, 391, 392, 393, 395, 396],
      dtype=int64)

I need to be able to do the following:

  • calculate the number of months between points where 'gap' crosses 0
  • remove items where the number of months is <12
  • average the remaining months

However, I don't know how to turn this nd.array into something useful that I can make the calculations from. When I try:

pd.DataFrame(zero_crossings)

I get the following df, which only returns the index:

      0
0    52
1    54
2    57
3    75
4    79
5    86
..   ..

Please help...

1 Answer 1

2

Just extended your code a bit to get the zero_crossings into the original dataframe as required.

import pandas as pd
import numpy as np

BHP_data = pd.DataFrame({'gap': [-0.199458, 0.472563, 0.463312, 0.493318, -0.509136, 0.534985, 0.784124]})
BHP_data['zero_crossings'] = 0

zero_crossings = np.where(np.diff(np.sign(BHP_data['gap'])))[0]
print(zero_crossings) # [0 3 4]

# Updates the column to 1 based on the 0 crossing
BHP_data.loc[zero_crossings, 'zero_crossings'] = 1

print(BHP_data)

Output

        gap  zero_crossings
0 -0.199458               1
1  0.472563               0
2  0.463312               0
3  0.493318               1
4 -0.509136               1
5  0.534985               0
6  0.784124               0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.