0

I have this dataframe:

df.head()
                         Open      High       Low     Close  Volume  day_month
2006-04-13 10:00:00   1921.75   1922.00   1918.00   1918.25   11782 2006-04-13
2006-04-13 10:30:00   1918.25   1931.75   1918.00   1931.00   39744 2006-04-13
2006-04-13 11:00:00   1931.25   1934.00   1929.00   1930.25   34385 2006-04-13
2006-04-13 11:30:00   1930.50   1932.00   1928.50   1931.25   13539 2006-04-13
2006-04-13 12:00:00   1931.25   1932.25   1928.25   1928.75   10045 2006-04-13


df.tail()

                         Open      High       Low     Close  Volume  day_month
2021-06-18 14:30:00  14077.50  14085.25  14033.00  14039.00   19573 2021-06-18
2021-06-18 15:00:00  14039.00  14085.50  14023.50  14077.00   27464 2021-06-18
2021-06-18 15:30:00  14077.00  14092.75  14028.75  14041.75   39410 2021-06-18
2021-06-18 16:00:00  14041.75  14049.00  14019.50  14042.75   17071 2021-06-18
2021-06-18 16:30:00  14040.00  14042.25  14015.00  14017.75    3167 2021-06-18

df.info()

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 57233 entries, 2006-04-13 10:00:00 to 2021-06-18 16:30:00
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Open       57233 non-null  float64       
 1   High       57233 non-null  float64       
 2   Low        57233 non-null  float64       
 3   Close      57233 non-null  float64       
 4   Volume     57233 non-null  int32         
 5   day_month  57233 non-null  datetime64[ns]
dtypes: datetime64[ns](1), float64(4), int32(1)

I am using marketprofile package to create a function and I want to store the output into different variables

from market_profile import MarketProfile

I create this function to store the values

def mp_va(df):
    mp = MarketProfile(df, tick_size = 0.25)
    mp_slice = mp[df.index.min():df.index.max()]
    
    return mp_slice.value_area[0], mp_slice.value_area[1], mp_slice.poc_price 

I want to store the three output into a data frame in python for all days in my dataset

Using the below code I applied the function I just create into all days in my data frame

df_mp = df.groupby(['day_month']).apply(mp_va)

This was the output:

df_mp

day_month
2006-04-13        (1927.5, 1931.25, 1930.25)
2006-04-17          (1898.5, 1922.5, 1898.5)
2006-04-18       (1923.75, 1938.25, 1935.25)
2006-04-19        (1935.75, 1941.25, 1936.5)
2006-04-20       (1939.25, 1941.75, 1939.25)
                           ...              
2021-06-14      (13998.75, 14055.5, 14021.0)
2021-06-15    (14030.25, 14097.25, 14097.25)
2021-06-16      (13916.5, 14016.5, 13922.75)
2021-06-17     (14024.75, 14160.0, 14024.75)
2021-06-18       (14052.0, 14106.5, 14096.5)
Length: 3913, dtype: object

This was one of the suggestions:

def mp_va(df):
    global df_2
    mp = MarketProfile(df, tick_size = 0.25)
    mp_slice = mp[df.index.min():df.index.max()]
    
    data =  {mp_slice.value_area[0], mp_slice.value_area[1], mp_slice.poc_price}
    df_2 = pd.DataFrame(data)
    return df_2

df_mp = df.groupby(['day_month']).apply(mp_va)

df_mp

The output for this code was:

                     0
day_month             
2006-04-13 0   1930.25
           1   1931.25
           2   1927.50
2006-04-17 0   1898.50
           1   1922.50
...                ...
2021-06-17 0  14024.75
           1  14160.00
2021-06-18 0  14096.50
           1  14106.50
           2  14052.00

[10202 rows x 1 columns]
ValueError: not enough values to unpack (expected 3, got 1)

Detailed traceback: 
  File "<string>", line 1, in <module>

This was my first attempt to create the data frame with all three variables by unpacking

va_high, va_low, op_price = df_mp

This is the output from that code which gives me an error

ValueError: too many values to unpack (expected 3)

I also tried:

def mp_va(df):
    mp = MarketProfile(df, tick_size = 0.25)
    mp_slice = mp[df.index.min():df.index.max()]
    
    data =  {mp_slice.value_area[0], mp_slice.value_area[1], mp_slice.poc_price}

But this code gives me an error:

ValueError: not enough values to unpack (expected 2, got 0)

I also tried this:

def mp_va(df):
    global df_2
    mp = MarketProfile(df, tick_size = 0.25)
    mp_slice = mp[df.index.min():df.index.max()]
    
    data =  {mp_slice.value_area[0], mp_slice.value_area[1], mp_slice.poc_price}
    df_2 = pd.DataFrame(data)

df_mp = df.groupby(['day_month']).apply(mp_va)

va_high, va_low, op_price = df_mp

But this gives me an error message:

ValueError: not enough values to unpack (expected 3, got 0)

My question is: Is there a way to store the values of the three outputs in a data frame from the above function?

The expected output will be:


             va_low  va_high  op_price   
2006-04-13   1927.5  1931.25   1930.25
2006-04-17   1898.5  1922.5    1934.50 
5
  • the first version is the correct way to return multiple values. But you're returning 3 values, not 2. Commented Jun 24, 2021 at 0:09
  • What is the desired result? Commented Jun 24, 2021 at 0:10
  • I edit my question. Hope it is clear now Commented Jun 24, 2021 at 0:22
  • 1
    Please post current output for both attempts, i.e., df_mp. Also without return, your function returns None. Commented Jun 24, 2021 at 0:28
  • You can append the outputs into a list and then return that list. Then you can get as many outputs as you possibly want Commented Jun 24, 2021 at 3:10

1 Answer 1

2

I'm not able to test this at the time, but based on the traceback; Your function isn't returning any values which is why it states (expected 3, got 0) if you add return df_2 to the end of mp_va() function, it should fix your issue

def mp_va(df):
    global df_2
    mp = MarketProfile(df, tick_size = 0.25)
    mp_slice = mp[df.index.min():df.index.max()]

    data =  {mp_slice.value_area[0], mp_slice.value_area[1], mp_slice.poc_price}
    df_2 = pd.DataFrame(data)
    return df_2

UPDATE: the below code now works

def mp_va(df): 
    global df_2 
    mp = MarketProfile(df, tick_size = 0.25) 
    mp_slice = mp[df.index.min():df.index.max()] data = {'vn Low': 
    [mp_slice.value_area[0]], 'vn High' : [mp_slice.value_area[1]], 'op Price': [mp_slice.poc_price]} 

    df_2 = pd.DataFrame(data) 
    return df_2 

df_map =  df.groupby(['day_month']).apply(mp_va)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you check the first and second output? Is there a way to get something similar to the last output?
I can't completely replicate the data you're using so it's hard to test completly, but you should be able to set the dataFrame to have the columns with the Dict //New Code Since Formatting doesn't work def mp_va(df): global df_2 mp = MarketProfile(df, tick_size = 0.25) mp_slice = mp[df.index.min():df.index.max()] data = {'vn Low': [mp_slice.value_area[0]], 'vn High' : [mp_slice.value_area[1]], 'op Price': [mp_slice.poc_price]} df_2 = pd.DataFrame(data) return df_2 df_map = df.groupby(['day_month']).apply(mp_va)

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.