Skip to content
Open
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
40 changes: 32 additions & 8 deletions src/soep_preparation/clean_modules/biobirth.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,52 @@ def clean(raw_data: pd.DataFrame) -> pd.DataFrame:
"tmp_p_id_child",
"tmp_birth_month_child",
]
# We remove rows that are completely empty that is,
# all non-existent children of the potential enumeration are dropped
# (individual with 2 children only takes two rows in the final data)
tmp_long = (
tmp_long = drop_unlisted_children(
pd.wide_to_long(
wide,
stubnames=prev_wide_variables,
i=["hh_id_original", "p_id"],
j="tmp_child_number",
sep="_",
)
.dropna(subset=prev_wide_variables, how="all")
.reset_index()
).reset_index(),
child_variables=prev_wide_variables,
)

long = pd.DataFrame()
long["hh_id_original"] = tmp_long["hh_id_original"]
long["p_id"] = tmp_long["p_id"]
long["number_of_children"] = tmp_long["tmp_number_of_children"]
long["child_number"] = tmp_long["tmp_child_number"]
long["child_number"] = apply_smallest_int_dtype(tmp_long["tmp_child_number"])
long["p_id_child"] = tmp_long["tmp_p_id_child"]
long["birth_year_child"] = tmp_long["tmp_birth_year_child"]
long["birth_month_child"] = tmp_long["tmp_birth_month_child"]

return long


def drop_unlisted_children(
long: pd.DataFrame, child_variables: list[str]
) -> pd.DataFrame:
"""Drop the enumeration slots without a child.

A person with two children keeps two rows. A person who lists no child, the
childless among them, keeps one row with the child variables and the child
number missing, so that their number of children survives.

Args:
long: The enumeration slots, one row per person and potential child.
child_variables: The variables describing a child.

Returns:
One row per listed child, and one per person without a listed child.
"""
listed = long[child_variables].notna().any(axis=1)
person = [long["hh_id_original"], long["p_id"]]
without_listed_child = ~listed.groupby(person, dropna=False).transform("any")
first_slot = long["tmp_child_number"] == long.groupby(person, dropna=False)[
"tmp_child_number"
].transform("min")
out = long.assign(
tmp_child_number=long["tmp_child_number"].astype("Int64").where(listed)
)
return out.loc[listed | (without_listed_child & first_slot)].reset_index(drop=True)
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ building_year_hh_min:
- 2023
- 2024
child_number:
dtype: int64
dtype: int8[pyarrow]
module: biobirth
reference: time_invariant
survey_years: null
Expand Down
49 changes: 49 additions & 0 deletions tests/clean_modules/test_biobirth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Tests for the biobirth cleaning module."""

import pandas as pd

from soep_preparation.clean_modules.biobirth import drop_unlisted_children

CHILD_VARIABLES = ["tmp_birth_year_child", "tmp_p_id_child"]
PARENT, CHILDLESS = 10, 20


def _long() -> pd.DataFrame:
"""Two enumeration slots for a mother of one child and a childless person."""
return pd.DataFrame(
{
"hh_id_original": [1, 1, 2, 2],
"p_id": [PARENT, PARENT, CHILDLESS, CHILDLESS],
"tmp_child_number": [1, 2, 1, 2],
"tmp_number_of_children": [1, 1, 0, 0],
"tmp_birth_year_child": [1980, None, None, None],
"tmp_p_id_child": [11, None, None, None],
}
)


def test_drop_unlisted_children_keeps_one_row_per_listed_child() -> None:
"""A parent keeps the slots holding a child and no others."""
out = drop_unlisted_children(_long(), child_variables=CHILD_VARIABLES)
assert out.loc[out["p_id"] == PARENT, "tmp_birth_year_child"].tolist() == [1980]


def test_drop_unlisted_children_keeps_a_person_without_children() -> None:
"""A person who lists no child keeps one row with their number of children."""
out = drop_unlisted_children(_long(), child_variables=CHILD_VARIABLES)
assert out.loc[out["p_id"] == CHILDLESS, "tmp_number_of_children"].tolist() == [0]


def test_drop_unlisted_children_marks_the_child_number_missing_without_children() -> (
None
):
"""The row of a person without a listed child carries no child number."""
out = drop_unlisted_children(_long(), child_variables=CHILD_VARIABLES)
assert out.loc[out["p_id"] == CHILDLESS, "tmp_child_number"].isna().all()


def test_drop_unlisted_children_keeps_persons_without_a_household_id() -> None:
"""A missing original household id does not drop a person's rows."""
long = _long().assign(hh_id_original=pd.array([1, 1, None, None], dtype="Int64"))
out = drop_unlisted_children(long, child_variables=CHILD_VARIABLES)
assert out["p_id"].tolist() == [PARENT, CHILDLESS]
Loading