BUG: read_csv dtype="category" now infers numeric categories - #7
Conversation
Previously, the c and python parsers produced string categories for numeric columns (e.g., categories=['1', '2'] with object dtype), while the pyarrow engine correctly inferred numeric types. Now all engines consistently infer proper types for categories. closes #56044 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Use explicit dtype mapping instead of accessing .numpy_dtype on a union type that the type checkers can't narrow. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…type_backend is defined The block referenced `dtype_backend` inside `_normalize_timezone_dtypes`, which only takes `df`. Every pyarrow-backed IO path hit a NameError at runtime, breaking Type Checking, Doctests, Doc Build, and pyarrow IO tests. Moved the block into `_post_convert_dtypes` (after the astype), where `dtype_backend` is already a parameter. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…read_csv dtype="category" - Merge categories when distinct strings convert to the same number (e.g. "1" and "1.0") instead of raising on duplicate categories - Skip numeric inference when thousands/decimal are set, since to_numeric is unaware of those options - Only downcast IntegerDtype categories that the pyarrow workaround introduced, not categories the user explicitly requested; restrict the downcast to read_csv (dtype is not None) and make it safe for duplicate column labels - Add tests for the above plus large-integer behavior - Move the whatsnew entry to the notable bug fixes section Co-Authored-By: Claude Fable 5 <[email protected]>
The c and python engines produced string categories for boolean columns under dtype="category" while the pyarrow engine inferred bool categories. All engines now infer boolean categories, mirroring each engine's non-categorical inference (numeric first, then boolean). GH#56044 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ory" - Defer category-dtype inference in low_memory mode until chunks are concatenated; per-chunk inference could produce differing category dtypes, breaking union_categoricals - Keep string categories when to_numeric turns "" into NaN (na_filter=False / keep_default_na=False) - Keep object dtype for empty inferred categories (all-NA columns) Co-Authored-By: Claude Fable 5 <[email protected]>
… docs - factor _from_converted_categories out of _from_inferred_categories so the low_memory deferred path runs to_numeric once on the unioned categories - update stale io.rst note obsoleted by GH#56044 inference - pin dtype_backend and QUOTE_NONNUMERIC category behavior in tests; scope the whatsnew claim to the default dtype_backend Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
…tegory" With dtype_backend="pyarrow" the python engine's values arrive as ArrowDtype strings, whose kind is "U" rather than "O", so the guard in _maybe_convert_categories bailed and no inference was applied at all. Categories now infer to int64[pyarrow], matching a non-categorical read. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
# Conflicts: # doc/source/whatsnew/v3.1.0.rst # pandas/_libs/parsers.pyx
…tegories unsorted With the c and python engines, a CategoricalDtype specifying ordered=True but not categories silently produced an unordered result, while the pyarrow engine honored it. Separately, low_memory=True (the c engine default) left string categories in the order the file's buffer chunks happened to produce them, rather than sorted as every other path gives. Also restore the pre-GH#56044 defaults of Categorical._from_inferred_categories so callers other than the parsers are unaffected, and collapse _from_converted_categories to a single recode pass. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Warning Review limit reachedNext included review available in 50 minutes. View limit detailsLimit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (13)
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthrough
ChangesCategorical dtype inference
Estimated code review effort: 4 (Complex) | ~45 minutes Merge Risk: ⚪ Minimal · up to The change updates CSV categorical dtype inference and adds related coverage and documentation; no actionable merge-blocking risk remains beyond normal checks and review. Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant Reader
participant TextReader
participant CategoricalArray
participant CParserWrapper
Reader->>TextReader: parse data with dtype="category"
TextReader->>CategoricalArray: infer numeric or boolean categories
TextReader->>CParserWrapper: provide low-memory categorical chunks
CParserWrapper->>CategoricalArray: reconcile categories after concatenation
CategoricalArray-->>Reader: return categorical columns
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 8.70% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 46 functions across 4 files. (3 skipped: 3 unsupported.) ✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
c83b924 to
5ef188f
Compare
…ed categories Follow-up fixes on top of the GH#56044 inference change: - with quoting=QUOTE_NONNUMERIC the deferred low-memory path now gives float64 categories like the eager path, casting before de-duplication so that values colliding at float64 precision merge into one category - an all-NA chunk no longer breaks union_categoricals when the other chunks inferred a non-object category dtype - to_numeric raises OverflowError for integers too large to represent as float64; such columns keep string categories rather than propagating it - under PANDAS_FUTURE_INFER_STRING=0 the pyarrow engine kept StringDtype categories for ordered categoricals, since CategoricalDtype.__eq__ ignores the categories' dtype when ordered and the astype was a no-op - read_fwf and read_excel coverage for the shared inference Co-Authored-By: Claude Opus 5 <[email protected]>
0eb7bfa to
f4391ca
Compare
# Conflicts: # pandas/_libs/parsers.pyx
abdaebc to
65097c2
Compare
# Conflicts: # pandas/io/parsers/c_parser_wrapper.py
7edc107 to
df7f525
Compare
# Conflicts: # pandas/_libs/parsers.pyx
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
…gine The c tokenizer matches the default True/False spellings with strncasecmp, so a plain read infers bool for "tRuE" while dtype="category" kept string categories. Fold those spellings for the c engine only, after consulting true_values/false_values, which the tokenizer checks first. Also keep the arrow backing for inferred boolean categories, document the low_memory all-NA-chunk crash and the per-chunk inference that chunksize now does, and cover the ordered-categorical fix that reaches every reader built on an Arrow table. Co-Authored-By: Claude Opus 5 <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
The pyarrow engine parsed a categorical with no explicit categories as large_string, so the categories were the raw text from the file instead of the inferred values the other engines now give (GH#56044). This reverses one branch of GH-62242, which aligned the pyarrow engine with the c and python engines as they behaved before this PR changed them. Its dtype=str and dtype=object handling, and every issue it cites, are unaffected. Co-Authored-By: Claude Opus 5 <[email protected]>
# Conflicts: # doc/source/whatsnew/v3.1.0.rst # pandas/io/parsers/c_parser_wrapper.py # pandas/tests/io/excel/test_readers.py # pandas/tests/io/parser/dtypes/test_categorical.py # pandas/tests/io/parser/test_read_fwf.py
Mirror of pandas-dev/pandas#64659. Runs the pandas test suite through dagz.
Summary by CodeRabbit
Bug Fixes
dtype="category"inference for CSV, table, fixed-width, Feather, and Excel readers.Documentation