diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bd6a359..045a6a18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.9] - 2022-04-21 +### Fix +- Fixed get_loc deprecation warning using now get_indexer pandas method. Improved selection of closest index. + ## [0.2.8] - 2022-04-18 ### Fix - Fixed if sentence to correctly use the supervisor API for publish data. diff --git a/docs/conf.py b/docs/conf.py index 840cb3dd..6dbc860b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -22,7 +22,7 @@ author = 'David HERNANDEZ' # The full version, including alpha/beta/rc tags -release = '0.2.8' +release = '0.2.9' # -- General configuration --------------------------------------------------- diff --git a/setup.py b/setup.py index a23faf8b..dd2ec0b2 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ setup( name='emhass', # Required - version='0.2.8', # Required + version='0.2.9', # Required description='An Energy Management System for Home Assistant', # Optional long_description=long_description, # Optional long_description_content_type='text/markdown', # Optional (see note above) diff --git a/src/emhass/command_line.py b/src/emhass/command_line.py index 1ae9c2d0..56c5b863 100644 --- a/src/emhass/command_line.py +++ b/src/emhass/command_line.py @@ -163,11 +163,9 @@ def publish_data(input_data_dict: dict, logger: logging.Logger, opt_res_dayahead.index.freq = input_data_dict['retrieve_hass_conf']['freq'] # Estimate the current index now_precise = datetime.now(input_data_dict['retrieve_hass_conf']['time_zone']).replace(second=0, microsecond=0) - try: - idx_closest = opt_res_dayahead.index.get_loc(now_precise, method='ffill') - except Exception: - logger.warning("Problem finding DataFrame index, using nearest option as a backup") - idx_closest = opt_res_dayahead.index.get_loc(now_precise, method='nearest') + idx_closest = opt_res_dayahead.index.get_indexer([now_precise], method='ffill')[0] + if idx_closest == -1: + idx_closest = opt_res_dayahead.index.get_indexer([now_precise], method='nearest')[0] # Publish PV forecast input_data_dict['rh'].post_data(opt_res_dayahead['P_PV'], idx_closest, 'sensor.p_pv_forecast', "W", "PV Power Forecast") diff --git a/tests/test_optimization.py b/tests/test_optimization.py index 85c53bbe..edf54e2c 100644 --- a/tests/test_optimization.py +++ b/tests/test_optimization.py @@ -5,6 +5,7 @@ import pandas as pd import pathlib import pickle +from datetime import datetime, timezone from emhass.retrieve_hass import retrieve_hass from emhass.optimization import optimization @@ -55,6 +56,9 @@ def setUp(self): self.days_list, self.costfun, root, logger) self.df_input_data = self.fcst.get_load_cost_forecast(self.df_input_data) self.df_input_data = self.fcst.get_prod_price_forecast(self.df_input_data) + self.input_data_dict = { + 'retrieve_hass_conf': retrieve_hass_conf, + } def test_perform_perfect_forecast_optim(self): self.opt_res = self.opt.perform_perfect_forecast_optim(self.df_input_data) @@ -72,6 +76,12 @@ def test_perform_dayahead_forecast_optim(self): self.assertIsInstance(self.opt_res_dayahead.index, pd.core.indexes.datetimes.DatetimeIndex) self.assertIsInstance(self.opt_res_dayahead.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype) self.assertTrue('cost_fun_'+self.costfun in self.opt_res_dayahead.columns) + # Testing estimation of the current index + now_precise = datetime.now(self.input_data_dict['retrieve_hass_conf']['time_zone']).replace(second=0, microsecond=0) + idx_closest = self.opt_res_dayahead.index.get_indexer([now_precise], method='ffill')[0] + self.assertTrue(idx_closest == -1) + idx_closest = self.opt_res_dayahead.index.get_indexer([now_precise], method='nearest')[0] + self.assertTrue(idx_closest == 0) if __name__ == '__main__': unittest.main()