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

Changes needed to support categorical dates #407

Open
jamesrkg opened this issue May 14, 2021 · 3 comments
Open

Changes needed to support categorical dates #407

jamesrkg opened this issue May 14, 2021 · 3 comments

Comments

@jamesrkg
Copy link

Related to #387.

Two changes needed to support categorical dates (as far as I can tell):

  1. Variable._MUTABLE_ATTRIBUTES
  • Add date to set of mutable attributes
class Variable(ReadOnly, DatasetSubvariablesMixin):
    """
    A pycrunch.shoji.Entity wrapper that provides variable-specific methods.
    DatasetSubvariablesMixin provides for subvariable interactions.
    """
    _MUTABLE_ATTRIBUTES = {'name', 'description', 'uniform_basis',
                           'view', 'notes', 'format', 'derived', 'date'}
    _IMMUTABLE_ATTRIBUTES = {'id', 'alias', 'type', 'discarded'}
  1. Variable.add_category:
  • Add date=None kwarg
  • If date is not None, add date to new_category (which I've also separated out in the below example)
    def add_category(self, id, name, numeric_value, missing=False, before_id=False, date=None):
        if self.resource.body['type'] not in CATEGORICAL_TYPES:
            raise TypeError(
                "Variable of type %s do not have categories"
                % self.resource.body.type)

        if self.resource.body.get('derivation'):
            raise TypeError("Cannot add categories on derived variables. Re-derive with the appropriate expression")

        categories = self.resource.body['categories']

        new_category = {
            'id': id,
            'missing': missing,
            'name': name,
            'numeric_value': numeric_value
        }
        if date is not None:
            new_category['date'] = date

        if before_id:
            # only accept int type
            assert isinstance(before_id, int)

            # see if id exist
            try:
                self.categories[before_id]
            except:
                raise AttributeError('before_id not found: {}'.format(before_id))

            new_categories = []
            for category in categories:
                if category['id'] == before_id:
                    new_categories.append(new_category)
                new_categories.append(category)
            categories = new_categories
        else:
            categories.append(new_category)

        resp = self.resource.edit(categories=categories)
        self._reload_variables()
        return resp
@jamesrkg
Copy link
Author

One more exception occurs when attempting to view the categories for a categorical date variable with ds[var].categories. In this case the error is KeyError: 'numeric_value'. I'm not sure what the fix for this is.

@jjdelc
Copy link
Contributor

jjdelc commented Jun 15, 2021

Took care of

  • Support date in Dataset.add_category
  • Handle the KeyError gracefully for non required category arguments

Did not take care of Variable.date that is not an attribute of the variable to make mutable.

@jamesrkg
Copy link
Author

Thanks @jjdelc!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants