-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
54 lines (39 loc) · 1.29 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
"""
"""
import os
import pandas as pd
from oemof.tabular.datapackage.building import read_elements, write_elements, \
read_sequences, write_sequences
def update_element(resource, element, directory="data/elements"):
""" Update single element entry.
Parameters
----------
resource : str
Resource name.
element : pd.Series
Element. Series name used as column name. (!)
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.update.html
"""
elements = read_elements(resource, directory=directory)
elements = elements.T.copy()
elements.update(element)
write_elements(resource, elements.T, directory=directory, replace=True)
return None
def update_field(resource, label, field, func, directory='data/elements'):
""" Update single field value by applying func
Parameters
----------
resource: str
Resource name.
label: str
Unique elment identifier.
field: str
Field entry to be changed.
func: function
Function to be applied on field entry.
"""
element = read_elements(resource, directory=directory).loc[label, :]
element[field] = func(element[field])
update_element(resource, element, directory=directory)
return None