<s> """<STR_LIT>""" <EOL> from pandas . core . index import MultiIndex , Index <EOL> from pandas . core . series import Series <EOL> from pandas . compat import OrderedDict , lmap <EOL> def _check_is_partition ( parts , whole ) : <EOL> whole = set ( whole ) <EOL> parts = [ set ( x ) for x in parts ] <EOL> if set . intersection ( * parts ) != set ( ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> if set . union ( * parts ) != whole : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def _to_ijv ( ss , row_levels = ( <NUM_LIT:0> , ) , column_levels = ( <NUM_LIT:1> , ) , sort_labels = False ) : <EOL> """<STR_LIT>""" <EOL> _check_is_partition ( [ row_levels , column_levels ] , range ( ss . index . nlevels ) ) <EOL> values = ss . _data . internal_values ( ) . _valid_sp_values <EOL> nonnull_labels = ss . dropna ( ) <EOL> def get_indexers ( levels ) : <EOL> """<STR_LIT>""" <EOL> values_ilabels = [ tuple ( x [ i ] for i in levels ) <EOL> for x in nonnull_labels . index ] <EOL> if len ( levels ) == <NUM_LIT:1> : <EOL> values_ilabels = [ x [ <NUM_LIT:0> ] for x in values_ilabels ] <EOL> def _get_label_to_i_dict ( labels , sort_labels = False ) : <EOL> """<STR_LIT>""" <EOL> labels = Index ( lmap ( tuple , labels ) ) . unique ( ) . tolist ( ) <EOL> if sort_labels : <EOL> labels = sorted ( list ( labels ) ) <EOL> d = OrderedDict ( ( k , i ) for i , k in enumerate ( labels ) ) <EOL> return ( d ) <EOL> def _get_index_subset_to_coord_dict ( index , subset , sort_labels = False ) : <EOL> def robust_get_level_values ( i ) : <EOL> try : <EOL> return index . get_level_values ( index . names [ i ] ) <EOL> except KeyError : <EOL> return index . get_level_values ( i ) <EOL> ilabels = list ( zip ( * [ robust_get_level_values ( i ) for i in subset ] ) ) <EOL> labels_to_i = _get_label_to_i_dict ( ilabels , <EOL> sort_labels = sort_labels ) <EOL> labels_to_i = Series ( labels_to_i ) <EOL> if len ( subset ) > <NUM_LIT:1> : <EOL> labels_to_i . index = MultiIndex . from_tuples ( labels_to_i . index ) <EOL> labels_to_i . index . names = [ index . names [ i ] for i in subset ] <EOL> labels_to_i . name = '<STR_LIT:value>' <EOL> return ( labels_to_i ) <EOL> labels_to_i = _get_index_subset_to_coord_dict ( ss . index , levels , <EOL> sort_labels = sort_labels ) <EOL> i_coord = labels_to_i [ values_ilabels ] . tolist ( ) <EOL> i_labels = labels_to_i . index . tolist ( ) <EOL> return i_coord , i_labels <EOL> i_coord , i_labels = get_indexers ( row_levels ) <EOL> j_coord , j_labels = get_indexers ( column_levels ) <EOL> return values , i_coord , j_coord , i_labels , j_labels <EOL> def _sparse_series_to_coo ( ss , row_levels = ( <NUM_LIT:0> , ) , column_levels = ( <NUM_LIT:1> , ) , <EOL> sort_labels = False ) : <EOL> """<STR_LIT>""" <EOL> import scipy . sparse <EOL> if ss . index . nlevels < <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not ss . index . is_unique : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> row_levels = [ ss . index . _get_level_number ( x ) for x in row_levels ] <EOL> column_levels = [ ss . index . _get_level_number ( x ) for x in column_levels ] <EOL> v , i , j , rows , columns = _to_ijv ( ss , row_levels = row_levels , <EOL> column_levels = column_levels , <EOL> sort_labels = sort_labels ) <EOL> sparse_matrix = scipy . sparse . coo_matrix ( <EOL> ( v , ( i , j ) ) , shape = ( len ( rows ) , len ( columns ) ) ) <EOL> return sparse_matrix , rows , columns <EOL> def _coo_to_sparse_series ( A , dense_index = False ) : <EOL> """<STR_LIT>""" <EOL> s = Series ( A . data , MultiIndex . from_arrays ( ( A . row , A . col ) ) ) <EOL> s = s . sort_index ( ) <EOL> s = s . to_sparse ( ) <EOL> if dense_index : <EOL> i = range ( A . shape [ <NUM_LIT:0> ] ) <EOL> j = range ( A . shape [ <NUM_LIT:1> ] ) <EOL> ind = MultiIndex . from_product ( [ i , j ] ) <EOL> s = s . reindex_axis ( ind ) <EOL> return s </s>
