bw2io.strategies.csv#
Functions#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Add an empty exchanges section to any dictionary in data that doesn't already have one. |
|
|
Remove any keys whose values are (Unknown). |
|
Convert string values to float or int where possible |
|
Convert boolean-like strings to booleans where possible. |
Reconstruct TemporalDistribution objects from exchange row columns. |
|
|
Convert tuple-like strings to actual tuples. |
Module Contents#
- bw2io.strategies.csv.csv_add_missing_exchanges_section(data)[source]#
Add an empty exchanges section to any dictionary in data that doesn’t already have one.
- Parameters:
data (list of dict) – A list of dictionaries, where each dictionary represents a row of data.
- Returns:
The updated list of dictionaries with an empty exchanges section added to any dictionary that doesn’t already have one.
- Return type:
list[dict]
Examples
>>> data = [ {"name": "John", "age": 30}, {"name": "Alice", "age": 25, "exchanges": []}, {"name": "Bob", "age": 40, "exchanges": [{"name": "NYSE"}]} ] >>> csv_add_missing_exchanges_section(data) [ {"name": "John", "age": 30, "exchanges": []}, {"name": "Alice", "age": 25, "exchanges": []}, {"name": "Bob", "age": 40, "exchanges": [{"name": "NYSE"}]} ]
- bw2io.strategies.csv.csv_drop_unknown(data)[source]#
Remove any keys whose values are (Unknown).
- Parameters:
data (list[dict]) – A list of dictionaries, where each dictionary represents a row of data.
- Returns:
The updated list of dictionaries with (Unknown) values removed from the keys.
- Return type:
list[dict]
Examples
>>> data = [ {"name": "John", "age": 30, "gender": "(Unknown)"}, {"name": "Alice", "age": 25, "gender": "Female"}, {"name": "Bob", "age": 40, "gender": "Male"} ] >>> csv_drop_unknown(data) [ {"name": "Alice", "age": 25, "gender": "Female"}, {"name": "Bob", "age": 40, "gender": "Male"} ]
- bw2io.strategies.csv.csv_numerize(data)[source]#
Convert string values to float or int where possible
- Parameters:
data (list of dict) – A list of datasets.
- Returns:
A list of datasets with string values converted to float or int where possible.
- Return type:
list of dict
Examples
>>> data = [{'amount': '10.0'}, {'exchanges': [{'amount': '20', 'uncertainty type': 'undefined'}]}] >>> csv_numerize(data) [{'amount': 10.0}, {'exchanges': [{'amount': 20, 'uncertainty type': 'undefined'}]}]
- bw2io.strategies.csv.csv_restore_booleans(data)[source]#
Convert boolean-like strings to booleans where possible.
- Parameters:
data (list of dict) – A list of datasets.
- Returns:
A list of datasets with booleans restored.
- Return type:
list of dict
Examples
>>> data = [{'categories': 'category1', 'is_animal': 'true'}, {'exchanges': [{'categories': 'category2', 'amount': '10.0', 'uncertainty type': 'undefined', 'is_biomass': 'False'}]}] >>> csv_restore_booleans(data) [{'categories': 'category1', 'is_animal': True}, {'exchanges': [{'categories': 'category2', 'amount': '10.0', 'uncertainty type': 'undefined', 'is_biomass': False}]}]
- bw2io.strategies.csv.csv_restore_temporal_distributions(data)[source]#
Reconstruct TemporalDistribution objects from exchange row columns.
Accepts both
temporal_distribution(underscore) andtemporal distribution(space) as the key name; the underscore form takes precedence when both are present.Expected exchange fields:
temporal_distribution: one ofdelta/relative/timedelta64,abs/absolute/datetime64,easy_timedelta_distribution(aliases:easy_timedelta,easy_td), oreasy_datetime_distribution(aliases:easy_datetime,easy_dt).date: list/tuple or comma-separated string of offsets (delta) or formatted date strings (abs).value: list/tuple or comma-separated string of floats. Rescaled to sum to 1 if necessary.resolution: numpy time-unit code such asY,M,D,h,m,s. Case is significant form(minutes) vsM(months); all other single-letter codes are case-insensitive.For
easy_timedelta_distribution: alsostart,end,steps,resolution; and optionallytd_kind,td_param.For
easy_datetime_distribution: alsostart,end,steps.
- Raises:
StrategyError – On any validation failure (unknown kind, missing fields, bad values, mismatched lengths, zero-sum amounts).
StrategyError – If
bw_temporalisis not installed and a temporal distribution is encountered.
- bw2io.strategies.csv.csv_restore_tuples(data)[source]#
Convert tuple-like strings to actual tuples.
- Parameters:
data (list of dict) – A list of datasets.
- Returns:
A list of datasets with tuples restored from string.
- Return type:
list of dict
Examples
>>> data = [{'categories': 'category1::category2'}, {'exchanges': [{'categories': 'category3::category4', 'amount': '10.0'}]}] >>> csv_restore_tuples(data) [{'categories': ('category1', 'category2')}, {'exchanges': [{'categories': ('category3', 'category4'), 'amount': '10.0'}]}]