

def encode(obj):
    '\n    Data encoder\n    '
    tobj = type(obj)
    if isinstance(obj, Index):
        if isinstance(obj, RangeIndex):
            return {
                'typ': 'range_index',
                'klass': u(obj.__class__.__name__),
                'name': getattr(obj, 'name', None),
                'start': getattr(obj, '_start', None),
                'stop': getattr(obj, '_stop', None),
                'step': getattr(obj, '_step', None),
            }
        elif isinstance(obj, PeriodIndex):
            return {
                'typ': 'period_index',
                'klass': u(obj.__class__.__name__),
                'name': getattr(obj, 'name', None),
                'freq': u_safe(getattr(obj, 'freqstr', None)),
                'dtype': u(obj.dtype.name),
                'data': convert(obj.asi8),
                'compress': compressor,
            }
        elif isinstance(obj, DatetimeIndex):
            tz = getattr(obj, 'tz', None)
            if (tz is not None):
                tz = u(tz.zone)
                obj = obj.tz_convert('UTC')
            return {
                'typ': 'datetime_index',
                'klass': u(obj.__class__.__name__),
                'name': getattr(obj, 'name', None),
                'dtype': u(obj.dtype.name),
                'data': convert(obj.asi8),
                'freq': u_safe(getattr(obj, 'freqstr', None)),
                'tz': tz,
                'compress': compressor,
            }
        elif isinstance(obj, MultiIndex):
            return {
                'typ': 'multi_index',
                'klass': u(obj.__class__.__name__),
                'names': getattr(obj, 'names', None),
                'dtype': u(obj.dtype.name),
                'data': convert(obj.values),
                'compress': compressor,
            }
        else:
            return {
                'typ': 'index',
                'klass': u(obj.__class__.__name__),
                'name': getattr(obj, 'name', None),
                'dtype': u(obj.dtype.name),
                'data': convert(obj.values),
                'compress': compressor,
            }
    elif isinstance(obj, Categorical):
        return {
            'typ': 'category',
            'klass': u(obj.__class__.__name__),
            'name': getattr(obj, 'name', None),
            'codes': obj.codes,
            'categories': obj.categories,
            'ordered': obj.ordered,
            'compress': compressor,
        }
    elif isinstance(obj, Series):
        if isinstance(obj, SparseSeries):
            raise NotImplementedError('msgpack sparse series is not implemented')
        else:
            return {
                'typ': 'series',
                'klass': u(obj.__class__.__name__),
                'name': getattr(obj, 'name', None),
                'index': obj.index,
                'dtype': u(obj.dtype.name),
                'data': convert(obj.values),
                'compress': compressor,
            }
    elif issubclass(tobj, NDFrame):
        if isinstance(obj, SparseDataFrame):
            raise NotImplementedError('msgpack sparse frame is not implemented')
        elif isinstance(obj, SparsePanel):
            raise NotImplementedError('msgpack sparse frame is not implemented')
        else:
            data = obj._data
            if (not data.is_consolidated()):
                data = data.consolidate()
            return {
                'typ': 'block_manager',
                'klass': u(obj.__class__.__name__),
                'axes': data.axes,
                'blocks': [{
                    'locs': b.mgr_locs.as_array,
                    'values': convert(b.values),
                    'shape': b.values.shape,
                    'dtype': u(b.dtype.name),
                    'klass': u(b.__class__.__name__),
                    'compress': compressor,
                } for b in data.blocks],
            }
    elif isinstance(obj, (datetime, date, np.datetime64, timedelta, np.timedelta64, NaTType)):
        if isinstance(obj, Timestamp):
            tz = obj.tzinfo
            if (tz is not None):
                tz = u(tz.zone)
            offset = obj.offset
            if (offset is not None):
                offset = u(offset.freqstr)
            return {
                'typ': 'timestamp',
                'value': obj.value,
                'offset': offset,
                'tz': tz,
            }
        if isinstance(obj, NaTType):
            return {
                'typ': 'nat',
            }
        elif isinstance(obj, np.timedelta64):
            return {
                'typ': 'timedelta64',
                'data': obj.view('i8'),
            }
        elif isinstance(obj, timedelta):
            return {
                'typ': 'timedelta',
                'data': (obj.days, obj.seconds, obj.microseconds),
            }
        elif isinstance(obj, np.datetime64):
            return {
                'typ': 'datetime64',
                'data': u(str(obj)),
            }
        elif isinstance(data, datetime):
            return {
                'typ': 'datetime',
                'data': u(obj.isoformat()),
            }
        elif isinstance(obj, date):
            return {
                'typ': 'date',
                'data': u(obj.isoformat()),
            }
        raise Exception(('cannot encode this datetimelike object: %s' % obj))
    elif isinstance(obj, Period):
        return {
            'typ': 'period',
            'ordinal': obj.ordinal,
            'freq': u(obj.freq),
        }
    elif isinstance(obj, BlockIndex):
        return {
            'typ': 'block_index',
            'klass': u(obj.__class__.__name__),
            'blocs': obj.blocs,
            'blengths': obj.blengths,
            'length': obj.length,
        }
    elif isinstance(obj, IntIndex):
        return {
            'typ': 'int_index',
            'klass': u(obj.__class__.__name__),
            'indices': obj.indices,
            'length': obj.length,
        }
    elif isinstance(obj, np.ndarray):
        return {
            'typ': 'ndarray',
            'shape': obj.shape,
            'ndim': obj.ndim,
            'dtype': u(obj.dtype.name),
            'data': convert(obj),
            'compress': compressor,
        }
    elif isinstance(obj, np.number):
        if np.iscomplexobj(obj):
            return {
                'typ': 'np_scalar',
                'sub_typ': 'np_complex',
                'dtype': u(obj.dtype.name),
                'real': u(obj.real.__repr__()),
                'imag': u(obj.imag.__repr__()),
            }
        else:
            return {
                'typ': 'np_scalar',
                'dtype': u(obj.dtype.name),
                'data': u(obj.__repr__()),
            }
    elif isinstance(obj, complex):
        return {
            'typ': 'np_complex',
            'real': u(obj.real.__repr__()),
            'imag': u(obj.imag.__repr__()),
        }
    return obj

dataset/ETHPy150Open pydata/pandas/pandas/io/packers.py encode/VarMisuse@1063/1422 `obj`->`data`