<s> """<STR_LIT>""" <EOL> import warnings <EOL> from pandas . compat import lrange , zip <EOL> from pandas import compat <EOL> import numpy as np <EOL> from pandas . core . index import Index , MultiIndex , _ensure_index <EOL> from pandas . core . frame import DataFrame <EOL> from pandas . core . panel import Panel <EOL> from pandas . sparse . frame import SparseDataFrame <EOL> from pandas . util . decorators import deprecate <EOL> import pandas . core . common as com <EOL> import pandas . core . ops as ops <EOL> import pandas . lib as lib <EOL> class SparsePanelAxis ( object ) : <EOL> def __init__ ( self , cache_field , frame_attr ) : <EOL> self . cache_field = cache_field <EOL> self . frame_attr = frame_attr <EOL> def __get__ ( self , obj , type = None ) : <EOL> return getattr ( obj , self . cache_field , None ) <EOL> def __set__ ( self , obj , value ) : <EOL> value = _ensure_index ( value ) <EOL> if isinstance ( value , MultiIndex ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> for v in compat . itervalues ( obj . _frames ) : <EOL> setattr ( v , self . frame_attr , value ) <EOL> setattr ( obj , self . cache_field , value ) <EOL> class SparsePanel ( Panel ) : <EOL> """<STR_LIT>""" <EOL> ndim = <NUM_LIT:3> <EOL> _typ = '<STR_LIT>' <EOL> _subtyp = '<STR_LIT>' <EOL> def __init__ ( self , frames = None , items = None , major_axis = None , <EOL> minor_axis = None , default_fill_value = np . nan , <EOL> default_kind = '<STR_LIT>' , copy = False ) : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , FutureWarning , stacklevel = <NUM_LIT:2> ) <EOL> if frames is None : <EOL> frames = { } <EOL> if isinstance ( frames , np . ndarray ) : <EOL> new_frames = { } <EOL> for item , vals in zip ( items , frames ) : <EOL> new_frames [ item ] = SparseDataFrame ( <EOL> vals , index = major_axis , columns = minor_axis , <EOL> default_fill_value = default_fill_value , <EOL> default_kind = default_kind ) <EOL> frames = new_frames <EOL> if not isinstance ( frames , dict ) : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> type ( frames ) . __name__ ) <EOL> self . default_fill_value = fill_value = default_fill_value <EOL> self . default_kind = kind = default_kind <EOL> if items is None : <EOL> items = Index ( sorted ( frames . keys ( ) ) ) <EOL> items = _ensure_index ( items ) <EOL> ( clean_frames , major_axis , <EOL> minor_axis ) = _convert_frames ( frames , major_axis , minor_axis , <EOL> kind = kind , fill_value = fill_value ) <EOL> self . _frames = clean_frames <EOL> for item in items : <EOL> if item not in clean_frames : <EOL> raise ValueError ( '<STR_LIT>' % item ) <EOL> self . _items = items <EOL> self . major_axis = major_axis <EOL> self . minor_axis = minor_axis <EOL> def _consolidate_inplace ( self ) : <EOL> pass <EOL> def __array_wrap__ ( self , result ) : <EOL> return SparsePanel ( result , items = self . items , <EOL> major_axis = self . major_axis , <EOL> minor_axis = self . minor_axis , <EOL> default_kind = self . default_kind , <EOL> default_fill_value = self . default_fill_value ) <EOL> @ classmethod <EOL> def from_dict ( cls , data ) : <EOL> """<STR_LIT>""" <EOL> return SparsePanel ( data ) <EOL> def to_dense ( self ) : <EOL> """<STR_LIT>""" <EOL> return Panel ( self . values , self . items , self . major_axis , self . minor_axis ) <EOL> def as_matrix ( self ) : <EOL> return self . values <EOL> @ property <EOL> def values ( self ) : <EOL> return np . array ( [ self . _frames [ item ] . values for item in self . items ] ) <EOL> _items = None <EOL> def _get_items ( self ) : <EOL> return self . _items <EOL> def _set_items ( self , new_items ) : <EOL> new_items = _ensure_index ( new_items ) <EOL> if isinstance ( new_items , MultiIndex ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> old_frame_dict = self . _frames <EOL> old_items = self . _items <EOL> self . _frames = dict ( ( new_k , old_frame_dict [ old_k ] ) <EOL> for new_k , old_k in zip ( new_items , old_items ) ) <EOL> self . _items = new_items <EOL> items = property ( fget = _get_items , fset = _set_items ) <EOL> major_axis = SparsePanelAxis ( '<STR_LIT>' , '<STR_LIT:index>' ) <EOL> minor_axis = SparsePanelAxis ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _ixs ( self , i , axis = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> key = self . _get_axis ( axis ) [ i ] <EOL> if com . is_list_like ( key ) : <EOL> return self . reindex ( ** { self . _get_axis_name ( axis ) : key } ) <EOL> return self . xs ( key , axis = axis ) <EOL> def _slice ( self , slobj , axis = <NUM_LIT:0> , kind = None ) : <EOL> """<STR_LIT>""" <EOL> axis = self . _get_axis_name ( axis ) <EOL> index = self . _get_axis ( axis ) <EOL> return self . reindex ( ** { axis : index [ slobj ] } ) <EOL> def _get_item_cache ( self , key ) : <EOL> return self . _frames [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> if isinstance ( value , DataFrame ) : <EOL> value = value . reindex ( index = self . major_axis , <EOL> columns = self . minor_axis ) <EOL> if not isinstance ( value , SparseDataFrame ) : <EOL> value = value . to_sparse ( fill_value = self . default_fill_value , <EOL> kind = self . default_kind ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _frames [ key ] = value <EOL> if key not in self . items : <EOL> self . _items = Index ( list ( self . items ) + [ key ] ) <EOL> def set_value ( self , item , major , minor , value ) : <EOL> """<STR_LIT>""" <EOL> dense = self . to_dense ( ) . set_value ( item , major , minor , value ) <EOL> return dense . to_sparse ( kind = self . default_kind , <EOL> fill_value = self . default_fill_value ) <EOL> def __delitem__ ( self , key ) : <EOL> loc = self . items . get_loc ( key ) <EOL> indices = lrange ( loc ) + lrange ( loc + <NUM_LIT:1> , len ( self . items ) ) <EOL> del self . _frames [ key ] <EOL> self . _items = self . _items . take ( indices ) <EOL> def __getstate__ ( self ) : <EOL> from pandas . io . pickle import _pickle_array <EOL> return ( self . _frames , _pickle_array ( self . items ) , <EOL> _pickle_array ( self . major_axis ) , <EOL> _pickle_array ( self . minor_axis ) , self . default_fill_value , <EOL> self . default_kind ) <EOL> def __setstate__ ( self , state ) : <EOL> frames , items , major , minor , fv , kind = state <EOL> from pandas . io . pickle import _unpickle_array <EOL> self . default_fill_value = fv <EOL> self . default_kind = kind <EOL> self . _items = _ensure_index ( _unpickle_array ( items ) ) <EOL> self . _major_axis = _ensure_index ( _unpickle_array ( major ) ) <EOL> self . _minor_axis = _ensure_index ( _unpickle_array ( minor ) ) <EOL> self . _frames = frames <EOL> def copy ( self , deep = True ) : <EOL> """<STR_LIT>""" <EOL> d = self . _construct_axes_dict ( ) <EOL> if deep : <EOL> new_data = dict ( ( k , v . copy ( deep = True ) ) <EOL> for k , v in compat . iteritems ( self . _frames ) ) <EOL> d = dict ( ( k , v . copy ( deep = True ) ) for k , v in compat . iteritems ( d ) ) <EOL> else : <EOL> new_data = self . _frames . copy ( ) <EOL> d [ '<STR_LIT>' ] = self . default_fill_value <EOL> d [ '<STR_LIT>' ] = self . default_kind <EOL> return SparsePanel ( new_data , ** d ) <EOL> def to_frame ( self , filter_observations = True ) : <EOL> """<STR_LIT>""" <EOL> if not filter_observations : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> I , N , K = self . shape <EOL> counts = np . zeros ( N * K , dtype = int ) <EOL> d_values = { } <EOL> d_indexer = { } <EOL> for item in self . items : <EOL> frame = self [ item ] <EOL> values , major , minor = _stack_sparse_info ( frame ) <EOL> indexer = minor * N + major <EOL> counts . put ( indexer , counts . take ( indexer ) + <NUM_LIT:1> ) <EOL> d_values [ item ] = values <EOL> d_indexer [ item ] = indexer <EOL> mask = counts == I <EOL> values = np . column_stack ( [ d_values [ item ] [ mask . take ( d_indexer [ item ] ) ] <EOL> for item in self . items ] ) <EOL> inds , = mask . nonzero ( ) <EOL> major_labels = inds % N <EOL> minor_labels = inds // N <EOL> index = MultiIndex ( levels = [ self . major_axis , self . minor_axis ] , <EOL> labels = [ major_labels , minor_labels ] , <EOL> verify_integrity = False ) <EOL> df = DataFrame ( values , index = index , columns = self . items ) <EOL> return df . sortlevel ( level = <NUM_LIT:0> ) <EOL> to_long = deprecate ( '<STR_LIT>' , to_frame ) <EOL> toLong = deprecate ( '<STR_LIT>' , to_frame ) <EOL> def reindex ( self , major = None , items = None , minor = None , major_axis = None , <EOL> minor_axis = None , copy = False ) : <EOL> """<STR_LIT>""" <EOL> major = com . _mut_exclusive ( major = major , major_axis = major_axis ) <EOL> minor = com . _mut_exclusive ( minor = minor , minor_axis = minor_axis ) <EOL> if com . _all_none ( items , major , minor ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> major = self . major_axis if major is None else major <EOL> minor = self . minor_axis if minor is None else minor <EOL> if items is not None : <EOL> new_frames = { } <EOL> for item in items : <EOL> if item in self . _frames : <EOL> new_frames [ item ] = self . _frames [ item ] <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> new_frames = self . _frames <EOL> if copy : <EOL> new_frames = dict ( ( k , v . copy ( ) ) <EOL> for k , v in compat . iteritems ( new_frames ) ) <EOL> return SparsePanel ( new_frames , items = items , major_axis = major , <EOL> minor_axis = minor , <EOL> default_fill_value = self . default_fill_value , <EOL> default_kind = self . default_kind ) <EOL> def _combine ( self , other , func , axis = <NUM_LIT:0> ) : <EOL> if isinstance ( other , DataFrame ) : <EOL> return self . _combineFrame ( other , func , axis = axis ) <EOL> elif isinstance ( other , Panel ) : <EOL> return self . _combinePanel ( other , func ) <EOL> elif lib . isscalar ( other ) : <EOL> new_frames = dict ( ( k , func ( v , other ) ) <EOL> for k , v in self . iteritems ( ) ) <EOL> return self . _new_like ( new_frames ) <EOL> def _combineFrame ( self , other , func , axis = <NUM_LIT:0> ) : <EOL> index , columns = self . _get_plane_axes ( axis ) <EOL> axis = self . _get_axis_number ( axis ) <EOL> other = other . reindex ( index = index , columns = columns ) <EOL> if axis == <NUM_LIT:0> : <EOL> new_values = func ( self . values , other . values ) <EOL> elif axis == <NUM_LIT:1> : <EOL> new_values = func ( self . values . swapaxes ( <NUM_LIT:0> , <NUM_LIT:1> ) , other . values . T ) <EOL> new_values = new_values . swapaxes ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> elif axis == <NUM_LIT:2> : <EOL> new_values = func ( self . values . swapaxes ( <NUM_LIT:0> , <NUM_LIT:2> ) , other . values ) <EOL> new_values = new_values . swapaxes ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> new_frames = { } <EOL> for item , item_slice in zip ( self . items , new_values ) : <EOL> old_frame = self [ item ] <EOL> ofv = old_frame . default_fill_value <EOL> ok = old_frame . default_kind <EOL> new_frames [ item ] = SparseDataFrame ( item_slice , <EOL> index = self . major_axis , <EOL> columns = self . minor_axis , <EOL> default_fill_value = ofv , <EOL> default_kind = ok ) <EOL> return self . _new_like ( new_frames ) <EOL> def _new_like ( self , new_frames ) : <EOL> return SparsePanel ( new_frames , self . items , self . major_axis , <EOL> self . minor_axis , <EOL> default_fill_value = self . default_fill_value , <EOL> default_kind = self . default_kind ) <EOL> def _combinePanel ( self , other , func ) : <EOL> items = self . items . union ( other . items ) <EOL> major = self . major_axis . union ( other . major_axis ) <EOL> minor = self . minor_axis . union ( other . minor_axis ) <EOL> this = self . reindex ( items = items , major = major , minor = minor ) <EOL> other = other . reindex ( items = items , major = major , minor = minor ) <EOL> new_frames = { } <EOL> for item in items : <EOL> new_frames [ item ] = func ( this [ item ] , other [ item ] ) <EOL> if not isinstance ( other , SparsePanel ) : <EOL> new_default_fill = self . default_fill_value <EOL> else : <EOL> new_default_fill = func ( self . default_fill_value , <EOL> other . default_fill_value ) <EOL> return SparsePanel ( new_frames , items , major , minor , <EOL> default_fill_value = new_default_fill , <EOL> default_kind = self . default_kind ) <EOL> def major_xs ( self , key ) : <EOL> """<STR_LIT>""" <EOL> slices = dict ( ( k , v . xs ( key ) ) for k , v in self . iteritems ( ) ) <EOL> return DataFrame ( slices , index = self . minor_axis , columns = self . items ) <EOL> def minor_xs ( self , key ) : <EOL> """<STR_LIT>""" <EOL> slices = dict ( ( k , v [ key ] ) for k , v in self . iteritems ( ) ) <EOL> return SparseDataFrame ( slices , index = self . major_axis , <EOL> columns = self . items , <EOL> default_fill_value = self . default_fill_value , <EOL> default_kind = self . default_kind ) <EOL> def pow ( self , val , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . __pow__ ( val ) <EOL> def mod ( self , val , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . __mod__ ( val ) <EOL> SparsePanel . _add_aggregate_operations ( use_numexpr = False ) <EOL> ops . add_special_arithmetic_methods ( SparsePanel , use_numexpr = False , ** <EOL> ops . panel_special_funcs ) <EOL> SparseWidePanel = SparsePanel <EOL> def _convert_frames ( frames , index , columns , fill_value = np . nan , kind = '<STR_LIT>' ) : <EOL> from pandas . core . panel import _get_combined_index <EOL> output = { } <EOL> for item , df in compat . iteritems ( frames ) : <EOL> if not isinstance ( df , SparseDataFrame ) : <EOL> df = SparseDataFrame ( df , default_kind = kind , <EOL> default_fill_value = fill_value ) <EOL> output [ item ] = df <EOL> if index is None : <EOL> all_indexes = [ x . index for x in output . values ( ) ] <EOL> index = _get_combined_index ( all_indexes ) <EOL> if columns is None : <EOL> all_columns = [ x . columns for x in output . values ( ) ] <EOL> columns = _get_combined_index ( all_columns ) <EOL> index = _ensure_index ( index ) <EOL> columns = _ensure_index ( columns ) <EOL> for item , df in compat . iteritems ( output ) : <EOL> if not ( df . index . equals ( index ) and df . columns . equals ( columns ) ) : <EOL> output [ item ] = df . reindex ( index = index , columns = columns ) <EOL> return output , index , columns <EOL> def _stack_sparse_info ( frame ) : <EOL> lengths = [ s . sp_index . npoints for _ , s in compat . iteritems ( frame ) ] <EOL> minor_labels = np . repeat ( np . arange ( len ( frame . columns ) ) , lengths ) <EOL> inds_to_concat = [ ] <EOL> vals_to_concat = [ ] <EOL> for col in frame . columns : <EOL> series = frame [ col ] <EOL> if not np . isnan ( series . fill_value ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> int_index = series . sp_index . to_int_index ( ) <EOL> inds_to_concat . append ( int_index . indices ) <EOL> vals_to_concat . append ( series . sp_values ) <EOL> major_labels = np . concatenate ( inds_to_concat ) <EOL> sparse_values = np . concatenate ( vals_to_concat ) <EOL> return sparse_values , major_labels , minor_labels </s>
