Skip to content

Commit 49785ac

Browse files
Fix bugs with pandas 1.5.0
1 parent ac80a06 commit 49785ac

File tree

6 files changed

+77
-53
lines changed

6 files changed

+77
-53
lines changed

docs/whats_new.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
What's New
22
==========
3+
v3.7.1 (2022/09/19)
4+
-------------------
5+
6+
New Features
7+
~~~~~~~~~~~~
8+
9+
Breaking changes
10+
~~~~~~~~~~~~~~~~
11+
12+
Deprecations
13+
~~~~~~~~~~~~
14+
15+
Bug fixes
16+
~~~~~~~~~
17+
- Fix bugs with :py:class:`pandas.DataFrame` 1.5.0 (:issue:`366`). (`@enekomartinmartinez <https://github.com/enekomartinmartinez>`_)
18+
19+
Documentation
20+
~~~~~~~~~~~~~
21+
22+
Performance
23+
~~~~~~~~~~~
24+
25+
Internal Changes
26+
~~~~~~~~~~~~~~~~
27+
328
v3.7.0 (2022/09/19)
429
-------------------
530

pysd/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.7.0"
1+
__version__ = "3.7.1"

pysd/py_backend/model.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,6 @@ def run(self, params=None, return_columns=None, return_timestamps=None,
11751175
# create a dictionary splitting run cached and others
11761176
capture_elements = self._split_capture_elements(capture_elements)
11771177

1178-
11791178
# include outputs in cache if needed
11801179
self._dependencies["OUTPUTS"] = {
11811180
element: 1 for element in capture_elements["step"]
@@ -1622,11 +1621,11 @@ def _split_capture_elements(self, capture_elements):
16221621
Returns
16231622
-------
16241623
capture_dict: dict
1625-
Dictionary of sets with keywords step and run.
1624+
Dictionary of list with keywords step and run.
16261625
16271626
"""
1628-
capture_dict = {'step': set(), 'run': set(), None: set()}
1629-
[capture_dict[self.cache_type[element]].add(element)
1627+
capture_dict = {'step': [], 'run': [], None: []}
1628+
[capture_dict[self.cache_type[element]].append(element)
16301629
for element in capture_elements]
16311630
return capture_dict
16321631

pysd/py_backend/output.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ModelOutput():
3030
----------
3131
model: pysd.Model
3232
PySD Model object
33-
capture_elements: set
33+
capture_elements: list
3434
Which model elements to capture - uses pysafe names.
3535
out_file: str or pathlib.Path
3636
Path to the file where the results will be written.
@@ -44,7 +44,7 @@ def __init__(self, model, capture_elements, out_file=None):
4444
# want them to run (DataFrameHandler runs first)
4545
self.handler = DataFrameHandler(DatasetHandler(None)).handle(out_file)
4646

47-
capture_elements.add("time")
47+
capture_elements.append("time")
4848
self.capture_elements = capture_elements
4949

5050
self.initialize(model)
@@ -206,7 +206,7 @@ def initialize(self, model, capture_elements):
206206
----------
207207
model: pysd.Model
208208
PySD Model object
209-
capture_elements: set
209+
capture_elements: list
210210
Which model elements to capture - uses pysafe names.
211211
212212
Returns
@@ -246,14 +246,14 @@ def initialize(self, model, capture_elements):
246246

247247
def update(self, model, capture_elements):
248248
"""
249-
Writes values of cache step variables from the capture_elements set
250-
in the netCDF4 Dataset.
249+
Writes values of cache step variables from the capture_elements
250+
list in the netCDF4 Dataset.
251251
252252
Parameters
253253
----------
254254
model: pysd.Model
255255
PySD Model object
256-
capture_elements: set
256+
capture_elements: list
257257
Which model elements to capture - uses pysafe names.
258258
259259
Returns
@@ -280,7 +280,7 @@ def __update_run_elements(self, model, capture_elements):
280280
----------
281281
model: pysd.Model
282282
PySD Model object
283-
capture_elements: set
283+
capture_elements: list
284284
Which model elements to capture - uses pysafe names.
285285
286286
Returns
@@ -335,7 +335,7 @@ def __create_ds_vars(self, model, capture_elements, time_dim=True):
335335
----------
336336
model: pysd.Model
337337
PySD Model object
338-
capture_elements: set
338+
capture_elements: list
339339
Which model elements to capture - uses pysafe names.
340340
time_dim: bool
341341
Whether to add time as the first dimension for the variable.
@@ -413,7 +413,7 @@ def initialize(self, model, capture_elements):
413413
----------
414414
model: pysd.Model
415415
PySD Model object
416-
capture_elements: set
416+
capture_elements: list
417417
Which model elements to capture - uses pysafe names.
418418
419419
Returns
@@ -432,15 +432,15 @@ def update(self, model, capture_elements):
432432
----------
433433
model: pysd.Model
434434
PySD Model object
435-
capture_elements: set
435+
capture_elements: list
436436
Which model elements to capture - uses pysafe names.
437437
438438
Returns
439439
-------
440440
None
441441
442442
"""
443-
self.ds.at[model.time.round()] = [
443+
self.ds.loc[model.time.round()] = [
444444
getattr(model.components, key)()
445445
for key in capture_elements]
446446

@@ -506,7 +506,7 @@ def add_run_elements(self, model, capture_elements):
506506
----------
507507
model: pysd.Model
508508
PySD Model object
509-
capture_elements: set
509+
capture_elements: list
510510
Which model elements to capture - uses pysafe names.
511511
512512
Returns

tests/pytest_pysd/pytest_output.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test_output_csv(self, fmt, sep, capsys, model, tmp_path):
306306

307307
@pytest.mark.parametrize("model_path", [test_model_look])
308308
def test_dataset_handler_step_setter(self, tmp_path, model):
309-
capture_elements = set()
309+
capture_elements = []
310310
results = tmp_path.joinpath("results.nc")
311311
output = ModelOutput(model, capture_elements, results)
312312

@@ -322,10 +322,10 @@ def test_dataset_handler_step_setter(self, tmp_path, model):
322322
def test_make_flat_df(self):
323323

324324
df = pd.DataFrame(index=[1], columns=['elem1'])
325-
df.at[1] = [xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
326-
{'Dim1': ['A', 'B', 'C'],
327-
'Dim2': ['D', 'E', 'F']},
328-
dims=['Dim1', 'Dim2'])]
325+
df.loc[1] = [xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
326+
{'Dim1': ['A', 'B', 'C'],
327+
'Dim2': ['D', 'E', 'F']},
328+
dims=['Dim1', 'Dim2'])]
329329

330330
expected = pd.DataFrame(index=[1], data={'Elem1[B,F]': 6.})
331331

@@ -341,7 +341,7 @@ def test_make_flat_df(self):
341341
def test_make_flat_df_0dxarray(self):
342342

343343
df = pd.DataFrame(index=[1], columns=['elem1'])
344-
df.at[1] = [xr.DataArray(5)]
344+
df.loc[1] = [xr.DataArray(5)]
345345

346346
expected = pd.DataFrame(index=[1], data={'Elem1': 5.})
347347

@@ -357,10 +357,10 @@ def test_make_flat_df_0dxarray(self):
357357
def test_make_flat_df_nosubs(self):
358358

359359
df = pd.DataFrame(index=[1], columns=['elem1', 'elem2'])
360-
df.at[1] = [25, 13]
360+
df.loc[1] = [25, 13]
361361

362362
expected = pd.DataFrame(index=[1], columns=['Elem1', 'Elem2'])
363-
expected.at[1] = [25, 13]
363+
expected.loc[1] = [25, 13]
364364

365365
return_addresses = {'Elem1': ('elem1', {}),
366366
'Elem2': ('elem2', {})}
@@ -378,24 +378,24 @@ def test_make_flat_df_return_array(self):
378378
the simulation dictionary. in this case, we can't force to float..."""
379379

380380
df = pd.DataFrame(index=[1], columns=['elem1', 'elem2'])
381-
df.at[1] = [xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
382-
{'Dim1': ['A', 'B', 'C'],
383-
'Dim2': ['D', 'E', 'F']},
384-
dims=['Dim1', 'Dim2']),
385-
xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
386-
{'Dim1': ['A', 'B', 'C'],
387-
'Dim2': ['D', 'E', 'F']},
388-
dims=['Dim1', 'Dim2'])]
381+
df.loc[1] = [xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
382+
{'Dim1': ['A', 'B', 'C'],
383+
'Dim2': ['D', 'E', 'F']},
384+
dims=['Dim1', 'Dim2']),
385+
xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
386+
{'Dim1': ['A', 'B', 'C'],
387+
'Dim2': ['D', 'E', 'F']},
388+
dims=['Dim1', 'Dim2'])]
389389

