-
Notifications
You must be signed in to change notification settings - Fork 1
/
volgpt_clean_data.py
97 lines (81 loc) · 3.3 KB
/
volgpt_clean_data.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pandas as pd
import numpy as np
import statsmodels.api as sm
from io import StringIO
from datetime import datetime
def clean_data(text_data, column_names=None):
if column_names is None:
column_names = ['DateTimeIndex', 'Ticker', 'CloseBidSize', 'CloseAskSize', 'CloseBidPrice', 'CloseAskPrice', 'WeightedMidPrice', 'rr', 'lr']
# Data cleaning
data_io = StringIO(text_data)
df = pd.read_csv(data_io, header=None, names=column_names)
# check date format
def check_date_format(date_str):
if pd.isna(date_str):
return False
try:
datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
return True
except ValueError:
return False
# Alternative date format checks
# def check_date_format(date_str):
# if pd.isna(date_str):
# return False
# try:
# dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
# min_date = datetime(2000, 1, 1) # for tractability, I set ranges outside of the actual data range
# max_date = datetime(2050, 12, 31)
# if dt < min_date or dt > max_date:
# return False
# return True
# except ValueError:
# return False
# Another alternative date format check
# def check_date_format(date_str):
# if pd.isna(date_str):
# return False
# try:
# dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
# min_date = pd.Timestamp.min.to_pydatetime()
# max_date = pd.Timestamp.max.to_pydatetime()
# if dt < min_date or dt > max_date:
# return False
# return True
# except ValueError:
# return False
def check_numeric(value):
if pd.isna(value):
return False
try:
float(value)
return True
except ValueError:
return False
data_io = StringIO(text_data)
df = pd.read_csv(data_io, header=None, names=column_names)
invalid_rows = []
for index, row in df.iterrows():
if not check_date_format(row['DateTimeIndex']) or \
not all(check_numeric(val) for val in row[['CloseBidSize', 'CloseAskSize', 'CloseBidPrice', 'CloseAskPrice', 'WeightedMidPrice', 'rr', 'lr']].values):
invalid_rows.append(index)
df_clean = df.drop(invalid_rows)
# Data type conversions
df_clean['DateTimeIndex'] = df_clean['DateTimeIndex'].apply(pd.to_datetime)
df_clean['Ticker'] = df_clean['Ticker'].astype(str)
df_clean['CloseBidSize'] = pd.to_numeric(df_clean['CloseBidSize'])
df_clean['CloseAskSize'] = pd.to_numeric(df_clean['CloseAskSize'])
df_clean['CloseBidPrice'] = pd.to_numeric(df_clean['CloseBidPrice'])
df_clean['CloseAskPrice'] = pd.to_numeric(df_clean['CloseAskPrice'])
df_clean['WeightedMidPrice'] = pd.to_numeric(df_clean['WeightedMidPrice'])
df_clean['rr'] = pd.to_numeric(df_clean['rr'])
df_clean['lr'] = pd.to_numeric(df_clean['lr'])
# print("Outputs from clean_data function: ")
# print("-----------------------------"), print()
# print("Original DataFrame: ")
# print(df)
# print("Cleaned DataFrame: ")
# print(df_clean)
# print("Invalid Rows: ")
# print(invalid_rows)
return df, df_clean, invalid_rows