Skip to content

Commit

Permalink
Merge pull request #4930 from taldcroft/tablepkey
Browse files Browse the repository at this point in the history
Make Table inherit primary_key attribute from its parent Table.
  • Loading branch information
taldcroft committed May 16, 2016
2 parents 170c4b6 + 0cce20c commit 36ed826
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -1423,6 +1423,9 @@ New Features

- ``astropy.table``

- Fixed bug where Tables created from existing Table objects were not
inheriting the ``primary_key`` attribute. [#4672]

- ``astropy.time``

- ``astropy.units``
Expand Down
2 changes: 2 additions & 0 deletions astropy/table/table.py
Expand Up @@ -675,6 +675,7 @@ def _init_from_table(self, data, names, dtype, n_cols, copy):
table = data # data is really a Table, rename for clarity
self.meta.clear()
self.meta.update(deepcopy(table.meta))
self.primary_key = table.primary_key
cols = list(table.columns.values())

self._init_from_list(cols, names, dtype, n_cols, copy)
Expand Down Expand Up @@ -714,6 +715,7 @@ def _new_from_slice(self, slice_):
table = self.__class__(masked=self.masked)
table.meta.clear()
table.meta.update(deepcopy(self.meta))
table.primary_key = self.primary_key
cols = self.columns.values()

newcols = []
Expand Down
25 changes: 25 additions & 0 deletions astropy/table/tests/test_table.py
Expand Up @@ -1711,3 +1711,28 @@ def test_replace_column_qtable():
assert t.colnames == ['a', 'b']
assert t['a'].info.meta is None
assert t['a'].info.format is None

def test_primary_key_is_inherited():
"""Test whether a new Table inherits the primary_key attribute from
its parent Table. Issue #4672"""

t = table.Table([(2, 3, 2, 1), (8, 7, 6, 5)], names=('a', 'b'))
t.add_index('a')
original_key = t.primary_key

# can't test if tuples are equal, so just check content
assert original_key[0] is 'a'

t2 = t[:]
t3 = t.copy()
t4 = table.Table(t)

# test whether the reference is the same in the following
assert original_key == t2.primary_key
assert original_key == t3.primary_key
assert original_key == t4.primary_key

# just test one element, assume rest are equal if assert passes
assert t.loc[1] == t2.loc[1]
assert t.loc[1] == t3.loc[1]
assert t.loc[1] == t4.loc[1]

0 comments on commit 36ed826

Please sign in to comment.