-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathaccessor.py
201 lines (160 loc) · 6.26 KB
/
accessor.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#
# Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
#
# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership. The Modin Development Team licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
# Code in this file may constitute partial or total reimplementation, or modification of
# existing code originally distributed by the Modin project, under the Apache License,
# Version 2.0.
"""
Implement various accessor classes for DataFrame and Series API.
SparseFrameAccessor implements API of pandas.DataFrame.sparse accessor.
SparseAccessor implements API of pandas.Series.sparse accessor.
CachedAccessor implements API of pandas.core.accessor.CachedAccessor
"""
import pandas
from pandas.core.dtypes.dtypes import SparseDtype
import snowflake.snowpark.modin.pandas as pd
from snowflake.snowpark.modin.plugin.utils.error_message import ErrorMessage
from snowflake.snowpark.modin.utils import _inherit_docstrings
class BaseSparseAccessor:
"""
Base class for various sparse DataFrame accessor classes.
Parameters
----------
data : DataFrame or Series
Object to operate on.
"""
_validation_msg = "Can only use the '.sparse' accessor with Sparse data."
def __init__(self, data=None) -> None:
self._parent = data
self._validate(data)
@classmethod
def _validate(cls, data):
"""
Verify that `data` dtypes are compatible with `pandas.core.arrays.sparse.dtype.SparseDtype`.
Parameters
----------
data : DataFrame
Object to check.
Raises
------
NotImplementedError
Function is implemented in child classes.
"""
ErrorMessage.not_implemented("Implemented by subclasses") # pragma: no cover
def _default_to_pandas(self, op, *args, **kwargs):
"""
Convert dataset to pandas type and call a pandas sparse.`op` on it.
Parameters
----------
op : str
Name of pandas function.
*args : list
Additional positional arguments to be passed in `op`.
**kwargs : dict
Additional keywords arguments to be passed in `op`.
Returns
-------
object
Result of operation.
"""
return self._parent._default_to_pandas(
lambda parent: op(parent.sparse, *args, **kwargs)
)
# Snowpark pandas does not support sparse accessors - remove docstrings to prevent doctests from running
# @_inherit_docstrings(pandas.core.arrays.sparse.accessor.SparseFrameAccessor)
class SparseFrameAccessor(BaseSparseAccessor):
@classmethod
def _validate(cls, data):
"""
Verify that `data` dtypes are compatible with `pandas.core.arrays.sparse.dtype.SparseDtype`.
Parameters
----------
data : DataFrame
Object to check.
Raises
------
AttributeError
If check fails.
"""
dtypes = data.dtypes
if not all(isinstance(t, SparseDtype) for t in dtypes):
raise AttributeError(cls._validation_msg)
@property
def density(self):
return self._parent._default_to_pandas(pandas.DataFrame.sparse).density
@classmethod
def from_spmatrix(cls, data, index=None, columns=None):
return pd.DataFrame(
pandas.DataFrame.sparse.from_spmatrix(data, index=index, columns=columns)
)
def to_dense(self):
return self._default_to_pandas(pandas.DataFrame.sparse.to_dense)
def to_coo(self):
return self._default_to_pandas(pandas.DataFrame.sparse.to_coo)
# Snowpark pandas does not support sparse accessors - remove docstrings to prevent doctests from running
# @_inherit_docstrings(pandas.core.arrays.sparse.accessor.SparseAccessor)
class SparseAccessor(BaseSparseAccessor):
@classmethod
def _validate(cls, data):
"""
Verify that `data` dtype is compatible with `pandas.core.arrays.sparse.dtype.SparseDtype`.
Parameters
----------
data : Series
Object to check.
Raises
------
AttributeError
If check fails.
"""
if not isinstance(data.dtype, SparseDtype):
raise AttributeError(cls._validation_msg)
@property
def density(self):
return self._parent._default_to_pandas(pandas.Series.sparse).density
@property
def fill_value(self):
return self._parent._default_to_pandas(pandas.Series.sparse).fill_value
@property
def npoints(self):
return self._parent._default_to_pandas(pandas.Series.sparse).npoints
@property
def sp_values(self):
return self._parent._default_to_pandas(pandas.Series.sparse).sp_values
@classmethod
def from_coo(cls, A, dense_index=False):
return cls._default_to_pandas(
pandas.Series.sparse.from_coo, A, dense_index=dense_index
)
def to_coo(self, row_levels=(0,), column_levels=(1,), sort_labels=False):
return self._default_to_pandas(
pandas.Series.sparse.to_coo,
row_levels=row_levels,
column_levels=column_levels,
sort_labels=sort_labels,
)
def to_dense(self):
return self._default_to_pandas(pandas.Series.sparse.to_dense)
@_inherit_docstrings(pandas.core.accessor.CachedAccessor)
class CachedAccessor:
def __init__(self, name: str, accessor) -> None:
self._name = name
self._accessor = accessor
def __get__(self, obj, cls):
if obj is None:
return self._accessor
accessor_obj = self._accessor(obj)
object.__setattr__(obj, self._name, accessor_obj)
return accessor_obj