r"""Encode all unicode/str objects in a dataframe in the encoding indicated (as a fun attribute)

    similar to to_ascii, but doesn't return a None, even when it fails.
    >>> encode(u'Is 2013 a year or a code point for "\u2013"?')
    b'Is 2013 a year or a code point for "\xe2\x80\x93"?'
    >>> print(u'Is 2013 a year or a code point for "\u2013"?')
    Is 2013 a year or a code point for "–"?

def encode(obj):
    r"""Encode all unicode/str objects in a dataframe in the encoding indicated (as a fun attribute)

    similar to to_ascii, but doesn't return a None, even when it fails.
    >>> encode(u'Is 2013 a year or a code point for "\u2013"?')
    b'Is 2013 a year or a code point for "\xe2\x80\x93"?'
    >>> print(u'Is 2013 a year or a code point for "\u2013"?')
    Is 2013 a year or a code point for "–"?
    """
    try:
        return obj.encode(encode.encoding)
    except AttributeError:
        pass
    except UnicodeDecodeError:
        logger.warning('Problem with byte sequence of type {}.'.format(type(obj)))
        # TODO: Check PG for the proper encoding and fix Django ORM settings so that unicode can be UTF-8 encoded!
        return str('').join([c for c in obj if c < MAX_CHR])
    # TODO: encode sequences of strings and dataframes of strings
    return obj