Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mic-716/Coerce empty strings to np.nan for PO Box addresses #354

Merged
merged 8 commits into from
Dec 1, 2023
Merged
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
16 changes: 12 additions & 4 deletions src/pseudopeople/interface.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union

import numpy as np
import pandas as pd
import yaml
from loguru import logger
from packaging.version import parse
from tqdm import tqdm

from pseudopeople import __version__ as psp_version
from pseudopeople.configuration import get_configuration
from pseudopeople.configuration.validator import validate_noise_level_proportions
from pseudopeople.constants import paths
from pseudopeople.constants.metadata import COPY_HOUSEHOLD_MEMBER_COLS, INT_COLUMNS
from pseudopeople.exceptions import DataSourceError
Expand Down Expand Up @@ -96,7 +95,11 @@ def _generate_dataset(

# Known pandas bug: pd.concat does not preserve category dtypes so we coerce
# again after concat (https://github.com/pandas-dev/pandas/issues/51362)
noised_dataset = _coerce_dtypes(noised_dataset, dataset, cleanse_int_cols=True)
noised_dataset = _coerce_dtypes(
noised_dataset,
dataset,
cleanse_int_cols=True,
)

logger.debug("*** Finished ***")

Expand Down Expand Up @@ -136,13 +139,18 @@ def _get_data_changelog_version(changelog):


def _coerce_dtypes(
data: pd.DataFrame, dataset: Dataset, cleanse_int_cols: bool = False
data: pd.DataFrame,
dataset: Dataset,
cleanse_int_cols: bool = False,
) -> pd.DataFrame:
# Coerce dtypes prior to noising to catch issues early as well as
# get most columns away from dtype 'category' and into 'object' (strings)
for col in dataset.columns:
if cleanse_int_cols and col.name in INT_COLUMNS:
data[col.name] = cleanse_integer_columns(data[col.name])
# Coerce empty strings to nans
if cleanse_int_cols and col.name not in INT_COLUMNS:
data[col.name] = data[col.name].replace("", np.nan)
if col.dtype_name != data[col.name].dtype.name:
data[col.name] = data[col.name].astype(col.dtype_name)

Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@ def test_validate_noise_level_proportions(caplog, column, noise_type, noise_leve
Tests that a warning is thrown when a user provides configuration overrides that are higher
than the calculated metadata proportions for that column noise type pairing.
"""

census = DATASETS.get_dataset("decennial_census")
user_filters = [
(census.date_column_name, "==", 2020),
Expand Down