-1

I want to adding new records into alldata file by concat newdata to it. Somehow the alldata get wiped out after each cycle. Can you please help to fix?

import schedule;import sched;import pandas as pd;

alldata=pd.DataFrame()

def myfunc(outside_file):
    newdata = pd.DataFrame({'id':['A', 'c'],
                        'value': [20, 0]},
                      )
    print(newdata)
    outside_file =pd.concat([outside_file,newdata])
    print(outside_file)
    
schedule.every().minute.at(':00').do(myfunc, alldata) 

while True:
    schedule.run_pending()
    print(alldata)
    time.sleep(30)
HnV
  • 9
  • 3

1 Answers1

1

You're not modifying alldata: you're passing it as argument to a function, and what's being modified is the argument inside this function, not the global variable. Compare the following:

  • Snippet 1:
import pandas as pd

alldata = pd.DataFrame()

def run(outside_df):
    df = pd.DataFrame({'id':['A', 'B'], 'value': [20, 0]})
    outside_df = pd.concat([outside_df, df])

for _ in range(5):
    run(alldata)
    
print(alldata)
# Empty DataFrame
# Columns: []
# Index: []
  • Snippet 2:
import pandas as pd

alldata = pd.DataFrame()

def run():
    global alldata
    df = pd.DataFrame({'id':['A', 'B'], 'value': [20, 0]})
    alldata = pd.concat([alldata, df])

for _ in range(5):
    run()
    
print(alldata)
#    id  value
#  0  A     20
#  1  B      0
#  0  A     20
#  1  B      0
#  0  A     20
#  1  B      0
#  0  A     20
#  1  B      0
#  0  A     20
#  1  B      0

Moral of the story: use global (or don't) when you want to modify a global variable inside a function.

enzo
  • 9,861
  • 3
  • 15
  • 38