-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.py
443 lines (280 loc) · 11.8 KB
/
wrapper.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""Wrapper for the querier's operations."""
# Authors: Thierry Moudiki
#
# License: BSD 3
import pandas as pd
import polars as pl
import sqlite3
from pymongo import MongoClient
from ..utils import polars_to_pandas, pandas_to_polars
from ..queries import (
select as select_,
update as update_,
delete as delete_,
concat as concat_,
filtr as filtr_,
join as join_,
summarize as summarize_,
drop as drop_,
request as request_,
setwhere as setwhere_,
)
class Querier(object):
""" A wrapper for chaining the querier's atomic operations, which are currently:
`concat`, `delete`, `drop`, `filtr`, `join`, `select`, `summarize`,
`update`, `request`
Attributes:
df: a data frame
a data frame (optional)
source: str
a csv file path, sql database path, or mongo db uri
db: str
database name, if `source` is a mongo db uri
table: str
name of the table/collection, if `source` is provided and is a database
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
def __init__(self, df=None, source=None, db=None, table=None, **kwargs):
if (df is not None):
assert (source is None) & (
table is None
), "No `source` required when `df` is provided"
self.df = df
elif source is not None:
assert df is None, "`df` is no used when `source` is provided"
self.source = source
if self.source[-3:] == "csv":
self.df = pd.read_csv(self.source, **kwargs)
elif self.source[0:7] == "mongodb":
assert (table is not None) & (
isinstance(table, str)
), "`table` name must be (a string) provided along with database `source`"
assert (db is not None) & (
isinstance(db, str)
), "A database `db` must be provided"
self.table = table
self.conn = MongoClient(self.source, **kwargs)
self.db = self.conn[db]
self.df = pd.DataFrame(list(self.db[table].find()))
else: # other db source
assert (table is not None) & (
isinstance(table, str)
), "`table` name must be (a string) provided along with db `source`"
self.table = table
try:
self.df = pd.read_sql_table(
table_name=self.table, con=self.source, **kwargs
)
except:
try: # sqlite3 source
self.conn = sqlite3.connect(self.source, **kwargs)
self.df = pd.read_sql_query(
"SELECT * FROM " + self.table,
con=self.conn,
**kwargs
)
except:
raise ValueError("Database format not supported")
else:
raise ValueError("Check database format provided in `source`")
self.colnames = self.df.columns.values
self.rownames = self.df.index.values
self.nrows = self.df.shape[0]
self.ncols = self.df.shape[1]
self.dtypes = self.df.dtypes.values
def get_df(self, return_values=False):
""" Get object's data frame attribute.
Args:
return_values: bool
If True, a numpy array is returned. Otherwise, a data frame (default).
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
if return_values:
return self.df.values
return self.df
def set_df(self, df):
""" Set object's data frame attribute.
Args:
df: A data frame
the data frame to be concatenated to our existing data
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = df
def select(
self,
req="*",
order_by=None,
asc=True,
limit=None,
random=False,
seed=123,
):
""" Select columns.
Args:
req: str
comma-separated columns names
order_by: str
sort the results by using these columns (optional)
asc: bool
if `order_by` is provided, `True` means: ascending ordering
limit: int
number of records to be retrieved
random: bool
`True` if we want a random set of records
seed: int
reproducibility seed for situations where `random == True`
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = select_(self.df, req, order_by, asc, limit, random, seed)
return self
def update(self, req=None):
""" Update the data frame.
Args:
req: str
specifying the transformation, e.g `new_size = 3*size`
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = update_(self.df, req)
return self
def delete(self, req=None):
""" Delete rows from the data frame.
Args:
req: str
criteria for filtering the rows to be deleted
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = delete_(self.df, req)
return self
def concat(self, df2, axis="h", **kwargs):
""" Concatenate data frames.
Args:
df2: A data frame
the data frame to be concatenated to our existing data
axis: str
specify the type of concatenation: "h"=horizontal, "v"=vertical
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
assert isinstance(df2, pd.DataFrame), "'df2' must be a data frame"
self.df = concat_(self.df, df2, axis, **kwargs)
return self
def filtr(self, req, limit=None, random=False, seed=123):
""" Filter rows, based on given criteria.
Args:
req: str
criteria for filtering the rows
limit: int
number of records to be retrieved
random: bool
`True` if we want a random set of records
seed: int
reproducibility seed for situations where `random == True`
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = filtr_(self.df, req, limit, random, seed)
return self
def join(self, df2, on=None, type_join="inner", **kwargs):
""" Join data frames into our existing data frame.
Args:
df2: A data frame
the data frame to be joined to our existing data
on: str
joining column/criterion
type_join: str
type of join. Options are: "left", "right", "outer", "inner".
Default is "inner" join.
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = join_(self.df, df2, on, type_join, **kwargs)
return self
def summarize(self, req, group_by=None, having=None, **kwargs):
""" Data summaries on rows.
Args:
req: str
specifying the aggregating operations on columns
group_by: str
comma-separated list of columns to be aggregated
having: str
filtering criterion on groups
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = summarize_(self.df, req, group_by, having, **kwargs)
return self
def drop(self, req):
""" Drop columns.
Args:
req: str
comma-separated list of columns to be dropped
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = drop_(self.df, req)
return self
def request(self, req, **kwargs):
""" SQL request on the data frame.
Args:
req: str
specifying the SQL request
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = request_(self.df, req, **kwargs)
return self
def setwhere(self, col, val, replace):
""" Set value.
Args:
col: str
column to be filtered on
val: object
value to be replaced in column `col`
replace: object
replacement value
copy: bool
If True, a new data frame is created else input data frame is modified (default False)
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
self.df = setwhere_(self.df, col=col, val=val,
replace=replace, copy=False)
return self
def write(self, output, conn=None, db=None, **kwargs):
""" Export data frame's content to csv file or database.
Args:
output: str
csv file path, sql table name, mongo db collection name
conn: a database connexion
database connexion (optional). Default: use the current object's
connexion, if provided.
db: str
collection name (optional), if `conn` is a mongo db connexion
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
if output[-3:] == "csv":
self.df.to_csv(path_or_buf=output, **kwargs)
else:
if conn is None:
try: # sql
self.df.to_sql(
output, con=self.conn, if_exists="fail", **kwargs
)
except: # mongo db
records = self.df.to_dict(orients="records")
exec(
"self.conn." + self.db[output] + ".insert_many(records)"
)
else:
try: # sql
self.df.to_sql(output, con=conn, if_exists="fail", **kwargs)
except: # mongo db
records = self.df.to_dict(orients="records")
exec("conn.db[" + output + "].insert_many(records)")