0

I am beginner to pandas and out of a DataFrame i want to join another - The code is runable but the shape of the joind DataFrame is only on row. What can i do? Target is, to join both df Frames, the other data completly joined to DataFrame

Actually - the dict i want to join has the shape 3 x 1, so the joined Dict Returns NaN Fields.

What did i wrong?

Here is my Code:

import pandas_datareader.data as pdr
import fix_yahoo_finance
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pdr.get_data_yahoo("AMZ", start="2019-01-01", end="2019-02-28")

def supeperc(df):
  #Date = pd.Series(df.index.values)
  high = pd.Series(df['High'])
  low = pd.Series(df['Low'])
  close  = pd.Series(df['Close'])
  open  = pd.Series(df['Open'])

  gesamtR = high - low  

  diffhighcopen = abs(high - open)
  diffhighclose = abs(high - close)
  difflowcopen = abs(low - open)
  difflowclose = abs(low - close)
  diffopenclose = pd.Series(abs(open - close))
  calccandle = pd.Series(0)

  if np.where(open < close):
    if np.where((diffopenclose * 100 / gesamtR) > 70):

      calccandle = pd.Series(2)
    elif  np.where((diffhighcopen * 100 / gesamtR) < 15):
      calccandle = pd.Series(2)
      pass
    else:
      calccandle = pd.Series(1)
  elif close < open:
    if np.where((diffopenclose * 100 / gesamtR) > 70):
        calccandle = pd.Series(-2)
    elif  np.where((difflowclose * 100 / gesamtR) < 15):
      #sollte eigentlich ein Hammer sein
      #print (difflowclose * 100 / gesamtR)
      calccandle = pd.Series(-2)
    else:
      calccandle = pd.Series(-1)
    pass
  else:
    if np.where(((difflowcopen + difflowclose) / 2 * 100  / gesamtR) <= 2):
      calccandle = pd.Series(-3)
    elif np.where(((diffhighcopen + diffhighclose) / 2 * 100  / gesamtR) <= 2): 

      calccandle = pd.Series(+3)
    else:
      calccandle = pd.Series(0)

  if  np.where((gesamtR * 100 / open) > 5):
    myrange= pd.Series(1)
  else:
    myrange= pd.Series(0)

  if  np.where((diffopenclose * 100 / open) > 5):
    myChange= pd.Series(1)
  else:
    myChange= pd.Series(0)
  can = {'FakeCandle':calccandle,'ProzentVola':myrange,'Aend':myChange}

  print (myChange)
  dfCand = pd.DataFrame(can)

  #print (dfCand)
  df =  df.join(dfCand)   
  return df
print (supeperc(df))
Tonnbo
  • 3
  • 5
  • Please clarify: " the joind DataFrame is only on row" - "only one row" ? – Alex Yu Feb 16 '19 at 13:51
  • it is a function call with about 50 inputs out from another DataFrame. My ideas was, that it creates the same shape of data - but it isn't - That is the problem - only a one row shape is created with this. completly wrong? The shape of the can Dict& DF is only 1 row – Tonnbo Feb 16 '19 at 14:06
  • I tested your code with the stock `SPY` and every thing works fine. – keineahnung2345 Feb 16 '19 at 14:24
  • The DataFrame is not filled or not filled correctly - want to join these - i tried it now with a complete Dataframe but the same - the values are not filled correctly into "can" DataFrame - want to join df with can - think i missunderstood the np.where statement – Tonnbo Feb 16 '19 at 14:41

1 Answers1

0

Answer - it is a numpy problem - you can not act with numpy like this. Used conditions and it worked: Pandas conditional creation of a series/dataframe column

Tonnbo
  • 3
  • 5