390390
expected = pd.DataFrame(index=[1], columns=['Elem1[A, Dim2]', 'Elem2'])
391-
expected.at[1] = [xr.DataArray([[1, 2, 3]],
392-
{'Dim1': ['A'],
393-
'Dim2': ['D', 'E', 'F']},
394-
dims=['Dim1', 'Dim2']),
395-
xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
396-
{'Dim1': ['A', 'B', 'C'],
397-
'Dim2': ['D', 'E', 'F']},
398-
dims=['Dim1', 'Dim2'])]
391+
expected.loc[1] = [xr.DataArray([[1, 2, 3]],
392+
{'Dim1': ['A'],
393+
'Dim2': ['D', 'E', 'F']},
394+
dims=['Dim1', 'Dim2']),
395+
xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
396+
{'Dim1': ['A', 'B', 'C'],
397+
'Dim2': ['D', 'E', 'F']},
398+
dims=['Dim1', 'Dim2'])]
399399

400400
return_addresses = {
401401
'Elem1[A, Dim2]': ('elem1', {'Dim1': ['A'],
@@ -414,14 +414,14 @@ def test_make_flat_df_return_array(self):
414414
def test_make_flat_df_flatten(self):
415415

416416
df = pd.DataFrame(index=[1], columns=['elem1', 'elem2'])
417-
df.at[1] = [xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
418-
{'Dim1': ['A', 'B', 'C'],
419-
'Dim2': ['D', 'E', 'F']},
420-
dims=['Dim1', 'Dim2']),
421-
xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
422-
{'Dim1': ['A', 'B', 'C'],
423-
'Dim2': ['D', 'E', 'F']},
424-
dims=['Dim1', 'Dim2'])]
417+
df.loc[1] = [xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
418+
{'Dim1': ['A', 'B', 'C'],
419+
'Dim2': ['D', 'E', 'F']},
420+
dims=['Dim1', 'Dim2']),
421+
xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
422+
{'Dim1': ['A', 'B', 'C'],
423+
'Dim2': ['D', 'E', 'F']},
424+
dims=['Dim1', 'Dim2'])]
425425

