def dataframe_to_serialized_dict(frame): block_manager = frame._data blocks = [] axes = [ax for ax in block_manager.axes] for block in block_manager.blocks: values = block.values block_data = {} if _pandas_api.is_datetimetz(values.dtype): block_data['timezone'] = pa.lib.tzinfo_to_string(values.tz) if hasattr(values, 'values'): values = values.values elif _pandas_api.is_categorical(values): block_data.update(dictionary=values.categories, ordered=values.ordered) values = values.codes block_data.update( placement=block.mgr_locs.as_array, block=values ) # If we are dealing with an object array, pickle it instead. if values.dtype == np.dtype(object): block_data['object'] = None block_data['block'] = builtin_pickle.dumps( values, protocol=builtin_pickle.HIGHEST_PROTOCOL) blocks.append(block_data) return { 'blocks': blocks, 'axes': axes }
def dataframe_to_serialized_dict(frame): import pandas.core.internals as _int block_manager = frame._data blocks = [] axes = [ax for ax in block_manager.axes] for block in block_manager.blocks: values = block.values block_data = {} if isinstance(block, _int.DatetimeTZBlock): block_data['timezone'] = pa.lib.tzinfo_to_string(values.tz) if hasattr(values, 'values'): values = values.values elif isinstance(block, _int.CategoricalBlock): block_data.update(dictionary=values.categories, ordered=values.ordered) values = values.codes block_data.update(placement=block.mgr_locs.as_array, block=values) # If we are dealing with an object array, pickle it instead. Note that # we do not use isinstance here because _int.CategoricalBlock is a # subclass of _int.ObjectBlock. if type(block) == _int.ObjectBlock: block_data['object'] = None block_data['block'] = builtin_pickle.dumps( values, protocol=builtin_pickle.HIGHEST_PROTOCOL) blocks.append(block_data) return {'blocks': blocks, 'axes': axes}
def _pickle_to_buffer(x): pickled = builtin_pickle.dumps(x, protocol=builtin_pickle.HIGHEST_PROTOCOL) return py_buffer(pickled)