Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bemserver_core/process/energy_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def energy2power(
convert_to,
):
"""Convert energy to power"""
timezone = start_dt.tzinfo
end_dt = end_dt.astimezone(timezone)

interval = energy_ts.get_property_value("Interval")
if interval is None:
Expand All @@ -71,6 +73,7 @@ def energy2power(
end_dt,
(energy_ts,),
data_state,
timezone=str(timezone),
)[energy_ts.id]

# Power = Energy / Time
Expand All @@ -93,13 +96,16 @@ def energyindex2power(
convert_to,
):
"""Convert energy index to power"""
timezone = start_dt.tzinfo
end_dt = end_dt.astimezone(timezone)

# Get energy index values
index_s = tsdio.get_timeseries_data(
start_dt,
end_dt,
(index_ts,),
data_state,
timezone=str(timezone),
)[index_ts.id]

# Compute energy as diff, with a 0 min for index rollover or meter change
Expand Down
3 changes: 3 additions & 0 deletions src/bemserver_core/process/forward_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def ffill(
bucket_width_unit,
):
"""Forward fill process"""
timezone = start_dt.tzinfo
end_dt = end_dt.astimezone(timezone)

# Define expected index
start_dt = ceil(start_dt, bucket_width_unit, bucket_width_value)
Expand All @@ -41,6 +43,7 @@ def ffill(
end_dt,
timeseries,
data_state,
timezone=str(timezone),
)

# For each TS, set last value before time interval as first value for interval
Expand Down
88 changes: 88 additions & 0 deletions tests/process/test_energy_power.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Energy <=> Power conversions tests"""

import datetime as dt
from zoneinfo import ZoneInfo

import pytest

Expand Down Expand Up @@ -153,6 +154,35 @@ def test_power2energy_process(self, users, timeseries):
)
assert_series_equal(data_s, expected_data_s)

# Check result is in start_dt timezone
data_s = power2energy(
start_dt.astimezone(ZoneInfo("Europe/Paris")),
h4_dt,
ts_0,
ds_1,
1800,
"Wh",
)
timestamps = sum(
[
[
dt.datetime(
2020, 1, 1, hour, 0, tzinfo=dt.timezone.utc
).astimezone(ZoneInfo("Europe/Paris")),
dt.datetime(
2020, 1, 1, hour, 30, tzinfo=dt.timezone.utc
).astimezone(ZoneInfo("Europe/Paris")),
]
for hour in (0, 1, 2, 3)
],
[],
)
expected_data_s = pd.Series(
[0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.5, 0.5],
index=pd.DatetimeIndex(timestamps, name="timestamp", freq="1800s"),
)
assert_series_equal(data_s, expected_data_s)

with pytest.raises(BEMServerCoreUndefinedUnitError):
data_s = power2energy(start_dt, h4_dt, ts_0, ds_1, 3600, "dummy")

Expand Down Expand Up @@ -283,6 +313,22 @@ def test_energy2power_process(self, users, timeseries):
):
data_s = energy2power(start_dt, end_dt, ts_4, ds_1, "W")

# Check result is in start_dt timezone
data_s = energy2power(
start_dt.astimezone(ZoneInfo("Europe/Paris")), h4_dt, ts_0, ds_1, "W"
)
timestamps = [
dt.datetime(2020, 1, 1, hour, 0, tzinfo=dt.timezone.utc).astimezone(
ZoneInfo("Europe/Paris")
)
for hour in (0, 1, 2, 3)
]
expected_data_s = pd.Series(
[0.0, 1.0, 0.0, 1.0],
index=pd.DatetimeIndex(timestamps, name="timestamp"),
)
assert_series_equal(data_s, expected_data_s)

with pytest.raises(BEMServerCoreUndefinedUnitError):
data_s = energy2power(start_dt, end_dt, ts_0, ds_1, "dummy")

Expand Down Expand Up @@ -410,6 +456,27 @@ def test_energyindex2power_process(self, users, timeseries):
)
assert_series_equal(data_s, expected_data_s)

# Check result is in start_dt timezone
data_s = energyindex2power(
start_dt.astimezone(ZoneInfo("Europe/Paris")),
h6_dt,
ts_0,
ds_1,
3600,
"W",
)
timestamps = [
dt.datetime(2020, 1, 1, hour, 0, tzinfo=dt.timezone.utc).astimezone(
ZoneInfo("Europe/Paris")
)
for hour in (0, 1, 2, 3, 4, 5)
]
expected_data_s = pd.Series(
[1.0, 3.0, 5.0, 7.0, 9.0, np.nan],
index=pd.DatetimeIndex(timestamps, name="timestamp", freq="h"),
)
assert_series_equal(data_s, expected_data_s)

with pytest.raises(BEMServerCoreUndefinedUnitError):
data_s = energyindex2power(start_dt, end_dt, ts_0, ds_1, 3600, "dummy")

Expand Down Expand Up @@ -537,6 +604,27 @@ def test_energyindex2energy_process(self, users, timeseries):
)
assert_series_equal(data_s, expected_data_s)

# Check result is in start_dt timezone
data_s = energyindex2energy(
start_dt.astimezone(ZoneInfo("Europe/Paris")),
h6_dt,
ts_0,
ds_1,
3600,
"Wh",
)
timestamps = [
dt.datetime(2020, 1, 1, hour, 0, tzinfo=dt.timezone.utc).astimezone(
ZoneInfo("Europe/Paris")
)
for hour in (0, 1, 2, 3, 4, 5)
]
expected_data_s = pd.Series(
[1.0, 3.0, 5.0, 7.0, 9.0, np.nan],
index=pd.DatetimeIndex(timestamps, name="timestamp", freq="h"),
)
assert_series_equal(data_s, expected_data_s)

with pytest.raises(BEMServerCoreUndefinedUnitError):
data_s = energyindex2energy(start_dt, end_dt, ts_0, ds_1, 3600, "dummy")

Expand Down
28 changes: 28 additions & 0 deletions tests/process/test_forward_fill.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Forward fill tests"""

import datetime as dt
from zoneinfo import ZoneInfo

import pytest

Expand Down Expand Up @@ -113,3 +114,30 @@ def test_ffill_process(self, users, timeseries):
)
expected_data_df.columns = [ts_3.id, ts_2.id, ts_2.id, ts_0.id]
assert_frame_equal(data_df, expected_data_df)

# Check result is in start_dt timezone
ts_l = (ts_0, ts_1, ts_2, ts_3)

data_df = ffill(
h4_dt.astimezone(ZoneInfo("Europe/Paris")),
end_dt,
ts_l,
ds_1,
2,
"hour",
)

timestamps = [
dt.datetime(2020, 1, 1, hour, 0, tzinfo=ZoneInfo("Europe/Paris"))
for hour in (6, 7, 8, 10, 12)
]
expected_data_df = pd.DataFrame(
{
ts_0.id: [1.0, np.nan, 1.0, 1.0, 1.0],
ts_1.id: [0.0, 6.0, 6.0, 9.0, 9.0],
ts_2.id: [np.nan, 42.0, 42.0, 42.0, 42.0],
ts_3.id: [np.nan, np.nan, np.nan, np.nan, np.nan],
},
index=pd.DatetimeIndex(timestamps, name="timestamp"),
)
assert_frame_equal(data_df, expected_data_df)
Loading