Let `Index._from_column` forward subclass-specific kwargs (e.g. `freq`) to avoid dtype-specialized index construction
- Dominant language
- C++
- Stars
- 9.8k
- Forks
- 1.1k
- Avg merge
- 3d 6m
- Merged PRs (30d)
- 278
Description
### Summary
`Index._from_column` infers the index subclass from the column's dtype, but it does not accept subclass specific construction arguments. As a result, call sites that need to pass something like `freq` to a `DatetimeIndex` have to special case the construction by dtype, e.g. in `python/cudf/cudf/core/dataframe.py`:
```python
if freq is not None and data_to_add[0].dtype.kind == "M":
# Preserve the freq of a DatetimeIndex passed as the key.
idx = cudf.DatetimeIndex._from_column(
data_to_add[0], name=names[0], freq=freq
)
else:
idx = Index._from_column(data_to_add[0], name=names[0])
```
### Proposal
Let `Index._from_column` accept and forward arbitrary extra keyword arguments to the resolved subclass's `_from_column` implementation (each subclass ignores or validates the kwargs it does not use). Then a single `Index._from_column(col, name=..., freq=...)` call works regardless of dtype, and we can drop the dtype based branching.
### Motivation
Losing frequency information (and similar subclass specific metadata) when constructing an index through the generic path has been a recurring source of bugs across several places. Centralizing the forwarding in `Index._from_column` would remove the duplicated, type specialized construction code and make freq preservation the default rather than something each call site has to remember.
### References
Suggested by @vyasr in review of PR #22914: https://github.com/rapidsai/cudf/pull/22914#discussion_r3493360104
Contributor guide
Research direction
Start with Index._from_column and the dtype-specific call site in python/cudf/cudf/core/dataframe.py. Inspect the resolved index subclasses to see how construction kwargs such as freq are handled, then verify that one generic call preserves DatetimeIndex frequency and that the dtype-based branch can be removed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100