I keep getting an error
UnboundLocalError: local variable 'df' referenced before assignment
for this function:
def compare_to_sector(Name, MA): df = df[(df.Date >= str(pd.Timestamp(datetime.now() - relativedelta(years=1))))]
This isn’t the full function, but the whole problem lies here. I am trying to create a new dataframe that only pulls from the past year of the initial dataframe.
I will need to create options for year count using a function variable once I get this figured out, but for right now I’m already getting an error here.
If I do this ‘df’ line outside the function, it works fine unto itself so.. must be something about calling it within the function.
This is happening because you’re using the same name for the new variable as the one you already have,
df. This is making the python interpreter think that you’re trying to reference
df in it’s own creation, so you’re “referencing” the variable before it’s assignment, like the error message says.
[david@darvid-pc ~]$ python Python 3.8.6 (default, Sep 30 2020, 04:00:38) [GCC 10.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = 5 >>> def f(): ... x = x + 5 ... >>> f() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in f UnboundLocalError: local variable 'x' referenced before assignment
There are a couple ways to fix this. You could indicate that the variable you’re using is global.
>>> def g(): ... global x ... x = x + 5 ... >>> g() >>> x 10
As you can see, this does have the side effect of actually mutating that global variable. Which may or may not be what you’re after.
You could also just use a different name.
>>> def h(): ... y = x + 5 ... print(y) ... >>> h() 10 >>> x 5 >>>