-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
77 lines (69 loc) · 2.49 KB
/
utils.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
# pylint: skip-file
""" This module contains utility and helper functions"""
import json
import pandas as pd
import numpy as np
def read_file(
file_path: str, dtypes: dict = None, downcast: bool = False
) -> pd.DataFrame:
"""
Reads a csv or xlsx file
:param file_path: an str path to csv or xlsx file
:param dtypes: a dictionary of data types
:param downcast: a boolean to downcast data types
:returns: a DataFrame
"""
if dtypes:
non_dates = dict(
filter(lambda val: val[1] != "datetime64[ns]", dtypes.items())
)
dates = dict(
filter(lambda val: val[1] == "datetime64[ns]", dtypes.items())
)
else:
non_dates = {}
dates = {}
if ".csv" in file_path:
frame = pd.read_csv(file_path, dtype=non_dates, sep=",")
elif ".xlsx" in file_path:
frame = pd.read_excel(file_path, dtype=non_dates)
for date in dates.keys():
if pd.api.types.is_numeric_dtype(frame[date]):
unix_date = frame[date].clip(lower=0).astype(str)
unix_date = unix_date.str[:10]
frame[date] = pd.to_datetime(
pd.Series(unix_date, dtype="datetime64[ns]"),
unit="s",
errors="ignore",
)
else:
frame[date] = pd.to_datetime(
pd.Series(frame[date], dtype="datetime64[ns]"), errors="ignore"
)
if downcast:
for col in frame.columns:
if issubclass(frame[col].dtypes.type, np.int_):
frame[col] = pd.to_numeric(frame[col], downcast="integer")
elif issubclass(frame[col].dtypes.type, np.float64):
frame[col] = pd.to_numeric(frame[col], downcast="float")
elif issubclass(frame[col].dtypes.type, np.object_) and (
len(frame[col].unique()) <= 20
):
frame[col] = frame[col].astype("category")
elif issubclass(frame[col].dtypes.type, np.object_) and (
len(frame[col].unique()) > 20
):
frame[col] = frame[col].astype(str)
return frame
class TypeEncoder(json.JSONEncoder):
"""Custom encoder class for json"""
def default(self, o):
if isinstance(o, np.bool_):
return bool(o)
if isinstance(o, np.integer):
return int(o)
if isinstance(o, np.floating):
return float(o)
if isinstance(o, set):
return list(o)
return super().default(o)