Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assignment to unbound variable #187

Merged
merged 1 commit into from
Apr 24, 2023
Merged

Conversation

nickto
Copy link
Contributor

@nickto nickto commented Apr 24, 2023

When specifying is_balanced=True in aggregate function, we get the following error:

UnboundLocalError: local variable 'dates' referenced before assignment

This happens because dates is defined only in not balanced case.

    if not is_balanced:
        dates         = Y_bottom_df['ds'].unique()
        [...]
    else:
        balanced_df = Y_bottom_df.copy()
        # `dates` is not defined in this if-else branch 

Suggested fix: define it in the balanced case too:

    if not is_balanced:
        dates         = Y_bottom_df['ds'].unique()
        [...]
    else:
        dates       = Y_bottom_df['ds'].unique()
        balanced_df = Y_bottom_df.copy()

Test

Code which fails before the proposed fix:

import random
from datetime import date, timedelta

import pandas as pd

from hierarchicalforecast.utils import aggregate

# Create a sample data frame
random.seed(2023)
T = 5
n_bottom = 4

df = pd.DataFrame(
    {
        "level_1": ["a"] * T * 2 + ["b"] * T * 2,
        "level_2": [chr(ord("c") + i) for i in range(n_bottom) for _ in range(T)],
        "y": [random.randint(2, 4) for i in range(n_bottom) for _ in range(T)],
        "ds": [date(year=2023, month=1, day=1) + timedelta(days=Δ) for Δ in range(T)] * n_bottom,
    }
)
print(df)
#   level_1 level_2  y          ds
# 0       a       c  3  2023-01-01
# 1       a       c  4  2023-01-02
# 2       a       d  3  2023-01-01
# 3       a       d  3  2023-01-02
# 4       b       e  3  2023-01-01
# 5       b       e  4  2023-01-02
# 6       b       f  4  2023-01-01
# 7       b       f  3  2023-01-02

# Aggregate
hierarchy_levels = [
    ["level_1"],
    ["level_1", "level_2"],
]

df, S, tags = aggregate(df=df, spec=hierarchy_levels, is_balanced=True)

results in

UnboundLocalError: local variable 'dates' referenced before assignment

When specifying `is_balanced=True`, we get the following error:

```
UnboundLocalError: local variable 'dates' referenced before assignment
```

This happens because `dates` is defined only in not balanced case. Fix:
define it in balanced case too.
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@AzulGarza AzulGarza self-requested a review April 24, 2023 19:42
Copy link
Member

@AzulGarza AzulGarza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @nickto! Thank you for letting us know about the issue and solving it.🎉

LGTM! :)

@AzulGarza AzulGarza merged commit 3364340 into Nixtla:main Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants