3

i have a function that creates a dataframe. This is the first function. In the second function i want to use the dataframe that i have created in the first function.

That is the shorten function:

def einlesen():
    rbu = pd.read_excel('INPUT\RBU_COIN2.xlsx') #RBU-DATEN AUS EXCEL EINLESEN
    data_faktura = pd.merge(rbu, hvl, on='EQ_NR', how='left') #RBU UND HVL ANHAND DER EQ_NR ZUGEORDNET
    print('RBU-DATEN UND HVL-DATEN ZUSAMMENGEFASST!')
    data_faktura['TA'] = data_faktura['TA'].replace(['1', '2', 'a ', '3', '4', '5', '6'], '7') #TA NACH SUCHKRITERIEN ERSETZT
    #data_faktura.to_excel('fakt_daten.xlsx', index=False) #OUTPUT ALS EXCELDATEI
    print('Daten für Berechnung des Zwischenergebnisses erfasst!')
    Button_start1.config(state=ACTIVE) #BUTTON WIEDER AUF AKTIV GESETZT
    msg_oben.config(text='Daten eingelesen! Bitte Berechnung starten!')
    #os.startfile('fakt_daten.xlsx')
    return data_faktura

as you can see i tried to return the dataframe from the function with "return data_faktura" but it didnt work.

i try to call the dataframe "data_faktura" like that:

def zwischenergebnis(data_faktura):
    data_kl2m = data_faktura # EINLESEN VON TA '<2M' UND BILDUNG DER SUMME
    data_kl2m = data_kl2m[data_kl2m.TA == '<2M']

Here is the error i get:

TypeError: zwischenergebnis() missing 1 required positional argument: 'data_faktura'

Thats how i call the functions:

Button_einlesen = ttk.Button(mainWin, text='Faktura einlesen!', command=einlesen)
Button_einlesen.grid(row=4, columnspan=6, sticky="ew")
Button_start1= ttk.Button(mainWin, text='Zwischenergebnisse berechnen!', state=DISABLED, command=zwischenergebnis)
Button_start1.grid(row=5, column=0, columnspan=3, sticky="ew")

How can i solve that? Or what im doing wrong here?

Thank you for your help!


@tzaman:

here is the error i get:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 93, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "C:\Users\---\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 250, in nansum
    the_sum = values.sum(axis, dtype=dtype_sum)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\numpy\core\_methods.py", line 32, in _sum
    return umr_sum(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for +: 'float' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\----\Winpython\python-3.4.3\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\----\[INPROGRESS] Faktura_sylvia\Faktool\geruest_tool.py", line 221, in zwischenergebnis
    data_ags1_mw_KW = data_ags1_mw['KW_WERT'].sum()
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\generic.py", line 4255, in stat_func
    skipna=skipna, numeric_only=numeric_only)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\pandas\core\series.py", line 2084, in _reduce
    return op(delegate, skipna=skipna, **kwds)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 45, in _f
    return f(*args, **kwargs)
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 95, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 250, in nansum
    the_sum = values.sum(axis, dtype=dtype_sum)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\numpy\core\_methods.py", line 32, in _sum
    return umr_sum(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for +: 'float' and 'str'
5
  • I don't see where data_faktura is created anywhere in your function. Commented Aug 3, 2016 at 8:15
  • 1
    Maybe in function def einlesen() need return rbu instead return data_faktura. Commented Aug 3, 2016 at 8:19
  • Please post all the relevant code, including how you're calling einlesen and zwischenergebnis. Commented Aug 3, 2016 at 8:19
  • i edited the function code so you can see where i create data_faktura.. was my fault. i deleted to much of the code to shorten it.. Commented Aug 3, 2016 at 8:20
  • i also added the code for the button with that i call the functions Commented Aug 3, 2016 at 8:21

1 Answer 1

3

Your button callbacks can't communicate directly via returning and passing parameters, since you're not actually calling them yourself.

Instead, you could do something like adding them both to a class and using an instance variable. Something like this:

class Commands():
    def einlesen(self):
        # ... all your code
        self.df = data_faktura

    def zwischenergebnis(self):
        data_faktura = self.df
        # proceed as before

Then you can instantiate a Commands object and bind its methods as your button callbacks:

commander = Commands()
Button_einlesen = ttk.Button(mainWin, text='..', command=commander.einlesen)
Button_start1= ttk.Button(mainWin, text='..', command=commander.zwischenergebnis)
Sign up to request clarification or add additional context in comments.

3 Comments

i tried it like you posted it! now i get an other error! I edit the error in my post!
Your error is on the line data_ags1_mw['KW_WERT'].sum() - apparently all the information you're trying to sum is not numeric, so it's failing. If you're still having trouble with that, please post a separate question with more details about your dataframe.
yes i see.. i check that! But your solution works for the mentioned problem! Thank you ;)

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.