426426
expected = pd.DataFrame(index=[1], columns=[
427427
'Elem1[A,D]',
@@ -437,7 +437,7 @@ def test_make_flat_df_flatten(self):
437437
'Elem2[C,E]',
438438
'Elem2[C,F]'])
439439

440-
expected.at[1] = [1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9]
440+
expected.loc[1] = [1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9]
441441

442442
return_addresses = {
443443
'Elem1[A,Dim2]': ('elem1', {'Dim1': ['A'],
@@ -456,7 +456,7 @@ def test_make_flat_df_flatten(self):
456456
def test_make_flat_df_flatten_transposed(self):
457457

458458
df = pd.DataFrame(index=[1], columns=['elem2'])
459-
df.at[1] = [
459+
df.loc[1] = [
460460
xr.DataArray(
461461
[[1, 4, 7], [2, 5, 8], [3, 6, 9]],
462462
{'Dim2': ['D', 'E', 'F'], 'Dim1': ['A', 'B', 'C']},
@@ -475,7 +475,7 @@ def test_make_flat_df_flatten_transposed(self):
475475
'Elem2[C,E]',
476476
'Elem2[C,F]'])
477477

478-
expected.at[1] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
478+
expected.loc[1] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
479479

480480
return_addresses = {
481481
'Elem2': ('elem2', {})}

tests/pytest_pysd/pytest_pysd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ def test__integrate(self, tmp_path, model):
12111211
from pysd.py_backend.model import ModelOutput
12121212
# TODO: think through a stronger test here...
12131213
model.time.add_return_timestamps(list(range(0, 5, 2)))
1214-
capture_elements = {'teacup_temperature'}
1214+
capture_elements = ['teacup_temperature']
12151215

12161216
out = ModelOutput(model, capture_elements, None)
12171217
model._integrate(out)

0 commit comments

Comments
 (0)