Repository navigation
Expand file tree
/
Copy pathvisualize_data.py
More file actions
59 lines (47 loc) · 1.76 KB
/
Copy pathvisualize_data.py
File metadata and controls
59 lines (47 loc) · 1.76 KB
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
import pandas as pd, numpy as np, matplotlib.pyplot as plt, matplotlib.dates as mdates
from datetime import datetime, date
pd.options.mode.chained_assignment = None
def remove_na(df,mixed_types,dtypes):
for mtype in mixed_types:
df[mtype] = pd.to_numeric(df[mtype],errors='coerce')
df = df.dropna(subset=mixed_types)
for mtype in mixed_types:
df[mtype] = df[mtype].astype(dtypes[mtype])
return df
def validate_col_type(df,colname,dtype):
return df.loc[df[colname].map(type) != dtype].empty
def prepare_data(filename,
cols=None,
mixed_types=None,
dtypes=None,
clean_data=None,
):
if mixed_types != None and dtypes == None:
print('please specify the dtypes for the columns of mixed types')
return
df = pd.read_csv(filename, usecols=cols)
if mixed_types != None:
df = remove_na(df,mixed_types,dtypes)
if clean_data != None:
df = clean_data(df)
return df
def visualize_data(df,
x_variable=None,
y_variable=None,
x_is_date=False,
y_is_date=False,
savepath=None):
fig, ax = plt.subplots()
ax.scatter(df[x_variable],df[y_variable],s=1)
if x_is_date:
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d, %Y'))
fig.autofmt_xdate()
if y_is_date:
ax.yaxis_date()
ax.yaxis.set_major_formatter(mdates.DateFormatter('%b %d, %Y'))
fig.autofmt_ydate()
if savepath == None:
plt.show()
else:
plt.savefig(savepath)