Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate storage of info in serialized columns. #12607

Merged
merged 2 commits into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion astropy/table/serialize.py
Expand Up @@ -134,7 +134,8 @@ def _represent_mixin_as_column(col, name, new_cols, mixin_cols,
# MaskedColumn). For primary data, we attempt to store any info on
# the format, etc., on the column, but not for ancillary data (e.g.,
# no sense to use a float format for a mask).
if data_attr == col.info._represent_as_dict_primary_data:
is_primary = data_attr == col.info._represent_as_dict_primary_data
if is_primary:
new_name = name
new_info = info
else:
Expand All @@ -146,6 +147,10 @@ def _represent_mixin_as_column(col, name, new_cols, mixin_cols,
and np.any(data.mask)) else Column
new_cols.append(col_cls(data, name=new_name, **new_info))
obj_attrs[data_attr] = SerializedColumn({'name': new_name})
if is_primary:
# Don't store info in the __serialized_columns__ dict for this column
# since this is redundant with info stored on the new column.
info = {}
else:
# recurse. This will define obj_attrs[new_name].
_represent_mixin_as_column(data, new_name, new_cols, obj_attrs)
Expand Down
14 changes: 14 additions & 0 deletions astropy/table/tests/test_mixin.py
Expand Up @@ -817,6 +817,20 @@ def test_represent_mixins_as_columns_unit_fix():
serialize.represent_mixins_as_columns(t)


def test_primary_data_column_gets_description():
"""
If the mixin defines a primary data column, that should get the
description, format, etc., so no __info__ should be needed.
"""
t = QTable({'a': [1, 2] * u.m})
t['a'].info.description = 'parrot'
t['a'].info.format = '7.2f'
tser = serialize.represent_mixins_as_columns(t)
assert '__info__' not in tser.meta['__serialized_columns__']['a']
assert tser['a'].format == '7.2f'
assert tser['a'].description == 'parrot'


def test_skycoord_with_velocity():
# Regression test for gh-6447
sc = SkyCoord([1], [2], unit='deg', galcen_v_sun=None)
Expand Down
9 changes: 6 additions & 3 deletions astropy/utils/masked/core.py
Expand Up @@ -352,9 +352,12 @@ def _represent_as_dict(self):
return out

def _construct_from_dict(self, map):
# 'data' may be a Column or a MaskedColumn. In the former case,
# there will also be a 'mask'.
return Masked(map.pop('data').data, mask=map.pop('mask', None))
# Override usual handling, since MaskedNDArray takes shape and buffer
# as input, which is less useful here.
# The map can contain either a MaskedColumn or a Column and a mask.
# Extract the mask for the former case.
map.setdefault('mask', getattr(map['data'], 'mask', False))
return self._parent_cls.from_unmasked(**map)


class MaskedArraySubclassInfo(MaskedInfoBase):
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/table/12607.bugfix.rst
@@ -0,0 +1,2 @@
Avoid duplicate storage of info in serialized columns if the column
used to serialize already can hold that information.