Skip to content

Fix Pandas 3.x compatibility, float-casting, and test discovery gap for USCensusPEP_Sex - #2142

Open
rohitkumarbhagat wants to merge 5 commits into
datacommonsorg:masterfrom
rohitkumarbhagat:pandas3-pep-fix
Open

Fix Pandas 3.x compatibility, float-casting, and test discovery gap for USCensusPEP_Sex#2142
rohitkumarbhagat wants to merge 5 commits into
datacommonsorg:masterfrom
rohitkumarbhagat:pandas3-pep-fix

Conversation

@rohitkumarbhagat

Copy link
Copy Markdown
Contributor

PR Description

Context & Problem

The scheduled execution for the USCensusPEP_Sex import failed in production due to the following reasons:

  1. Pandas 3.x Deprecation Removal: The preprocessing script called pd.read_table(..., delim_whitespace=True). This keyword was completely removed in Pandas 3.x, causing the pipeline to crash in the production environment.
  2. Silent Test Discovery Skipping: Unit tests for this import were silently skipped in CI builds because the directories scripts/us_census/pep/ and scripts/us_census/pep/us_pep_sex/ were missing package-initialization __init__.py files, masking errors.
  3. Float-Casting Bug: Re-enabling the tests revealed a bug where a blank row in county intercensal files introduced NaN values, forcing year and FIPS codes to be cast to floats (resulting in malformed values like 1989.0 and geoId/1001.0 in the CSV outputs).

Solution & Changes

  1. Pandas 3.x Compatibility:

    • Replaced all instances of delim_whitespace=True with sep=r'\s+' in scripts/us_census/pep/us_pep_sex/process.py.
    • Added checks to guard absl.flags definitions in process.py from duplicate definition crashes during test discovery.
  2. Data Type and Format Correctness:

    • Updated the intercensal county parsing function to drop NaN/blank rows immediately and cast year/FIPS columns explicitly to int64. This fixes the malformed 1989.0 years and geoId/1001.0 county codes.
    • Cast the melted Observation column explicitly to int64 to prevent .0 suffixes in the final CSV file.
    • Regenerated the expected unit test CSV, MCF, and TMCF files to align with the correct integer-formatted output.
  3. CI/CD Enablement & Code Cleanup:

    • Created empty package __init__.py files in both PEP directory levels to ensure unit tests are auto-discovered and executed by CI.
    • Refactored the adjacent annual_population unit test preprocess_test.py to write outputs to a temporary directory rather than writing directly into the local git workspace.

Verification

  • All PEP tests auto-discovered and passed successfully (OK).
  • Project linter check passed successfully with zero warnings/errors.

- Replaced delim_whitespace=True with sep=r'\s+' for Pandas 3.x compatibility.
- Fixed float-casting issue in county intercensal data parsing by dropping NaN rows and explicitly casting Year/Observation to int64.
- Added missing __init__.py files in us_census/pep/ to resolve CI/CD test discovery gap.
- Protected absl flags against duplicate definition error during test discovery.
- Refactored annual_population preprocess_test.py to use TemporaryDirectory for output verification.
@rohitkumarbhagat
rohitkumarbhagat requested a review from ajaits August 6, 2026 12:58

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the US Census PEP preprocessing scripts and tests. Key changes include using temporary directories for test outputs, avoiding flag redefinitions, replacing deprecated pandas parameters, and casting data columns to integer types to clean up the output formatting. The reviewer feedback highlights a potential runtime crash when casting the Observation column to int64 if nulls or empty strings are present, suggesting a safer conversion to Int64. Additionally, the reviewer recommends refactoring the test setup in preprocess_test.py from __init__ to setUpClass to prevent redundant execution of heavy setup tasks.

Comment thread scripts/us_census/pep/us_pep_sex/process.py Outdated
Comment thread scripts/us_census/pep/annual_population/preprocess_test.py Outdated
@rohitkumarbhagat

Copy link
Copy Markdown
Contributor Author

/gemini review

@rohitkumarbhagat
rohitkumarbhagat enabled auto-merge (squash) August 6, 2026 13:31

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the PEP (Population Estimates Program) preprocessing scripts and tests. Key changes include refactoring test classes to use standard setUpClass and tearDownClass methods, preventing duplicate flag definitions, replacing deprecated pandas parameters, and ensuring proper integer casting for years, FIPS codes, and observations. The expected test data has been updated to reflect these formatting improvements. There are no review comments, so I have no feedback to provide.

flags.DEFINE_bool(
'is_summary_levels', False,
'Options: True for all summary_levels and False for only 162')
if 'mode' not in flags.FLAGS:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a redefinition or cyclic dependency with this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this prevents a flag redefinition rather than a cyclic dependency. Abseil uses a process-global flag registry, and test discovery imports both annual_population.preprocess and us_pep_sex.process; both define the same mode string flag. Without the guard, whichever module is imported second raises DuplicateFlagError.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have refactored code to lazy load the flags, to avoid cases where redeclaration conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants