zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
U
¬ý°dçã@sìdZddlmZddlmZmZddlZddlm    Z    ddl
m Z m Z ddl mZddlmZdd    lmZdd
lmZer€ddlZd d d œd d„Zd'dddddœdd„Zd(dddddœdd„Zd)dddddd œd!d"„Zd*d#ddd$œd%d&„ZdS)+zR
Interaction with scipy.sparse matrices.
 
Currently only includes to_coo helpers.
é)Ú annotations)Ú TYPE_CHECKINGÚIterableN)Úlib)Ú
IndexLabelÚnpt)Únotna)Ú    factorize)Ú
MultiIndex)ÚSeriesr©ÚpartsZwholecCsHt|ƒ}dd„|Dƒ}tj|Žtƒkr.tdƒ‚tj|Ž|krDtdƒ‚dS)NcSsg|] }t|ƒ‘qS©)Úset©Ú.0Úxrrú]d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/core/arrays/sparse/scipy_sparse.pyÚ
<listcomp>!sz'_check_is_partition.<locals>.<listcomp>z4Is not a partition because intersection is not null.z2Is not a partition because union is not the whole.)rÚ intersectionÚ
ValueErrorÚunionr rrrÚ_check_is_partitions rFztuple[int] | list[int]znpt.NDArray[np.intp]Úboolz-tuple[npt.NDArray[np.intp], list[IndexLabel]])ÚlevelsÚ valid_ilocsÚ sort_labelsÚreturncsv|r6t|ƒdkr6ˆjj|d|}ˆjj|d}n0t ‡fdd„|Dƒ¡}t||d\}}||}| ¡}||fS)aö
    For a MultiIndexed sparse Series `ss`, return `ax_coords` and `ax_labels`,
    where `ax_coords` are the coordinates along one of the two axes of the
    destination sparse matrix, and `ax_labels` are the labels from `ss`' Index
    which correspond to these coordinates.
 
    Parameters
    ----------
    ss : Series
    levels : tuple/list
    valid_ilocs : numpy.ndarray
        Array of integer positions of valid values for the sparse matrix in ss.
    sort_labels : bool, default False
        Sort the axis labels before forming the sparse matrix. When `levels`
        refers to a single level, set to True for a faster execution.
 
    Returns
    -------
    ax_coords : numpy.ndarray (axis coordinates)
    ax_labels : list (axis labels)
    ércsg|]}ˆj |¡ ¡‘qSr)ÚindexZget_level_valuesZto_numpy)rZlvl©ÚssrrrLsz#_levels_to_axis.<locals>.<listcomp>)Úsort)ÚlenrÚcodesrrZfast_zipr    Útolist)r!rrrZ    ax_coordsZ    ax_labelsZ levels_valuesr$rr rÚ_levels_to_axis(sÿr&©r©rzatuple[np.ndarray, npt.NDArray[np.intp], npt.NDArray[np.intp], list[IndexLabel], list[IndexLabel]])Ú
row_levelsÚ column_levelsrrc Csrt||gt|jjƒƒ|jj}t|ƒ}||}|jjj|}t    ||||d\}}    t    ||||d\}
} |||
|    | fS)a‡
    For an arbitrary MultiIndexed sparse Series return (v, i, j, ilabels,
    jlabels) where (v, (i, j)) is suitable for passing to scipy.sparse.coo
    constructor, and ilabels and jlabels are the row and column labels
    respectively.
 
    Parameters
    ----------
    ss : Series
    row_levels : tuple/list
    column_levels : tuple/list
    sort_labels : bool, default False
        Sort the row and column labels before forming the sparse matrix.
        When `row_levels` and/or `column_levels` refer to a single level,
        set to `True` for a faster execution.
 
    Returns
    -------
    values : numpy.ndarray
        Valid values to populate a sparse matrix, extracted from
        ss.
    i_coords : numpy.ndarray (row coordinates of the values)
    j_coords : numpy.ndarray (column coordinates of the values)
    i_labels : list (row labels)
    j_labels : list (column labels)
    )r)
rÚrangerÚnlevelsÚarrayZ    sp_valuesrZsp_indexÚindicesr&) r!r)r*rZsp_valsZna_maskÚvaluesrZi_coordsZi_labelsZj_coordsZj_labelsrrrÚ_to_ijvUs$'ÿ
ÿ
r0r z Iterable[int]zBtuple[scipy.sparse.coo_matrix, list[IndexLabel], list[IndexLabel]])r!r)r*rrc s˜ddl}ˆjjdkrtdƒ‚ˆjjs,tdƒ‚‡fdd„|Dƒ}‡fdd„|Dƒ}tˆ|||d    \}}}}}    |jj|||fft|ƒt|    ƒfd
}
|
||    fS) zÐ
    Convert a sparse Series to a scipy.sparse.coo_matrix using index
    levels row_levels, column_levels as the row and column
    labels respectively. Returns the sparse_matrix, row and column labels.
    rNéz-to_coo requires MultiIndex with nlevels >= 2.zADuplicate index entries are not allowed in to_coo transformation.csg|]}ˆj |¡‘qSr©rZ_get_level_numberrr rrr¤sz(sparse_series_to_coo.<locals>.<listcomp>csg|]}ˆj |¡‘qSrr2rr rrr¥s)r)r*r)Úshape)    Ú scipy.sparserr,rZ    is_uniquer0ÚsparseZ
coo_matrixr#) r!r)r*rÚscipyÚvÚiÚjÚrowsÚcolumnsZ sparse_matrixrr rÚsparse_series_to_coos(  ÿÿ
ÿr<zscipy.sparse.coo_matrix)ÚAÚ dense_indexrc
Cs¦ddlm}z"t|jt |j|jf¡dd}Wn:tk
rh}zt    dt
|ƒj ›dƒ|‚W5d}~XYnX|  ¡}|  ||jƒ¡}|r¢t |j|jg¡}| |¡}|S)a
    Convert a scipy.sparse.coo_matrix to a Series with type sparse.
 
    Parameters
    ----------
    A : scipy.sparse.coo_matrix
    dense_index : bool, default False
 
    Returns
    -------
    Series
 
    Raises
    ------
    TypeError if A is not a coo_matrix
    r)Ú SparseDtypeF)ÚcopyzExpected coo_matrix. Got z     instead.N)Zpandasr?r Údatar
Z from_arraysÚrowÚcolÚAttributeErrorÚ    TypeErrorÚtypeÚ__name__Z
sort_indexZastypeZdtypeZ from_productZreindex)r=r>r?ZserÚerrÚindrrrÚcoo_to_sparse_series°s "ÿþ
rJ)F)r'r(F)r'r(F)F)Ú__doc__Ú
__future__rÚtypingrrÚnumpyÚnpZ pandas._libsrZpandas._typingrrZpandas.core.dtypes.missingrZpandas.core.algorithmsr    Zpandas.core.indexes.apir
Zpandas.core.seriesr r4r6rr&r0r<rJrrrrÚ<module>s0       ü/ü<ü"ÿ