Skip to content

Commit 0cce20c

Browse files
rirzetaldcroft
authored andcommitted
Make Table inherit primary_key attribute from its parent Table. Add tests for issue astropy#4672.
1 parent 3c47c10 commit 0cce20c

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎CHANGES.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,9 @@ New Features
14101410

14111411
- ``astropy.table``
14121412

1413+
- Fixed bug where Tables created from existing Table objects were not
1414+
inheriting the ``primary_key`` attribute. [#4672]
1415+
14131416
- ``astropy.time``
14141417

14151418
- ``astropy.units``

‎astropy/table/table.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ def _init_from_table(self, data, names, dtype, n_cols, copy):
675675
table = data # data is really a Table, rename for clarity
676676
self.meta.clear()
677677
self.meta.update(deepcopy(table.meta))
678+
self.primary_key = table.primary_key
678679
cols = list(table.columns.values())
679680

680681
self._init_from_list(cols, names, dtype, n_cols, copy)
@@ -714,6 +715,7 @@ def _new_from_slice(self, slice_):
714715
table = self.__class__(masked=self.masked)
715716
table.meta.clear()
716717
table.meta.update(deepcopy(self.meta))
718+
table.primary_key = self.primary_key
717719
cols = self.columns.values()
718720

719721
newcols = []

‎astropy/table/tests/test_table.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,3 +1711,28 @@ def test_replace_column_qtable():
17111711
assert t.colnames == ['a', 'b']
17121712
assert t['a'].info.meta is None
17131713
assert t['a'].info.format is None
1714+
1715+
def test_primary_key_is_inherited():
1716+
"""Test whether a new Table inherits the primary_key attribute from
1717+
its parent Table. Issue #4672"""
1718+
1719+
t = table.Table([(2, 3, 2, 1), (8, 7, 6, 5)], names=('a', 'b'))
1720+
t.add_index('a')
1721+
original_key = t.primary_key
1722+
1723+
# can't test if tuples are equal, so just check content
1724+
assert original_key[0] is 'a'
1725+
1726+
t2 = t[:]
1727+
t3 = t.copy()
1728+
t4 = table.Table(t)
1729+
1730+
# test whether the reference is the same in the following
1731+
assert original_key == t2.primary_key
1732+
assert original_key == t3.primary_key
1733+
assert original_key == t4.primary_key
1734+
1735+
# just test one element, assume rest are equal if assert passes
1736+
assert t.loc[1] == t2.loc[1]
1737+
assert t.loc[1] == t3.loc[1]
1738+
assert t.loc[1] == t4.loc[1]

0 commit comments

Comments
 (0)