-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
43 lines (31 loc) · 952 Bytes
/
update.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
# Authors: Thierry Moudiki
#
# License: BSD 3
import pandas as pd
import polars as pl
from ..utils import parse_update_request
from ..utils import polars_to_pandas, pandas_to_polars
# modifies input df (!)
# update(df, 'tip = 2*tip')
# update(df, "toto = 3*tip")
# update(df, 'tip = np.mean(tip)')
# update(df, 'toto = np.mean(tip)')
def update(df, req):
""" Update the data frame.
Args:
df: a data frame
a data frame
req: str
specifying the transformation, e.g `new_size = 3*size`
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
is_polars = False
if isinstance(df, pl.DataFrame):
is_polars = True
df = polars_to_pandas(df)
# if request is not None:
exec(parse_update_request(req), {"df": df})
if is_polars:
return pandas_to_polars(df)
return df