from datetime import datetime


def chart_grouping_as_date(value):
    """Transforms a string YYYYMM or YYYY in a date object"""
    value = str(value)
    for format in ('%Y', '%Y%m'):
        try:
            return datetime.strptime(str(value), format).date()
        except ValueError:
            pass