Skip to content

Commit

Permalink
Infer timeseries frequency after loading (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegrabowski authored Sep 2, 2024
1 parent 13f9a25 commit 16c33c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tests/test_disaggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def test_chow_lin(self):

def test_chow_lin_backcasting_error(self):
# Test case for quarterly to monthly disaggregation backcasting error, issue #6

expected = pd.read_csv("tests/data/R_Output_chow-lin_QtoM_2.csv")

low_freq_data = pd.read_csv("tests/data/AL_Quarterly_Data_Modified.csv")
Expand Down Expand Up @@ -281,6 +280,29 @@ def test_chow_lin_two_indicator(self):

self.assertEqual(expected, sales_q_chow_lin.to_frame())

def test_chow_lin_no_freq(self):
expected = pd.read_csv("tests/data/R_output_chow_lin_two_indicator.csv", index_col=0)
expected.index = self.exports_q.index
expected.columns = ["sales"]

df2 = self.exports_q.merge(self.imports_q, left_index=True, right_index=True)

sales_nofreq = self.sales_a.copy()
sales_nofreq.index.freq = None
assert sales_nofreq.index.freq is None

sales_q_chow_lin = disaggregate_series(
sales_nofreq,
df2.resample("QS-OCT").first().assign(constant=1),
method="chow-lin",
agg_func="sum",
optimizer_kwargs={"method": "l-bfgs-b"},
verbose=True,
)

self.assertEqual(expected, sales_q_chow_lin.to_frame())
assert sales_q_chow_lin.index.freq == "QS-OCT"

def test_denton(self):
expected = pd.read_csv("tests/data/R_output_denton.csv", index_col=0)
expected.index = pd.date_range(start="1975-01-01", freq="QS-OCT", periods=144)
Expand Down
3 changes: 3 additions & 0 deletions tsdisagg/ts_disagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ def prepare_input_dataframes(df1, df2, target_freq, method):
"dataframe of high-frequency indicators must be provided."
)

df1_out.index.freq = df1_out.index.inferred_freq
df2_out.index.freq = df2_out.index.inferred_freq

df = pd.merge(df1_out, df2_out, left_index=True, right_index=True, how="outer")
return df, df1_out, df2_out, time_conversion_factor

Expand Down

0 comments on commit 16c33c6

Please sign in to comment.