Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/export-source-inputs.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Export recorded IDs and declared observations, carry or uprate inputs for a requested period, and name the supplied formula values omitted from input exports.
1 change: 1 addition & 0 deletions changelog.d/flat-file-datasets.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Load flat files with exact variable names and optional person IDs, preserve population structure through exports, and name missing group membership columns.
1 change: 1 addition & 0 deletions changelog.d/map-categorical-to-groups.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Map text and enums to the value each group's members share, use the first enum member for empty groups, and raise when members disagree.
18 changes: 18 additions & 0 deletions docs/python_api/simulations.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ The `policyengine_core.simulations` module contains the definition of `Simulatio
:show-inheritance:
```

### Exporting flat-file inputs

`Simulation.to_input_dataframe(period=...)` exports stored inputs for the
requested period, all-time inputs, and the inputs the simulation carries or
uprates for that period. It also exports person IDs, group membership and
roles. A group appears in the file if it has at least one member. A formula
that reads another period needs inputs from that period too. Omit `period`
to include inputs from every stored period.

### Mapping text and enums

A group receives the text or enum value its members share. Members with
different values raise a `ValueError`. An empty group receives an empty string
or the first enum member. Enum mapping returns an `EnumArray` by default. Use
`calculate(..., map_to=..., decode_enums=True)` to compare enum labels, such
as `"IA"`. If you need arithmetic on enum codes, convert the source values to
a numeric array before mapping.

## Microsimulation

```{eval-rst}
Expand Down
24 changes: 23 additions & 1 deletion policyengine_core/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,29 @@ def from_file(file_path: str, time_period: str = None):

@staticmethod
def from_dataframe(dataframe: pd.DataFrame, time_period: str = None):
"""Creates a dataset from a DataFrame.
"""Creates a dataset from a person-level DataFrame.

Each row is one person. A column named ``variable__period`` holds that
variable for that period, and a column named ``variable`` holds it for
``time_period``. An exact registered name takes precedence over splitting
at the last ``__``. Variables defined for all time accept any period,
and ``Simulation.to_input_dataframe`` spells theirs ``ETERNITY``.

Each row also says which group the person belongs to, through a
``person_{entity}_id`` or ``{entity}_id`` column for every group entity.
A ``person_id`` column is optional, and rows number people without one.
A group variable reads its value from the first member's row.

Period exports reserve ``person_id__STRUCTURE`` for person labels and
``person_{entity}_id__STRUCTURE`` for group positions. They repeat the
group label in ``{entity}_id__STRUCTURE`` and give each person's role key
in ``person_{entity}_role__STRUCTURE``. These columns take precedence
when building the population. Their values never become variable inputs.
A group needs a member row to appear in the file.

Args:
dataframe (pd.DataFrame): The person-level data.
time_period (str, optional): The period of unsuffixed columns.

Returns:
Dataset: The dataset.
Expand Down
3 changes: 3 additions & 0 deletions policyengine_core/reforms/reform.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ def __init__(self, baseline: TaxBenefitSystem):
# by previously-computed baseline ``ParameterNodeAtInstant`` objects.
self._parameters_at_instant_cache = {}
self.variables = baseline.variables.copy()
self.source_input_variables = (
baseline.source_input_variables | self.source_input_variables
)
self.decomposition_file_path = baseline.decomposition_file_path
self.key = self.__class__.__name__
if not hasattr(self, "apply"):
Expand Down
Loading
Loading