-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfile_utils.py
More file actions
332 lines (283 loc) · 10.8 KB
/
file_utils.py
File metadata and controls
332 lines (283 loc) · 10.8 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import pandas as pd
import os
def file_search(path=".", doctype="csv", like=[""], strict=False):
"""
This function creates a list of all files of a certain type, satisfying the criteria outlined
in like = [...] parameter. The function only searches for files in the specified folder
of the current working directory that is set by the user.
Parameters
-----------
path : string
Path to a folder in the current working directory
default = '.', i.e. current working directory folder
doctype : string
Document format to search for
e.g. 'csv' or 'xlsx'
default = 'csv'
like : list
A list of words to filter the file search on
default = [''], i.e. no filter
strict : bool
Set True to search for filenames containing all words from 'like' list (
default = False
Returns
-------
list
Examples
-------
>>> file_search(doctype = 'md')
['README.md', 'CONTRIBUTING.md']
>>> file_search(doctype = 'md', like = ['READ'])
['README.md']
"""
if not isinstance(path, str):
raise ValueError("Please input path as a string")
elif not isinstance(doctype, str):
raise ValueError("Please input doctype as a string")
elif not isinstance(like, list):
raise ValueError("Please input like as a list")
elif not isinstance(strict, bool):
raise ValueError("Please input strict as a bool")
else:
pass
list_of_files = []
if strict is False:
for file in os.listdir(path):
if (file.split(".")[-1] == doctype) & (any(x in file for x in like)):
list_of_files.append(file)
else:
for file in os.listdir(path):
if (file.split(".")[-1] == doctype) & (all(x in file for x in like)):
list_of_files.append(file)
return list_of_files
def import_files(
path=".", doctype="csv", sheet="Sheet1", subdir=False, like=[""], strict=False
):
"""
This function imports all documents of a given format to a dictionary
and returns this dictionary, keeping original file names.
Parameters
----------
path : string
Path to a folder in the current working directory
default = '.', i.e. current working directory folder
doctype : string
Document format to search for
e.g. 'csv' or 'xlsx'
default = 'csv'
sheet : string
Sheet name of the xlsx file
default = 'Sheet1'
subdir : bool
True to allow download all files, including the subdirectories
default = False
like : list
A list of words to filter the file search on
default = [''], i.e. no filter
strict : bool
Set True to search for filenames containing all words from 'like' list
default = False
Returns
-------
out : dict
Examples
--------
'>>> import_files()'
File Data_AprF_2019 is successfully imported
File Data_AugF_2019 is successfully imported
File Data_JulF_2019 is successfully imported
File Data_JunF_2019_v1 is successfully imported
File Data_MayF_2019 is successfully imported
File Data_SepP_2019 is successfully imported
'>>> import_files(like = ['Aug','Sep'])'
File Data_AugF_2019 is successfully imported
File Data_SepP_2019 is successfully imported
"""
if not isinstance(path, str):
raise ValueError("Please input path as a string")
elif not isinstance(doctype, str):
raise ValueError("Please input doctype as a string")
elif not isinstance(sheet, str):
raise ValueError("Please input sheet as a string")
elif not isinstance(subdir, bool):
raise ValueError("Please input subdir as a bool")
elif not isinstance(like, list):
raise ValueError("Please input like as a list")
elif not isinstance(strict, bool):
raise ValueError("Please input strict as a bool")
else:
pass
dict_files = {}
if subdir is True:
for r, d, f in os.walk(path):
for file in f:
b = any(x in file for x in like)
if strict is True:
b = all(x in file for x in like)
if (file.split(".")[-1] == doctype) & (b is True):
k = file.strip("." + doctype)
try:
name = os.path.join(r, file)
print("\nImporting " + k + "...", end="", flush=True)
if doctype == "csv":
dict_files[name.strip(".\\").strip(".csv")] = pd.read_csv(
name
)
print("\rFile " + k + " is successfully imported")
else:
dict_files[
name.strip(".\\").strip(".xlsx")
] = pd.read_excel(name, sheet_name=sheet)
print("\rFile " + k + " is successfully imported")
except Exception as ex:
raise (ex)
else:
for file in os.listdir(path):
b = any(x in file for x in like)
if strict is True:
b = all(x in file for x in like)
if (file.split(".")[-1] == doctype) & (b is True):
k = file.strip("." + doctype)
try:
name = os.path.join(path, file)
print("\nImporting " + k + "...", end="", flush=True)
if doctype == "csv":
dict_files[k] = pd.read_csv(name)
print("\rFile " + k + " is successfully imported")
else:
dict_files[k] = pd.read_excel(name, sheet_name=sheet)
print("\rFile " + k + " is successfully imported")
except Exception as ex:
raise (ex)
return dict_files
def compare(x, y, names=["x", "y"], dups=False, same=False, comment=False):
"""
This function returns a dictionary with:
1. Same values between data frames x and y
2. Values in x, not in y
3. Values in y, not in x
(optional):
(4) Duplicates of x
(5) Duplicates of y
(6) Boolean of whether x and y are the same
Parameters
----------
x : pandas.DataFrame
DataFrame #1
y : pandas.DataFrame
DataFrame #2
names : list
a list of user preferred file names
e.g. ['File1', 'File2']
default = ['x','y']
dups : bool
True to include duplicates check for each file
default = False
same : bool
True to activate. Outputs True if DataFrames are the same
default = False
comment : bool
True to activate. Prints out statistics of the compariosn results
e.g. number of same valeus, number of duplicates, number of outliers and whether the DataFrames are the same
default = False
Returns
-------
out : dict
Examples
--------
'>>> c = compare(df1, df2, names = ['df1','df2'], dups = True, same = True, comment =True)'
There are 133891 same values
There are 16531 outliers in df1
There are 20937 outliers in df2
There are 48704 duplicates in df1
There are 0 duplicates in df2
The DataFrames are not the same
'>>> c = compare(df2, df2, names = ['df2','df2'], dups = True, same = True, comment =True)'
There are 154444 same values
There are 0 outliers in df2
There are 0 outliers in df2
There are 0 duplicates in df2
There are 0 duplicates in df2
The DataFrames are the same
"""
if not isinstance(x, pd.DataFrame):
raise ValueError("Please input x as a pandas.DataFrame")
elif not isinstance(y, pd.DataFrame):
raise ValueError("Please input y as a pandas.DataFrame")
elif not isinstance(names, list):
raise ValueError("Please input names as a list")
elif not isinstance(dups, bool):
raise ValueError("Please input dups as a bool")
elif not isinstance(same, bool):
raise ValueError("Please input same as a bool")
elif not isinstance(comment, bool):
raise ValueError("Please input comment as a bool")
dict_temp = {}
try:
dict_temp["same_values"] = pd.merge(
x.drop_duplicates(), y.drop_duplicates(), how="inner"
)
except Exception as ex:
raise (ex)
try:
dict_temp[names[0] + "_not_" + names[1]] = pd.concat(
[x, dict_temp["same_values"]], ignore_index=True
).drop_duplicates(keep=False)
dict_temp[names[1] + "_not_" + names[0]] = pd.concat(
[y, dict_temp["same_values"]], ignore_index=True
).drop_duplicates(keep=False)
except Exception as ex:
raise (ex)
if dups is True:
try:
dict_temp[names[0] + "_dups"] = x[x.duplicated()]
dict_temp[names[1] + "_dups"] = y[y.duplicated()]
except Exception as ex:
raise (ex)
if same is True:
try:
if (x.shape == y.shape) & (x.shape == dict_temp["same_values"].shape):
dict_temp["Same"] = True
else:
dict_temp["Same"] = False
except Exception as ex:
raise (ex)
try:
if comment is True:
print(
"\nThere are " + str(dict_temp["same_values"].shape[0]) + " same values"
)
print(
"There are "
+ str(dict_temp[names[0] + "_not_" + names[1]].shape[0])
+ " outliers in "
+ str(names[0])
)
print(
"There are "
+ str(dict_temp[names[1] + "_not_" + names[0]].shape[0])
+ " outliers in "
+ str(names[1])
)
if dups is True:
print(
"There are "
+ str(dict_temp[names[0] + "_dups"].shape[0])
+ " duplicates in "
+ names[0]
)
print(
"There are "
+ str(dict_temp[names[1] + "_dups"].shape[0])
+ " duplicates in "
+ names[1]
)
if same is True:
if dict_temp["Same"] is True:
s = "the same"
else:
s = "not the same"
print("DataFrames are " + s)
except Exception as ex:
raise (ex)
return dict_temp