bw2io.strategies.csv#

Functions#

_coerce_float_list(seq, field, exc)

_coerce_int_list(seq, field, exc)

_is_blank(value)

_normalize_abs_dates(seq, resolution, exc)

_normalize_kind(value)

_normalize_resolution(value, exc)

_parse_sequence(value, field, exc)

csv_add_missing_exchanges_section(data)

Add an empty exchanges section to any dictionary in data that doesn't already have one.

csv_drop_unknown(data)

Remove any keys whose values are (Unknown).

csv_numerize(data)

Convert string values to float or int where possible

csv_restore_booleans(data)

Convert boolean-like strings to booleans where possible.

csv_restore_temporal_distributions(data)

Reconstruct TemporalDistribution objects from exchange row columns.

csv_restore_tuples(data)

Convert tuple-like strings to actual tuples.

Module Contents#

bw2io.strategies.csv._coerce_float_list(seq, field, exc)[source]#
bw2io.strategies.csv._coerce_int_list(seq, field, exc)[source]#
bw2io.strategies.csv._is_blank(value)[source]#
bw2io.strategies.csv._normalize_abs_dates(seq, resolution, exc)[source]#
bw2io.strategies.csv._normalize_kind(value)[source]#
bw2io.strategies.csv._normalize_resolution(value, exc)[source]#
bw2io.strategies.csv._parse_sequence(value, field, exc)[source]#
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) and temporal distribution (space) as the key name; the underscore form takes precedence when both are present.

Expected exchange fields:

  • temporal_distribution: one of delta / relative / timedelta64, abs / absolute / datetime64, easy_timedelta_distribution (aliases: easy_timedelta, easy_td), or easy_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 as Y, M, D, h, m, s. Case is significant for m (minutes) vs M (months); all other single-letter codes are case-insensitive.

  • For easy_timedelta_distribution: also start, end, steps, resolution; and optionally td_kind, td_param.

  • For easy_datetime_distribution: also start, end, steps.

Raises:
  • StrategyError – On any validation failure (unknown kind, missing fields, bad values, mismatched lengths, zero-sum amounts).

  • StrategyError – If bw_temporalis is 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'}]}]