Fix Pandas 3.x compatibility, float-casting, and test discovery gap for USCensusPEP_Sex - #2142
Fix Pandas 3.x compatibility, float-casting, and test discovery gap for USCensusPEP_Sex#2142rohitkumarbhagat wants to merge 5 commits into
Conversation
- 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.
There was a problem hiding this comment.
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.
|
/gemini review |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Is there a redefinition or cyclic dependency with this?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
i have refactored code to lazy load the flags, to avoid cases where redeclaration conflict.
PR Description
Context & Problem
The scheduled execution for the
USCensusPEP_Seximport failed in production due to the following reasons:pd.read_table(..., delim_whitespace=True). This keyword was completely removed in Pandas 3.x, causing the pipeline to crash in the production environment.scripts/us_census/pep/andscripts/us_census/pep/us_pep_sex/were missing package-initialization__init__.pyfiles, masking errors.NaNvalues, forcing year and FIPS codes to be cast to floats (resulting in malformed values like1989.0andgeoId/1001.0in the CSV outputs).Solution & Changes
Pandas 3.x Compatibility:
delim_whitespace=Truewithsep=r'\s+'inscripts/us_census/pep/us_pep_sex/process.py.absl.flagsdefinitions inprocess.pyfrom duplicate definition crashes during test discovery.Data Type and Format Correctness:
int64. This fixes the malformed1989.0years andgeoId/1001.0county codes.Observationcolumn explicitly toint64to prevent.0suffixes in the final CSV file.CI/CD Enablement & Code Cleanup:
__init__.pyfiles in both PEP directory levels to ensure unit tests are auto-discovered and executed by CI.annual_populationunit testpreprocess_test.pyto write outputs to a temporary directory rather than writing directly into the local git workspace.Verification
OK).