Skip to content

Commit 31c55de

Browse files
authored
Merge pull request #358 from ie3-institute/df/#357-fix-filter-for-interval
Fix error in filter_data_for_time_interval()
2 parents 9df50ee + ff18ddd commit 31c55de

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
1919
- Removed `q_characteristics` from `create_energy_management_systems_data` [#326](https://github.com/ie3-institute/pypsdm/issues/326)
2020
- Fix `emInput` from `parent_em` to `controlling_em` [#333](https://github.com/ie3-institute/pypsdm/issues/333)
2121
- Fix also from `em` to `controlling_em` for all `SystemParticipants` [#337](https://github.com/ie3-institute/pypsdm/issues/337)
22+
- Fix error in `filter_data_for_time_interval()` [#357](https://github.com/ie3-institute/pypsdm/issues/357)
2223

2324

2425
## 0.0.6

pypsdm/processing/dataframe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def filter_data_for_time_interval(
6969
if all_after_start and all_after_end:
7070
return data.drop(data.index)
7171
elif all_after_start:
72-
start_row = data.index[1]
72+
start_row = data.index[0]
73+
start = start_row
7374
else:
7475
before_start = data.index[data.index <= start]
7576
start_row = before_start[-1]

tests/processing/test_dataframe.py

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import pandas as pd
22
from numpy import float64
33

4-
from pypsdm.processing.dataframe import add_df, divide_positive_negative
4+
from pypsdm.processing.dataframe import (
5+
add_df,
6+
divide_positive_negative,
7+
filter_data_for_time_interval,
8+
)
59

610
index = pd.date_range("2012-01-01 10:00:00", "2012-01-01 13:00:00", freq="h")
711
data = [1, 2, -2, 3]
@@ -37,3 +41,139 @@ def test_add_df():
3741
b = b.reindex(index=[3, 2])
3842
res = add_df(a, b)
3943
pd.testing.assert_frame_equal(res, expected)
44+
45+
46+
def test_filter_data_for_time_interval():
47+
input_data = {
48+
"time": [
49+
"2016-04-01 17:30:00",
50+
"2016-04-01 18:15:00",
51+
"2016-04-01 19:30:00",
52+
"2016-04-01 20:45:00",
53+
],
54+
"p": [0.022, 0.0, 0.022, 0.0],
55+
"q": [0.0, 0.0, 0.0, 0.0],
56+
}
57+
58+
input_df = pd.DataFrame(input_data)
59+
60+
input_df["time"] = pd.to_datetime(input_df["time"])
61+
input_df.set_index("time", inplace=True)
62+
63+
start_1 = pd.to_datetime("2016-04-01 17:00:00")
64+
end_1 = pd.to_datetime("2016-04-01 21:00:00")
65+
res_1 = filter_data_for_time_interval(input_df, start_1, end_1)
66+
67+
expected_data_1 = {
68+
"p": [0.022, 0.000, 0.022, 0.000],
69+
"q": [0.000, 0.000, 0.000, 0.000],
70+
}
71+
72+
expected_index_1 = [
73+
pd.Timestamp("2016-04-01 17:30:00"),
74+
pd.Timestamp("2016-04-01 18:15:00"),
75+
pd.Timestamp("2016-04-01 19:30:00"),
76+
pd.Timestamp("2016-04-01 20:45:00"),
77+
]
78+
79+
expected_1 = pd.DataFrame(expected_data_1)
80+
expected_1.index = expected_index_1
81+
expected_1.index.name = "time"
82+
83+
pd.testing.assert_frame_equal(res_1, expected_1)
84+
85+
start_2 = pd.to_datetime("2016-04-01 17:30:00")
86+
end_2 = pd.to_datetime("2016-04-01 21:00:00")
87+
res_2 = filter_data_for_time_interval(input_df, start_2, end_2)
88+
89+
expected_data_2 = {
90+
"p": [0.022, 0.000, 0.022, 0.000],
91+
"q": [0.000, 0.000, 0.000, 0.000],
92+
}
93+
94+
expected_index_2 = [
95+
pd.Timestamp("2016-04-01 17:30:00"),
96+
pd.Timestamp("2016-04-01 18:15:00"),
97+
pd.Timestamp("2016-04-01 19:30:00"),
98+
pd.Timestamp("2016-04-01 20:45:00"),
99+
]
100+
101+
expected_2 = pd.DataFrame(expected_data_2)
102+
expected_2.index = expected_index_2
103+
expected_2.index.name = "time"
104+
105+
pd.testing.assert_frame_equal(res_2, expected_2)
106+
107+
start_2 = pd.to_datetime("2016-04-01 18:00:00")
108+
end_2 = pd.to_datetime("2016-04-01 21:00:00")
109+
res_2 = filter_data_for_time_interval(input_df, start_2, end_2)
110+
111+
expected_data_2 = {
112+
"p": [0.022, 0.000, 0.022, 0.000],
113+
"q": [0.000, 0.000, 0.000, 0.000],
114+
}
115+
116+
expected_index_2 = [
117+
pd.Timestamp("2016-04-01 18:00:00"),
118+
pd.Timestamp("2016-04-01 18:15:00"),
119+
pd.Timestamp("2016-04-01 19:30:00"),
120+
pd.Timestamp("2016-04-01 20:45:00"),
121+
]
122+
123+
expected_2 = pd.DataFrame(expected_data_2)
124+
expected_2.index = expected_index_2
125+
expected_2.index.name = "time"
126+
127+
pd.testing.assert_frame_equal(res_2, expected_2)
128+
129+
start_3 = pd.to_datetime("2016-04-01 18:15:00")
130+
end_3 = pd.to_datetime("2016-04-01 21:00:00")
131+
res_3 = filter_data_for_time_interval(input_df, start_3, end_3)
132+
133+
expected_data_3 = {"p": [0.000, 0.022, 0.000], "q": [0.000, 0.000, 0.000]}
134+
135+
expected_index_3 = [
136+
pd.Timestamp("2016-04-01 18:15:00"),
137+
pd.Timestamp("2016-04-01 19:30:00"),
138+
pd.Timestamp("2016-04-01 20:45:00"),
139+
]
140+
141+
expected_3 = pd.DataFrame(expected_data_3)
142+
expected_3.index = expected_index_3
143+
expected_3.index.name = "time"
144+
145+
pd.testing.assert_frame_equal(res_3, expected_3)
146+
147+
start_4 = pd.to_datetime("2016-04-01 17:00:00")
148+
end_4 = pd.to_datetime("2016-04-01 18:30:00")
149+
res_4 = filter_data_for_time_interval(input_df, start_4, end_4)
150+
151+
expected_data_4 = {"p": [0.022, 0.000], "q": [0.000, 0.000]}
152+
153+
expected_index_4 = [
154+
pd.Timestamp("2016-04-01 17:30:00"),
155+
pd.Timestamp("2016-04-01 18:15:00"),
156+
]
157+
158+
expected_4 = pd.DataFrame(expected_data_4)
159+
expected_4.index = expected_index_4
160+
expected_4.index.name = "time"
161+
162+
pd.testing.assert_frame_equal(res_4, expected_4)
163+
164+
start_5 = pd.to_datetime("2016-04-01 17:00:00")
165+
end_5 = pd.to_datetime("2016-04-01 17:15:00")
166+
res_5 = filter_data_for_time_interval(input_df, start_5, end_5)
167+
168+
expected_data_5 = {
169+
"p": [],
170+
"q": [],
171+
}
172+
173+
expected_index_5 = pd.DatetimeIndex([])
174+
175+
expected_5 = pd.DataFrame(expected_data_5)
176+
expected_5.index = expected_index_5
177+
expected_5.index.name = "time"
178+
179+
pd.testing.assert_frame_equal(res_5, expected_5)

0 commit comments

Comments
 (0)