fix(#220): PandasAdapter auto time-column detection was unreachable dead code#238
fix(#220): PandasAdapter auto time-column detection was unreachable dead code#238himax12 wants to merge 1 commit intosktime:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes unreachable auto time-column detection in PandasAdapter.load() by correcting an isinstance() check that previously always evaluated as true due to inclusion of pd.Index.
Changes:
- Remove
pd.Indexfrom thedf.indextype check so_detect_time_column()can run for non-datetime/non-range indexes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| elif not isinstance(df.index, (pd.DatetimeIndex, pd.RangeIndex)): | ||
| # Try to detect time column |
There was a problem hiding this comment.
After removing pd.Index from this check, _detect_time_column() will now run when df.index is a pd.PeriodIndex (and other pd.Index subclasses like MultiIndex). Since validate() explicitly treats PeriodIndex as a supported time index, running auto-detection here can unexpectedly overwrite an existing valid time index via df.set_index(time_col). Consider adding pd.PeriodIndex to the tuple (and/or explicitly skipping auto-detection for other already-acceptable index types) so only truly "generic"/non-time-like indexes trigger detection.
| elif not isinstance(df.index, (pd.DatetimeIndex, pd.RangeIndex)): | |
| # Try to detect time column | |
| elif not isinstance(df.index, (pd.DatetimeIndex, pd.RangeIndex, pd.PeriodIndex)): | |
| # Try to detect time column only when there is no already-supported time index |
Summary
The condition isinstance(df.index, (pd.DatetimeIndex, pd.RangeIndex, pd.Index)) was ALWAYS True because pd.Index is the base class of ALL pandas indexes. This made the 'not' always False, making _detect_time_column() unreachable dead code.
The Fix
Removed pd.Index from the tuple:
This allows auto-detection to run when index is not DatetimeIndex or RangeIndex.
Changes
Testing