-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
33 lines (24 loc) · 928 Bytes
/
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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import functools
import typing
import numpy as np
import pandas as pd
def downcast_dtypes(df: pd.DataFrame):
"""Downcast dtypes of a DataFrame from 64bit to 32bit."""
float_cols = [c for c in df if df[c].dtype == "float64"]
int_cols = [c for c in df if df[c].dtype == "int64"]
df[float_cols] = df[float_cols].astype(np.float32)
df[int_cols] = df[int_cols].astype(np.int32)
return df
def ensure_directory_exists(func: typing.Callable = None, directory: str = None):
"""Decorator that checks if a given directory exists. If not, the directory is created."""
if func is None:
return functools.partial(ensure_directory_exists, directory=directory)
@functools.wraps(func)
def f(*args, **kwargs):
if not os.path.isdir(directory):
os.mkdir(directory)
return func(*args, **kwargs)
return f