-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.py
58 lines (41 loc) · 1.29 KB
/
set.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
# Authors: Thierry Moudiki
#
# License: BSD 3 Clause Clear
import numpy as np
import pickle
import warnings
import pandas as pd
import polars as pl
from ..utils import polars_to_pandas, pandas_to_polars
warnings.filterwarnings("ignore")
def setwhere(df, col, val, replace, copy=False):
""" Set value.
Args:
df: a data frame
a data frame
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
"""
is_polars = False
if isinstance(df, pl.DataFrame):
is_polars = True
df = polars_to_pandas(df)
if copy is True:
df_ = pickle.loads(pickle.dumps(df, -1))
df_[col][np.where(df[col] == val)[0]] = replace
if is_polars:
return pandas_to_polars(df_)
return df_
# copy == False:
df[col][np.where(df[col] == val)[0]] = replace
if is_polars:
return pandas_to_polars(df)
return df