Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. import pandas as pd
  2. from pandas_datareader import data, wb
  3. import numpy as np
  4. from datetime import date
  5.  
  6. pd.set_option('expand_frame_repr', False)
  7.  
  8. df = data.DataReader('GE', 'yahoo', date (2000, 1, 1), date (2000, 2, 1))
  9. df['x'] = np.where (df['Open'] > df['High'].shift(-2), 1, np.nan)
  10. print (df.round(2))
  11.  
  12. # this section of code works perfectly for an integer based index.......
  13. ii = df[pd.notnull(df['x'])].index
  14. dd = np.diff(ii)
  15. jj = [ii[i] for i in range(1,len(ii)) if dd[i-1] > 2]
  16. jj = [ii[0]] + jj
  17.  
  18. for ci in jj:
  19. df.loc[ci:ci+2,'x'] = 1.0
  20. # end of section that works perfectly for an integer based index......
  21.  
  22. print (df.round(2))
  23.  
  24. Open High Low Close Volume Adj Close x
  25. Date
  26. 2000-01-03 153.00 153.69 149.19 150.00 22069800 29.68 1.0
  27. 2000-01-04 147.25 148.00 144.00 144.00 22121400 28.49 1.0
  28. 2000-01-05 143.75 147.00 142.56 143.75 27292800 28.44 NaN
  29. 2000-01-06 143.12 146.94 142.63 145.67 19873200 28.82 NaN
  30. 2000-01-07 148.00 151.88 147.00 151.31 20141400 29.94 NaN
  31. 2000-01-10 152.69 154.06 151.12 151.25 15226500 29.93 NaN
  32. 2000-01-11 151.00 152.69 150.62 151.50 15123000 29.98 NaN
  33. 2000-01-12 151.06 153.25 150.56 152.00 18342300 30.08 NaN
  34. 2000-01-13 153.13 154.94 153.00 153.75 14953500 30.42 1.0
  35. 2000-01-14 153.38 154.63 149.56 151.00 18480300 29.88 1.0
  36. 2000-01-18 149.62 149.62 146.75 148.00 18296700 29.29 NaN
  37. 2000-01-19 146.50 150.94 146.25 148.72 14849700 29.43 NaN
  38. 2000-01-20 149.06 149.75 142.63 145.94 30759000 28.88 1.0
  39. 2000-01-21 147.94 148.25 143.94 144.13 24005400 28.52 1.0
  40. 2000-01-24 145.31 145.94 136.44 138.13 27116100 27.33 1.0
  41. 2000-01-25 138.06 140.38 137.00 138.50 25387500 27.41 NaN
  42. 2000-01-26 140.50 142.19 138.88 141.44 15856800 27.99 NaN
  43. 2000-01-27 141.56 141.75 137.06 141.75 19243500 28.05 1.0
  44. 2000-01-28 140.31 140.50 133.63 134.00 29846700 26.52 1.0
  45. 2000-01-31 134.00 135.94 133.06 134.00 21782700 26.52 NaN
  46. 2000-02-01 134.25 137.00 134.00 136.00 27339000 26.91 NaN
  47. Traceback (most recent call last):
  48. File "C:stocksquestion4 for stack overflow.py", line 15, in <module>
  49. jj = [ii[i] for i in range(1,len(ii)) if dd[i-1] > 2]
  50. File "C:stocksquestion4 for stack overflow.py", line 15, in <listcomp>
  51. jj = [ii[i] for i in range(1,len(ii)) if dd[i-1] > 2]
  52. TypeError: Cannot cast ufunc greater input from dtype('<m8[ns]') to dtype('<m8') with casting rule 'same_kind'
  53.  
  54. Open High Low Close Volume Adj Close x
  55. Date
  56. 2000-01-03 153.00 153.69 149.19 150.00 22069800 29.68 1.0
  57. 2000-01-04 147.25 148.00 144.00 144.00 22121400 28.49 1.0
  58. 2000-01-05 143.75 147.00 142.56 143.75 27292800 28.44 1.0
  59. 2000-01-06 143.12 146.94 142.63 145.67 19873200 28.82 NaN
  60. 2000-01-07 148.00 151.88 147.00 151.31 20141400 29.94 NaN
  61. 2000-01-10 152.69 154.06 151.12 151.25 15226500 29.93 NaN
  62. 2000-01-11 151.00 152.69 150.62 151.50 15123000 29.98 NaN
  63. 2000-01-12 151.06 153.25 150.56 152.00 18342300 30.08 NaN
  64. 2000-01-13 153.13 154.94 153.00 153.75 14953500 30.42 1.0
  65. 2000-01-14 153.38 154.63 149.56 151.00 18480300 29.88 1.0
  66. 2000-01-18 149.62 149.62 146.75 148.00 18296700 29.29 1.0
  67. 2000-01-19 146.50 150.94 146.25 148.72 14849700 29.43 NaN
  68. 2000-01-20 149.06 149.75 142.63 145.94 30759000 28.88 1.0
  69. 2000-01-21 147.94 148.25 143.94 144.13 24005400 28.52 1.0
  70. 2000-01-24 145.31 145.94 136.44 138.13 27116100 27.33 1.0
  71. 2000-01-25 138.06 140.38 137.00 138.50 25387500 27.41 NaN
  72. 2000-01-26 140.50 142.19 138.88 141.44 15856800 27.99 NaN
  73. 2000-01-27 141.56 141.75 137.06 141.75 19243500 28.05 1.0
  74. 2000-01-28 140.31 140.50 133.63 134.00 29846700 26.52 1.0
  75. 2000-01-31 134.00 135.94 133.06 134.00 21782700 26.52 1.0
  76. 2000-02-01 134.25 137.00 134.00 136.00 27339000 26.91 NaN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement