Skip to content

Commit 4bb48c5

Browse files
committed
Vectorized part of on_off_status and updated test pickle files
1 parent 84dee56 commit 4bb48c5

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

caar/timeseries.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,21 @@ def on_off_status(df, id, start, end, freq='1min'):
103103
.snap(freq=freq)
104104
.tolist())
105105
# Populate array
106+
starts = _integer_index_based_on_freq(starts_by_freq, start, freq)
107+
ends = _integer_index_based_on_freq(record_ends_by_freq, start, freq)
108+
106109
for i in range(len(records)):
107-
start_index = _index_of_timestamp(start, starts_by_freq[i], freq)
108-
end_index = _index_of_timestamp(start, record_ends_by_freq[i], freq)
109-
status_in_intervals[start_index:end_index + 1]['on'] = 1
110+
status_in_intervals[starts[i]:ends[i] + 1]['on'] = 1
110111
return status_in_intervals
111112

112113

114+
def _integer_index_based_on_freq(datetimes, reference, frequency):
115+
time_deltas = pd.Series(datetimes) - reference
116+
freq = _timedelta_from_string(frequency)
117+
indexes = np.array(time_deltas/freq).astype(np.int)
118+
return indexes
119+
120+
113121
def temps_arr_by_freq(df, id, start, end, cols=None, freq='1min', actuals_only=False):
114122
"""Returns NumPy array containing timestamps ('times') and temperatures at the specified frequency. If *actuals_only* is True, only the observed temperatures will be returned in an array. Otherwise, by default, intervals without observations are filled with zeros.
115123

tests/data/test_TX_cycles.pickle

14.1 KB
Binary file not shown.
-153 KB
Binary file not shown.

tests/test_caar.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,20 @@ def test_df_creation_after_fixed_dict(data_file, states, thermostats, postal, cy
228228
df = df_creation_func(clean_dict, **kwargs)
229229
assert isinstance(df, pd.DataFrame)
230230

231-
# @slow
232-
# @pytest.mark.parametrize("df_fixture, id, start, end, freq",
233-
# [(cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
234-
# dt.datetime(2012, 6, 18, 23, 0, 0), '1min30s'),
235-
# (cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
236-
# dt.datetime(2012, 6, 18, 23, 0, 0), 'min30s'),
237-
# (cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
238-
# dt.datetime(2012, 6, 18, 23, 0, 0), '2min'),
239-
# (cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
240-
# dt.datetime(2012, 6, 18, 23, 0, 0), 'min')])
241-
# def test_on_off_status_by_interval(df_fixture, id, start, end, freq):
242-
# kwargs = {'freq': freq}
243-
# on_off = ts.on_off_status(df_fixture, id, start, end, **kwargs)
244-
# assert len(on_off['times']) > 0
231+
232+
@pytest.mark.parametrize("df_fixture, id, start, end, freq",
233+
[(cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
234+
dt.datetime(2012, 6, 18, 23, 0, 0), '1min30s'),
235+
(cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
236+
dt.datetime(2012, 6, 18, 23, 0, 0), 'min30s'),
237+
(cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
238+
dt.datetime(2012, 6, 18, 23, 0, 0), '2min'),
239+
(cycle_df_fixture(), THERMO_ID1, dt.datetime(2012, 6, 18, 21, 0, 0),
240+
dt.datetime(2012, 6, 18, 23, 0, 0), 'min')])
241+
def test_on_off_status_by_interval(df_fixture, id, start, end, freq):
242+
kwargs = {'freq': freq}
243+
on_off = ts.on_off_status(df_fixture, id, start, end, **kwargs)
244+
assert len(on_off['times']) > 0
245245
#
246246
# @slow
247247
# @pytest.mark.parametrize("df_fixture, id, start, end, freq",

0 commit comments

Comments
 (0)