3

I am following a tutorial over at https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ and I've got a bit stuck. I am tyring to define, then immediately call, a function.

My code is as follows:

def merge_dfs_on_column(dataframes, labels, col):
    '''merge a single column of each dataframe on to a new combined dataframe'''
    series_dict={}
    for index in range(len(dataframes)):
        series_dict[labels[index]]=dataframes[index][col]
    return pd.DataFrame(series_dict)
# Merge the BTC price dataseries into a single dataframe
btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')

I can clearly see that I have defined the merge_dfs_on_column fucntion and I think the syntax is correct, however, when I call the function on the last line, I get the following error:

NameError                                 Traceback (most recent call last)
<ipython-input-22-a113142205e3> in <module>()
      1 # Merge the BTC price dataseries into a single dataframe
----> 2 btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')

NameError: name 'merge_dfs_on_column' is not defined

I have Googled for answers and carefully checked the syntax, but I can't see why that function isn't recognised when called.

Westworld
  • 190
  • 1
  • 2
  • 14

4 Answers4

6

Your function definition isn't getting executed by the Python interpreter before you call the function.

Double check what is getting executed and when. In Jupyter it's possible to run code out of input-order, which seems to be what you are accidentally doing. (perhaps try 'Run All')

David A
  • 415
  • 1
  • 4
  • 13
  • Good to hear! Since this is the problem you might consider changing the title to something like: "Name error when calling defined function in Jupyter"? – David A Dec 23 '17 at 20:22
1

Well, if you're defining yourself,

Then you probably have copy and pasted it directly from somewhere on the web and it might have characters that you are probably not able to see.

Just define that function by typing it and use pass and comment out other code and see if it is working or not.

Muku
  • 538
  • 4
  • 18
  • I deliberately avoid copy/pasting when doing tutorials in the hope that by typing it all out, I will learn the way the code is put together. That is the case here. Actually, to ensure that my typing was not the problem, I then actually replaced my typed version with a copy/paste version and had the same problem!! – Westworld Dec 23 '17 at 13:15
0

"run all" does not work.
Shutting down the kernel and restarting does not help either.

If I write:

def whatever(a):
    return a*2

whatever("hallo")

in the next cell, this works.

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Bo Scho
  • 21
  • 2
  • This looks like it's a new question rather than a comment. In which case you're more likely to get help by posting a new question. – David A Apr 19 '21 at 23:06
0

I have also experienced this kind of problem frequently in jupyter notebook
But after replacing %% with %%time the error resolved. I didn't know why?
So,after some browsing i get that this is not jupyter notenook issue,it is ipython issue
and here is the issue and also this problem is answered in this stackoverflow question