Fix Table primary key propagation and TimeSeries index preservation - #20359
Closed
CAOShurong wants to merge 3 commits into
Closed
Fix Table primary key propagation and TimeSeries index preservation#20359CAOShurong wants to merge 3 commits into
CAOShurong wants to merge 3 commits into
Conversation
- In Table.__init__, set primary_key to the first copied index if primary_key is None and indices exist. - In Table.__getitem__, preserve primary_key on column slices if the primary key is a subset of the sliced columns. - In TimeSeries.__init__ and BinnedTimeSeries.__init__, remove indices on time / time_bin_start before removing the column so the re-added column starts clean without stale or duplicate indices. - In TimeSeries.add_column and add_columns, ensure the time index and primary_key are verified and added if not present. - Add regression tests covering Table column slice primary_key preservation, Table initialization from indexed column, BinnedTimeSeries column slicing, TimeSeries initialization from existing TimeSeries time or QTable, and downsampling a column-subset TimeSeries. Fixes astropy#20297 Fixes astropy#11704
Contributor
|
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.
|
Contributor
Author
|
pre-commit.ci autofix |
Member
|
Closing:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses primary key loss on sliced/copied tables and index corruption when constructing TimeSeries instances from existing columns or tables.
Following the architectural guidance from @taldcroft in #20354:
Table.__init__: When copied indices are attached from columns butprimary_keyis None,self.primary_keyis initialized to the first copied index (self.indices[0].id), matching the "first index is primary" policy inTable.add_index.Table.__getitem__(column slicing): Preservesprimary_keyon column slices whenself.primary_key and set(self.primary_key) <= set(item). Note that this meanst[['a', 'b']].to_pandas()now properly respects the table's primary key ifais the primary key.TimeSeries.__init__: Callsself.remove_indices("time")beforeself.remove_column("time")so the re-added column starts clean.TimeSeries.add_column/add_columns: Adds a shared helper_check_time_indexthat verifies an index ontimeexists and setsprimary_key = ('time',)if missing. This also preventsfold()from losing the time index when a second index exists on the table.BinnedTimeSeries.__init__: Callsself.remove_indices("time_bin_start")before removing the column, avoiding duplicate index creation ontime_bin_start.astropy/table/index.py: Kept completely untouched and clean.Fixes #20297
Fixes #11704
Checklist for package maintainer(s)
astropy/table/tests/test_index.pyand all 2854 tests inastropy/timeseriespass).docs/changes/table/20359.bugfix.rst,docs/changes/timeseries/20359.bugfix.rst).