-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretreat.py
executable file
·138 lines (122 loc) · 4.43 KB
/
pretreat.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
"""
Made with Gooey:
/ ____| | | |
| | __ ___ ___ ___ _ _| | |
| | |_ |/ _ \ / _ \ / _ \ | | | | |
| |__| | (_) | (_) | __/ |_| |_|_|
\_____|\___/ \___/ \___|\__, (_|_)
__/ |
|___/
PRE-TREAT
=========
This program will automagically perform common metadata
operations on files, making a GUI via Gooey
"""
from message import display_message
import pandas as pd
import os
from dateparser.search import search_dates
from dateparser import parse
from argparse import ArgumentParser
from gooey import Gooey, GooeyParser
@Gooey(dump_build_config=False, program_name='Pre-Treat')
def main():
desc = 'A GUI program for cleaning tabular data'
parser = GooeyParser(description=desc)
# Add ability to choose a file
parser.add_argument('file_input',
metavar='File Input',
action='store',
help='Select the file you want to process',
widget='FileChooser')
# Add ability to save the file
parser.add_argument('output_directory',
metavar='Output Directory',
action='store',
help='Choose where to save the output',
widget='DirChooser')
args = parser.parse_args()
display_message()
return args
def make_data_frame(file_input):
"""
Take the input data file (assuming Excel) and return a pandas DataFrame
"""
input_df = pd.read_excel(file_input)
return input_df
def trim_spaces(data_input):
"""
Take the DataFrame and remove surrounding spaces on values
"""
data_input.replace('(^\s+|\s+$)', '', regex=True, inplace=True)
return data_input
def remove_double_spaces(data_input):
"""
Take the DataFrame and remove consecutive spaces
"""
data_input.replace(to_replace='\s\s', value=' ', regex=True, inplace=True)
return data_input
def delimiters_to_pipes(data_input):
"""
Take the DataFrame and within topics, replace commas with pipes
"""
data_input.replace({'Subject:topic': r'[,;]\s'}, {'Subject:topic': ' | '}, regex=True, inplace=True)
return data_input
def process_dates(data_input):
"""
Try to handle dates, and start to populate the Begin + End Date columns
"""
date_begin = []
date_end = []
columns = data_input.columns
for column in columns:
if column.lower().startswith("date"):
for row in data_input[column]:
if len(str(row)) == 4:
date_begin.append(str(row) + "-01-01")
date_end.append(str(row) + "-12-31")
elif len(str(row)) == 10:
date_begin.append(str(row))
date_end.append(str(row))
elif pd.isnull(row) == True:
date_begin.append("")
date_end.append("")
else:
parsed_begin = parse(str(row), settings={'PREFER_DAY_OF_MONTH': 'first'})
parsed_end = parse(str(row), settings={'PREFER_DAY_OF_MONTH': 'last'})
date_begin.append(str(parsed_begin))
date_end.append(str(parsed_end))
data_input['Begin date'] = date_begin
data_input['End date'] = date_end
return data_input
def save_results(summarized_data, output):
"""
Take all the data and save as Excel file
"""
summarized_data = data_frame
output_file = os.path.join(output, "gooey_output.xlsx")
writer = pd.ExcelWriter(output_file, engine='xlsxwriter')
summarized_data.to_excel(writer, index=False)
writer.save()
# Comment out to switch to csv output
#output_file = os.path.join(output, "gooey_output.csv")
#summarized_data.to_csv(output_file)
if __name__ == '__main__':
conf = main()
input_file = conf.file_input
print('You chose this file: ' + str(input_file))
df = make_data_frame(conf.file_input)
print("Here's a preview:\n", df.head())
# Strip spaces
data_frame = trim_spaces(df)
# Remove double spaces
data_frame = remove_double_spaces(df)
# Replace semicolons with pipes
data_frame = delimiters_to_pipes(df)
# Begin to populate Begin and End Date
data_frame = process_dates(df)
# Save the file as Excel
print("Saving results data")
save_results(data_frame, conf.output_directory)
print("Done!")