def to_datetime(date_key):
        '''
        Extract the first date from 'key' matching YYYY-MM-DD
        or YYYY-MM, and convert to datetime.
        '''
        match = re.search(r'\d{4}-\d{2}(-\d{2})?', date_key)
        formatter = '%Y-%m'
        if len(match.group()) == 10:
            formatter += '-%d'
        return datetime.strptime(
            match.group(), formatter).replace(tzinfo=pytz.UTC)
def to_datetime ( date_key ) : match = re . search ( r'\d{4}-\d{2}(-\d{2})?' , date_key ) formatter = '%Y-%m' if len ( match . group ( ) ) == 10 : formatter += '-%d' return datetime . strptime ( match . group ( ) , formatter ) . replace ( tzinfo = pytz . UTC )
Extract the first date from 'key' matching YYYY-MM-DD
        or YYYY-MM, and convert to datetime.