def encode(obj):
tobj = type(obj)<EOL>if isinstance(obj, Index):<EOL><INDENT>if isinstance(obj, RangeIndex):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT:start>': getattr(obj, '<STR_LIT>', None),<EOL>'<STR_LIT>': getattr(obj, '<STR_LIT>', None),<EOL>'<STR_LIT>': getattr(obj, '<STR_LIT>', None)}<EOL><DEDENT>elif isinstance(obj, PeriodIndex):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT>': getattr(obj, '<STR_LIT>', None),<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': convert(obj.asi8),<EOL>'<STR_LIT>': compressor}<EOL><DEDENT>elif isinstance(obj, DatetimeIndex):<EOL><INDENT>tz = getattr(obj, '<STR_LIT>', None)<EOL>if tz is not None:<EOL><INDENT>tz = tz.zone<EOL>obj = obj.tz_convert('<STR_LIT>')<EOL><DEDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': convert(obj.asi8),<EOL>'<STR_LIT>': getattr(obj, '<STR_LIT>', None),<EOL>'<STR_LIT>': tz,<EOL>'<STR_LIT>': compressor}<EOL><DEDENT>elif isinstance(obj, (IntervalIndex, IntervalArray)):<EOL><INDENT>if isinstance(obj, IntervalIndex):<EOL><INDENT>typ = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>typ = '<STR_LIT>'<EOL><DEDENT>return {'<STR_LIT>': typ,<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT:left>': getattr(obj, '<STR_LIT:left>', None),<EOL>'<STR_LIT:right>': getattr(obj, '<STR_LIT:right>', None),<EOL>'<STR_LIT>': getattr(obj, '<STR_LIT>', None)}<EOL><DEDENT>elif isinstance(obj, MultiIndex):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT>': getattr(obj, '<STR_LIT>', None),<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': convert(obj.values),<EOL>'<STR_LIT>': compressor}<EOL><DEDENT>else:<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT:index>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': convert(obj.values),<EOL>'<STR_LIT>': compressor}<EOL><DEDENT><DEDENT>elif isinstance(obj, Categorical):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT>': obj.codes,<EOL>'<STR_LIT>': obj.categories,<EOL>'<STR_LIT>': obj.ordered,<EOL>'<STR_LIT>': compressor}<EOL><DEDENT>elif isinstance(obj, Series):<EOL><INDENT>if isinstance(obj, SparseSeries):<EOL><INDENT>raise NotImplementedError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>else:<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT:name>': getattr(obj, '<STR_LIT:name>', None),<EOL>'<STR_LIT:index>': obj.index,<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': convert(obj.values),<EOL>'<STR_LIT>': compressor}<EOL><DEDENT><DEDENT>elif issubclass(tobj, NDFrame):<EOL><INDENT>if isinstance(obj, SparseDataFrame):<EOL><INDENT>raise NotImplementedError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>else:<EOL><INDENT>data = obj._data<EOL>if not data.is_consolidated():<EOL><INDENT>data = data.consolidate()<EOL><DEDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT>': data.axes,<EOL>'<STR_LIT>': [{'<STR_LIT>': b.mgr_locs.as_array,<EOL>'<STR_LIT>': convert(b.values),<EOL>'<STR_LIT>': b.values.shape,<EOL>'<STR_LIT>': b.dtype.name,<EOL>'<STR_LIT>': b.__class__.__name__,<EOL>'<STR_LIT>': compressor} for b in data.blocks]<EOL>}<EOL><DEDENT><DEDENT>elif isinstance(obj, (datetime, date, np.datetime64, timedelta,<EOL>np.timedelta64)) or obj is NaT:<EOL><INDENT>if isinstance(obj, Timestamp):<EOL><INDENT>tz = obj.tzinfo<EOL>if tz is not None:<EOL><INDENT>tz = tz.zone<EOL><DEDENT>freq = obj.freq<EOL>if freq is not None:<EOL><INDENT>freq = freq.freqstr<EOL><DEDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:value>': obj.value,<EOL>'<STR_LIT>': freq,<EOL>'<STR_LIT>': tz}<EOL><DEDENT>if obj is NaT:<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>'}<EOL><DEDENT>elif isinstance(obj, np.timedelta64):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:data>': obj.view('<STR_LIT>')}<EOL><DEDENT>elif isinstance(obj, timedelta):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:data>': (obj.days, obj.seconds, obj.microseconds)}<EOL><DEDENT>elif isinstance(obj, np.datetime64):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:data>': str(obj)}<EOL><DEDENT>elif isinstance(obj, datetime):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:data>': obj.isoformat()}<EOL><DEDENT>elif isinstance(obj, date):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT:date>',<EOL>'<STR_LIT:data>': obj.isoformat()}<EOL><DEDENT>raise Exception(<EOL>"<STR_LIT>".format(obj=obj))<EOL><DEDENT>elif isinstance(obj, Period):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.ordinal,<EOL>'<STR_LIT>': obj.freqstr}<EOL><DEDENT>elif isinstance(obj, Interval):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:left>': obj.left,<EOL>'<STR_LIT:right>': obj.right,<EOL>'<STR_LIT>': obj.closed}<EOL><DEDENT>elif isinstance(obj, BlockIndex):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT>': obj.blocs,<EOL>'<STR_LIT>': obj.blengths,<EOL>'<STR_LIT>': obj.length}<EOL><DEDENT>elif isinstance(obj, IntIndex):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.__class__.__name__,<EOL>'<STR_LIT>': obj.indices,<EOL>'<STR_LIT>': obj.length}<EOL><DEDENT>elif isinstance(obj, np.ndarray):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.shape,<EOL>'<STR_LIT>': obj.ndim,<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': convert(obj),<EOL>'<STR_LIT>': compressor}<EOL><DEDENT>elif isinstance(obj, np.number):<EOL><INDENT>if np.iscomplexobj(obj):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT>': obj.real.__repr__(),<EOL>'<STR_LIT>': obj.imag.__repr__()}<EOL><DEDENT>else:<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.dtype.name,<EOL>'<STR_LIT:data>': obj.__repr__()}<EOL><DEDENT><DEDENT>elif isinstance(obj, complex):<EOL><INDENT>return {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': obj.real.__repr__(),<EOL>'<STR_LIT>': obj.imag.__repr__()}<EOL><DEDENT>return obj<EOL>
Data encoder