예제 #1
0
def pandas_hook(dct):
	if not isinstance(dct, dict):
		return dct
	if '__pandas_dataframe__' not in dct and '__pandas_series__' not in dct:
		return dct
	if '__pandas_dataframe__' in dct:
		try:
			from pandas import DataFrame
		except ImportError:
			raise NoPandasException('Trying to decode a map which appears to repr esent a pandas data structure, but pandas appears not to be installed.')
		from numpy import dtype, array
		meta = dct.pop('__pandas_dataframe__')
		indx = dct.pop('index') if 'index' in dct else None
		dtypes = dict((colname, dtype(tp)) for colname, tp in zip(meta['column_order'], meta['types']))
		data = OrderedDict()
		for name, col in dct.items():
			data[name] = array(col, dtype=dtypes[name])
		return DataFrame(
			data=data,
			index=indx,
			columns=meta['column_order'],
			# mixed `dtypes` argument not supported, so use duct of numpy arrays
		)
	elif '__pandas_series__' in dct:
		from pandas import Series
		from numpy import dtype, array
		meta = dct.pop('__pandas_series__')
		indx = dct.pop('index') if 'index' in dct else None
		return Series(
			data=dct['data'],
			index=indx,
			name=meta['name'],
			dtype=dtype(meta['type']),
		)
	return dct	# impossible
예제 #2
0
def nopandas_hook(dct):
    if isinstance(dct, dict) and ('__pandas_dataframe__' in dct
                                  or '__pandas_series__' in dct):
        raise NoPandasException((
            'Trying to decode a map which appears to represent a pandas '
            'data structure, but pandas support is not enabled, perhaps it is not installed.'
        ))
    return dct
예제 #3
0
def pandas_hook(dct):
    if '__pandas_dataframe__' in dct or '__pandas_series__' in dct:
        # todo: this is experimental
        if not getattr(pandas_hook, '_warned', False):
            pandas_hook._warned = True
            warning(
                'Pandas loading support in json-tricks is experimental and may change in future versions.'
            )
    if '__pandas_dataframe__' in dct:
        try:
            from pandas import DataFrame
        except ImportError:
            raise NoPandasException(
                'Trying to decode a map which appears to represent a pandas data structure, but pandas appears not to be installed.'
            )
        from numpy import dtype, array
        meta = dct.pop('__pandas_dataframe__')
        indx = dct.pop('index') if 'index' in dct else None
        dtypes = dict(
            (colname, dtype(tp))
            for colname, tp in zip(meta['column_order'], meta['types']))
        data = OrderedDict()
        for name, col in dct.items():
            data[name] = array(col, dtype=dtypes[name])
        return DataFrame(
            data=data,
            index=indx,
            columns=meta['column_order'],
            # mixed `dtypes` argument not supported, so use duct of numpy arrays
        )
    elif '__pandas_series__' in dct:
        from pandas import Series
        from numpy import dtype, array
        meta = dct.pop('__pandas_series__')
        indx = dct.pop('index') if 'index' in dct else None
        return Series(
            data=dct['data'],
            index=indx,
            name=meta['name'],
            dtype=dtype(meta['type']),
        )
    return dct