spinjet / pdopt-code

Public Release Version of PDOPT
https://pdopt-code.readthedocs.io/en/main/
MIT License
3 stars 1 forks source link

FutureWarnings when running example 1 #3

Closed jbussemaker closed 9 months ago

jbussemaker commented 11 months ago

Environment: Python 12 env created with conda, dependencies installed with pip install -r requirements.txt

When I run energy_management_experiment.py in example_1, I continuously get the following warnings:

C:\...\pdopt-code\example\example_1\HE_Model.py:324: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[0.02935453 0.04485256 0.0603506  0.07584864 0.09134668 0.10684472
 0.12234276 0.1378408  0.15333884 0.16883688 0.18433491 0.19983295
 0.21533099 0.23082903 0.24632707 0.26182511 0.27732315 0.29282119
 0.30831923 0.32381726 0.3393153  0.35481334 0.37031138 0.38580942
 0.40130746 0.4168055  0.43230354 0.44780158 0.46329962 0.47879765]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  mission.loc[mission['tag'] == s, 'pDOH'] = tmp
C:\...\pdopt-code\example\example_1\HE_Model.py:324: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[0.24989405 0.24987103 0.24984757 0.24982366 0.24979927 0.24977438
 0.24974898 0.24972304 0.24969653 0.24966944 0.24964174 0.24961339
 0.24958437 0.24955465 0.24952418 0.24949293 0.24946086 0.24942793
 0.24939408 0.24935926 0.24932342 0.24928649 0.24924841 0.24920909
 0.24916847 0.24912644 0.24908291 0.24903776 0.24899086 0.24894209
 0.24889128]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  mission.loc[mission['tag'] == s, 'pDOH'] = tmp

The env has pandas 2.1.4 installed.

Part of JOSS submission

e-dub commented 9 months ago

I am also getting this future warning, e.g. from example 1

pdopt-code/example/example_1/HE_Model.py:324: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '[0.21654579 0.22007194 0.2235981  0.22712425 0.2306504  0.23417656
 0.23770271 0.24122887 0.24475502 0.24828118 0.25180733 0.25533348
 0.25885964 0.26238579 0.26591195 0.2694381  0.27296426 0.27649041
 0.28001656 0.28354272 0.28706887 0.29059503 0.29412118 0.29764734
 0.30117349 0.30469965 0.3082258  0.31175195 0.31527811 0.31880426]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.

and example 2

pdopt-code/example/example_2/HE_Model.py:324: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '[0.0958239  0.09273458 0.08964526 0.08655594 0.08346662 0.08037729
 0.07728797 0.07419865 0.07110933 0.06802001 0.06493069 0.06184137
 0.05875204 0.05566272 0.0525734  0.04948408 0.04639476 0.04330544
 0.04021611 0.03712679 0.03403747 0.03094815 0.02785883 0.02476951
 0.02168019 0.01859086 0.01550154 0.01241222 0.0093229  0.00623358]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  mission.loc[mission['tag'] == s, 'pDOH'] = tmp

Running

spinjet commented 9 months ago

Dear @jbussemaker @e-dub

I ran the code on a fresh installation and had the same FutureWarning issue. It stems from when the file mission.csv is loaded in the code at line 336 of HE_model.py: https://github.com/spinjet/pdopt-code/blob/d2bf7d21ed99f6790ab0c4ec7ca927a0e4a6cc6b/example/example_1/HE_Model.py#L335

In the .csv file the column pDOH only contains zeros, hence Pandas interpreted it as a int64 type. As the degree of hybridisation is then defined in the function generate_doh, this is replaced with a float value from 0 to 1. It seems in previous versions of Pandas this recast didn't throw any Warnings, hence I never noticed it.

This has now been fiixed by ensuring the dtype of the pDOH column in the dataframe is set to float when the energy management strategy is created (see in https://github.com/spinjet/pdopt-code/blob/31f65670a6fa449833f975c7cba305950c74c2ad/example/example_1/HE_Model.py#L330):

   mission.loc[mission['tag'] == s, 'pDOH'] = None
   mission.loc[mission['tag'] == s, 'pDOH'] = tmp.astype(float)

I also took the opportunity to do a bit of linting in the example files.