Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix Table primary key propagation and TimeSeries index preservation - #20359

Closed
CAOShurong wants to merge 3 commits into
astropy:mainfrom
CAOShurong:fix-table-primary-key-timeseries
Closed

Fix Table primary key propagation and TimeSeries index preservation#20359
CAOShurong wants to merge 3 commits into
astropy:mainfrom
CAOShurong:fix-table-primary-key-timeseries

Conversation

@CAOShurong

@CAOShurong CAOShurong commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

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:

  1. Table.__init__: When copied indices are attached from columns but primary_key is None, self.primary_key is initialized to the first copied index (self.indices[0].id), matching the "first index is primary" policy in Table.add_index.
  2. Table.__getitem__ (column slicing): Preserves primary_key on column slices when self.primary_key and set(self.primary_key) <= set(item). Note that this means t[['a', 'b']].to_pandas() now properly respects the table's primary key if a is the primary key.
  3. TimeSeries.__init__: Calls self.remove_indices("time") before self.remove_column("time") so the re-added column starts clean.
  4. TimeSeries.add_column / add_columns: Adds a shared helper _check_time_index that verifies an index on time exists and sets primary_key = ('time',) if missing. This also prevents fold() from losing the time index when a second index exists on the table.
  5. BinnedTimeSeries.__init__: Calls self.remove_indices("time_bin_start") before removing the column, avoiding duplicate index creation on time_bin_start.
  6. astropy/table/index.py: Kept completely untouched and clean.

Fixes #20297
Fixes #11704

Checklist for package maintainer(s)

  • Changes have been thoroughly tested locally (all 594 tests in astropy/table/tests/test_index.py and all 2854 tests in astropy/timeseries pass).
  • Regression tests added for Table column slice primary key preservation, Table init from indexed column, BinnedTimeSeries single index, and TimeSeries init from existing TimeSeries time or QTable.
  • Changelog fragments added (docs/changes/table/20359.bugfix.rst, docs/changes/timeseries/20359.bugfix.rst).

- 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
@github-actions

Copy link
Copy Markdown
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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@CAOShurong

Copy link
Copy Markdown
Contributor Author

pre-commit.ci autofix

@pllim pllim added this to the v8.1.0 milestone Sep 10, 2026
@taldcroft

Copy link
Copy Markdown
Member

Closing:

@taldcroft taldcroft closed this Sep 11, 2026
@pllim pllim added the invalid label Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants