-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdask_dataset.py
More file actions
337 lines (292 loc) · 10.5 KB
/
dask_dataset.py
File metadata and controls
337 lines (292 loc) · 10.5 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from cdisc_rules_engine.models.dataset.pandas_dataset import PandasDataset
import dask.dataframe as dd
import dask.array as da
import pandas as pd
import numpy as np
import re
from typing import List, Union
DEFAULT_NUM_PARTITIONS = 4
class DaskDataset(PandasDataset):
def __init__(
self,
data=dd.from_pandas(pd.DataFrame(), npartitions=DEFAULT_NUM_PARTITIONS),
columns=None,
length=None,
):
self._data = data
self.length = length
if columns and self._data.empty:
self._data = dd.from_pandas(
pd.DataFrame(columns=columns), npartitions=DEFAULT_NUM_PARTITIONS
)
@property
def data(self):
return self._data
@property
def loc(self):
return self._data.loc
@property
def size(self):
memory_usage = self.data.get_partition(0).compute().memory_usage()
return memory_usage.sum()
@data.setter
def data(self, data):
self._data = data
def __getitem__(self, item):
return self._data[item].compute().reset_index(drop=True)
def is_column_sorted_within(self, group, column):
return (
False
not in np.concatenate(
self._data.groupby(group, sort=False)[column]
.apply(
lambda partition: sorted(partition.sort_index().values)
== partition.sort_index().values
)
.compute()
.values
)
.ravel()
.tolist()
)
def __setitem__(self, key, value):
if isinstance(value, list):
chunks = self._data.map_partitions(lambda x: len(x)).compute().to_numpy()
array_values = da.from_array(value, chunks=tuple(chunks))
self._data[key] = array_values
elif isinstance(value, pd.Series):
self._data = self._data.reset_index()
self._data = self._data.set_index("index")
self._data[key] = value
elif isinstance(value, dd.DataFrame):
for column in value:
self._data[column] = value[column]
else:
self._data[key] = value
def __len__(self):
if not self.length:
length = self._data.shape[0]
if not isinstance(length, int):
length = length.compute()
self.length = length
return self.length
@classmethod
def from_dict(cls, data: dict, **kwargs):
dataframe = dd.from_dict(data, npartitions=DEFAULT_NUM_PARTITIONS, **kwargs)
return cls(dataframe)
@classmethod
def from_records(cls, data: List[dict], **kwargs):
data = pd.DataFrame.from_records(data, **kwargs)
dataframe = dd.from_pandas(data, npartitions=DEFAULT_NUM_PARTITIONS)
return cls(dataframe)
@classmethod
def get_series_values(cls, series) -> list:
if not cls.is_series(series):
return []
if isinstance(cls, pd.Series):
return series.values
else:
return series.compute().values
def get(self, target: Union[str, List[str]], default=None):
if isinstance(target, list):
for column in target:
if column not in self._data:
# List contains values not in the dataset, treat as list of values
return default
return self._data[target].compute()
elif target in self._data:
return self._data[target].compute()
return default
def apply(self, func, **kwargs):
return self._data.apply(func, **kwargs).compute()
def merge(self, other, **kwargs):
if isinstance(other, pd.Series):
new_data = self._data.merge(
dd.from_pandas(other.reset_index(), npartitions=self._data.npartitions),
**kwargs,
)
else:
new_data = self._data.merge(other, **kwargs)
return self.__class__(new_data)
def __concat_columns(self, current, other):
for column in other.columns:
current[column] = other[column]
return current
def concat(self, other, **kwargs):
if kwargs.get("axis", 0) == 1:
if isinstance(other, list):
new_data = self._data.copy()
for dataset in other:
new_data = self.__concat_columns(new_data, dataset)
else:
new_data = self.__concat_columns(self._data.copy(), other)
else:
if isinstance(other, list):
datasets = [dataset.data for dataset in other]
new_data = dd.concat([self._data] + datasets, **kwargs)
else:
new_data = dd.concat([self._data.copy(), other.data], **kwargs)
return self.__class__(new_data)
def groupby(self, by: List[str], **kwargs):
invalid_kwargs = ["as_index"]
return self.__class__(
self._data.groupby(
by, **self._remove_invalid_kwargs(invalid_kwargs, kwargs)
)
)
def get_grouped_size(self, by, **kwargs):
if isinstance(self._data, pd.DataFrame):
grouped_data = self._data[by].groupby(by, **kwargs)
else:
grouped_data = self._data[by].compute().groupby(by, **kwargs)
group_sizes = grouped_data.size()
if self.is_series(group_sizes):
group_sizes = group_sizes.to_frame(name="size")
return group_sizes
@classmethod
def is_series(cls, data) -> bool:
return isinstance(data, dd.Series) or isinstance(data, pd.Series)
def len(self) -> int:
return self._data.shape[0].compute()
def rename(self, index=None, columns=None, inplace=True):
self._data = self._data.rename(index=index, columns=columns)
return self
def drop(self, labels=None, axis=0, columns=None, errors="raise"):
"""
Drop specified labels from rows or columns.
"""
self._data = self._data.drop(
labels=labels, axis=axis, columns=columns, errors=errors
)
return self
def melt(
self,
id_vars=None,
value_vars=None,
var_name=None,
value_name="value",
col_level=None,
):
"""
Unpivots a DataFrame from wide format to long format,
optionally leaving identifier variables set.
"""
new_data = self._data.melt(
id_vars=id_vars,
var_name=var_name,
value_vars=value_vars,
value_name=value_name,
col_level=col_level,
)
return self.__class__(new_data)
def assign(self, **kwargs):
return self.data.assign(**kwargs)
def copy(self):
new_data = self._data.copy()
return self.__class__(new_data)
def equals(self, other_dataset):
is_equal = True
for column in self.data:
if column not in other_dataset:
return False
is_equal = (
is_equal
& self[column]
.reset_index(drop=True)
.eq(other_dataset[column].reset_index(drop=True))
.all()
)
return is_equal
def get_error_rows(self, results) -> "pd.Dataframe":
"""
Returns a pandas dataframe with all errors found in the dataset. Limited to 1000
"""
self.data["computed_index"] = 1
self.data["computed_index"] = self.data["computed_index"].cumsum() - 1
data_with_results = self.data.set_index("computed_index", sorted=True)
data_with_results["results"] = results
data_with_results = data_with_results.fillna(value={"results": False})
return data_with_results[data_with_results["results"]].head(
1000, npartitions=-1
)
@classmethod
def cartesian_product(cls, left, right):
"""
Return the cartesian product of two dataframes
"""
return cls(
dd.from_pandas(
left.compute().merge(right, how="cross"),
npartitions=DEFAULT_NUM_PARTITIONS,
)
)
def dropna(self, inplace=False, **kwargs):
result = self._data.dropna(**kwargs)
if inplace:
self._data = result
return None
else:
return self.__class__(result)
def at(self, row_label, col_label):
"""
Get a single value for a row/column pair.
"""
partition_index = self.data.loc[row_label:row_label].partitions[0].compute()
value = partition_index.at[row_label, col_label]
return value
def drop_duplicates(self, subset=None, keep="first", **kwargs):
"""
Drop duplicate rows from the dataset.
"""
new_data = self._data.drop_duplicates(subset=subset, keep=keep, **kwargs)
return self.__class__(new_data)
def replace(self, to_replace, value, **kwargs):
self._data = self._data.replace(to_replace, value, **kwargs)
return self
def astype(self, dtype, **kwargs):
self._data = self._data.astype(dtype, **kwargs)
return self
def filter(self, **kwargs):
columns_regex = kwargs.get("regex")
columns_subset = [
column for column in self.columns if re.match(columns_regex, column)
]
new_data = self._data[columns_subset]
return self.__class__(new_data)
def min(self, *args, **kwargs):
"""
Return the minimum of the values over the requested axis.
"""
result = self._data.min(*args, **kwargs)
return self.__class__(result)
def reset_index(self, drop=False, **kwargs):
"""
Reset the index of the dataset.
"""
self._data = self._data.reset_index(drop=drop, **kwargs)
return self
def iloc(self, row, column):
"""
Purely integer-location based indexing for selection by position.
"""
return self.data.iloc[row, column].compute()
def fillna(
self,
value=None,
method=None,
axis=None,
inplace=False,
limit=None,
downcast=None,
):
"""
Fill NA/NaN values using the specified method.
"""
result = self._data.fillna(value=value, method=method, axis=axis, limit=limit)
if inplace:
self._data = result
return None
else:
return self.__class__(result)
def to_dict(self, **kwargs) -> dict:
return list(self._data.map_partitions(lambda x: x.to_dict(orient="records")))