fix(timeseries): preserve primary_key on sliced tables and fix aggregate_downsample - #20354
fix(timeseries): preserve primary_key on sliced tables and fix aggregate_downsample#20354CAOShurong wants to merge 4 commits into
Conversation
…fix aggregate_downsample (astropy#20297)
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
|
If AI tools were used to develop this pull request, describe the tools including specific model and version, how they were used, and what content is AI generated. |
|
Hi @pllim, Thank you! In accordance with the Astropy AI Usage Policy, here is the disclosure for this PR:
|
|
@CAOShurong - Review verdict from Claude Fable 5.1, which I agree with. This PR has substantial defects and the basic approach needs to be changed. If you'd like to do that then please open a new PR. Review of PR #20354: preserve
|
Fixes #20297
Related to #11704
Description
When slicing a
TimeSeriesor indexedTablein Astropy by column names (e.g.ts_sub = ts["time", "a"]),Table.__getitem__copied indices but droppedprimary_key, leavingts_sub.primary_key = None.Furthermore, in
TimeSeries.add_column(), because the copied index existed inself.indices,len(self.indices)was not 0, preventingself.add_index("time")not running, leavingprimary_keypermanentlyNone.Consequently, operations relying on primary key indexing like
aggregate_downsample(ts_sub, ...)(which callsts.iloc[:]) triggeredTableLoc._get_index_id_and_item(item)withindex_id = self.table.primary_key(None), causingTableIndices.__getitem__(None)to fail with:TypeError: list indices must be integers or slices, not NoneType.This pull request resolves the issue by:
primary_keyinTable.__getitem__whenever all primary key columns are retained in the sliced table.TimeSeries.add_column()andTimeSeries.add_columns(), ensuringprimary_key = ("time",)is properly assigned when"time"is present even if indices were carried over from sliced data.TableLoc._get_index_id_and_item(): whenprimary_keyis not set and exactly one index exists, use that index instead of passingNone.TableIndices.__getitem__(), raising a clearValueError("Table has no primary key or index specified")instead of lettinglist.__getitem__(None)non-informatively fail withTypeError.AI Disclosure
Tests
test_downsample_subset_columns()inastropy/timeseries/tests/test_downsample.py.docs/changes/timeseries/20354.bugfix.rst.