3

I'm working with some data, and just writing lines in sequence works fine and gives me the results I want (to extract a row of data from the date from the dataframe 'restaurant'):

orders = restaurant[(restaurant.index == date)]

However, when I put this into a function, it no longer is able to look it up by date, and instead just gives me a blank data frame:

def datesearch(date)   
    orders = restaurant[(restaurant.index == date)]
    return orders

I can't seem to figure out why it's fine outside the function, but for some reason, it can't search by the date when I put it in a function.

7
  • Are you passing the date to the function? Commented Sep 12, 2014 at 21:06
  • I'm very sorry, but what does that mean? Do I need to write date = date into my function first? Commented Sep 12, 2014 at 21:07
  • You'd call datesearch as datesearch(date). Are you getting an error message? Commented Sep 12, 2014 at 21:07
  • No - I don't get an error. Instead, I get a blank dataframe. It seems like my function is unable to locate the "date" row. Commented Sep 12, 2014 at 21:08
  • Hm, okay. Are you catching the return value from the function? In other words, orders = datesearch(date)? Commented Sep 12, 2014 at 21:09

1 Answer 1

1

I think restaurant is a global variable, so it might not be using the correct data. Try this:

def datesearch(date) 
    global restaurant  
    orders = restaurant[(restaurant.index == date)]
    return orders
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand how this could possibly solve the problem, since you only need to declare a name global if you are modifying it.

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.