Skip to content

Commit

Permalink
Merged in bugfix/empty-power-data (pull request #94)
Browse files Browse the repository at this point in the history
Bugfix/empty power data

* Actually convert to pandas DataFrames for use in the UI.

* Test multiplication for empty frames.

* Remove test parameterization already commented out.

Approved-by: Nicolas Höning
  • Loading branch information
Flix6x committed Nov 3, 2020
1 parent 6a0e251 commit 860b2e1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bvp/data/queries/utils.py
Expand Up @@ -154,7 +154,7 @@ def simplify_index(
else:
raise KeyError(f"Level {col} not found")
bdf.index = bdf.index.get_level_values("event_start")
return bdf
return pd.DataFrame(bdf)


def multiply_dataframe_with_deterministic_beliefs(
Expand Down Expand Up @@ -182,6 +182,9 @@ def multiply_dataframe_with_deterministic_beliefs(
an additional "belief_horizon" column if both df1 and df2 contain this column, and
an additional "source" column if result_source is set.
"""
if df1.empty and df2.empty:
return df1

df = (df1["event_value"] * df2["event_value"] * multiplication_factor).to_frame(
name="event_value"
)
Expand Down
61 changes: 61 additions & 0 deletions bvp/data/tests/test_queries.py
Expand Up @@ -136,3 +136,64 @@ def test_multiplication():
axis=0,
)
pd.testing.assert_frame_equal(df, df_compare)


def test_multiplication_with_one_empty_dataframe():
df1 = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df1["event_value"] = pd.to_numeric(df1["event_value"])
df1["belief_horizon"] = pd.to_timedelta(df1["belief_horizon"])

df2 = pd.DataFrame(
[[10.0, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
)

df_compare = pd.DataFrame(
[[np.nan, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
)
# set correct types
df_compare["event_value"] = pd.to_numeric(df_compare["event_value"])
df_compare["belief_horizon"] = pd.to_timedelta(df_compare["belief_horizon"])

df = multiply_dataframe_with_deterministic_beliefs(df1, df2)
pd.testing.assert_frame_equal(df, df_compare)


def test_multiplication_with_both_empty_dataframe():
df1 = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df1["event_value"] = pd.to_numeric(df1["event_value"])
df1["belief_horizon"] = pd.to_timedelta(df1["belief_horizon"])

df2 = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df2["event_value"] = pd.to_numeric(df2["event_value"])
df2["belief_horizon"] = pd.to_timedelta(df2["belief_horizon"])

df_compare = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df_compare["event_value"] = pd.to_numeric(df_compare["event_value"])
df_compare["belief_horizon"] = pd.to_timedelta(df_compare["belief_horizon"])

df = multiply_dataframe_with_deterministic_beliefs(df1, df2)
pd.testing.assert_frame_equal(df, df_compare)

0 comments on commit 860b2e1

Please sign in to comment.