Skip to content

Commit

Permalink
Pandas 1 0 0 compatibility fixes (#156)
Browse files Browse the repository at this point in the history
* bug #153; pandas timedelta no longer supports M,Y

* bug #152; error in groupby.first()

* bug #153; pandas timedelta no longer supports M,Y

* bug #154; pandas.util.testing has no _check_isinstance

* bug #155; pandas.Series.nonzero() no longer exists

* Fix PEP8 errors
  • Loading branch information
capelastegui committed Feb 3, 2020
1 parent 9bc3300 commit f424bc3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
8 changes: 5 additions & 3 deletions anticipy/forecast.py
Expand Up @@ -1251,11 +1251,13 @@ def run_forecast_single(df_y,
l_df_optimize_info,
sort=False,
ignore_index=True)

# Determine best fits
df_best_fit = (
df_metadata.loc[df_metadata.is_fit].sort_values('aic_c').groupby(
'source', as_index=False).first()[['source_long', 'model']]
df_metadata.loc[df_metadata.is_fit]
.sort_values('aic_c')
[['source', 'source_long', 'model']]
.groupby('source', as_index=False)
.first()
)
df_best_fit['is_best_fit'] = True

Expand Down
32 changes: 22 additions & 10 deletions anticipy/model_utils.py
Expand Up @@ -94,14 +94,24 @@ def apply_a_x_scaling(a_x, model=None, scaling_factor=100.0):
return a_x


dict_freq_units_per_year = {
'A': 1.0,
'Y': 1.0,
'D': 365.0,
'W': 52.0,
'M': 12,
'Q': 4,
'H': 24 * 365.0}
dict_freq_units_per_year = dict(
A=1.0,
Y=1.0,
D=365.0,
W=52.0,
M=12,
Q=4,
H=24 * 365.0
)

dict_dateoffset_input = dict(
Y='years',
A='years',
M='months',
W='weeks',
D='days',
H='hours'
)


def get_s_x_extrapolate(
Expand Down Expand Up @@ -160,8 +170,10 @@ def get_s_x_extrapolate(
# change to dict to support more frequencies
freq_units_per_year = dict_freq_units_per_year.get(freq_short, 365.0)
extrapolate_units = extrapolate_years * freq_units_per_year
offset_input = {dict_dateoffset_input.get(freq_short):
extrapolate_units}
date_end_forecast = date_end_actuals + \
pd.to_timedelta(extrapolate_units, unit=freq_short)
pd.DateOffset(**offset_input)

index = pd.date_range(
date_start_actuals,
Expand Down Expand Up @@ -258,7 +270,7 @@ def get_s_aic_c_best_result_key(s_aic_c):
if s_aic_c.empty or s_aic_c.isnull().all():
return None
if (s_aic_c.values == -np.inf).any():
(key_best_result,) = (s_aic_c == -np.inf).nonzero()
(key_best_result,) = (s_aic_c == -np.inf).to_numpy().nonzero()[0]
key_best_result = s_aic_c.index[key_best_result.min()]
else:
key_best_result = s_aic_c.argmin()
Expand Down
3 changes: 2 additions & 1 deletion anticipy/utils_test.py
Expand Up @@ -118,7 +118,8 @@ def assert_series_equal(
"""
le = left
ri = right
pdt._check_isinstance(le, ri, pd.Series)
self.assertIsInstance(le, pd.Series)
self.assertIsInstance(ri, pd.Series)
if ignore_index:
le = le.reset_index(drop=True)
ri = ri.reset_index(drop=True)
Expand Down
12 changes: 4 additions & 8 deletions tests/test_model_utils.py
Expand Up @@ -114,17 +114,13 @@ def test_get_a_x_date_extrapolate(self):
self.assertLessEqual(
s_x.index.max(),
ts.index.max() +
1.1 *
pd.Timedelta(
1,
'Y'))
# add 1.1 years
1.1 * pd.Timedelta(365, 'D'))
self.assertGreaterEqual(
s_x.index.max(),
ts.index.max() +
0.9 *
pd.Timedelta(
1,
'Y'))
# add 0.9 years
0.9 * pd.Timedelta(365, 'D'))

# Check that all actuals values are in extrapolated series
self.assertEquals(np.setdiff1d(ts.index, s_x.index).size, 0)
Expand Down

0 comments on commit f424bc3

Please sign in to comment.