-
Notifications
You must be signed in to change notification settings - Fork 3
/
X_Data Profiling.py
207 lines (161 loc) · 5.07 KB
/
X_Data Profiling.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
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
#%% [markdown]
# # Data Profiling Playbook
# The intention of this notebook is to develop a generic approach to profiling a single column.
# This will take into account:
# - The primary data type of the column - eg if it's a date, we can generate more specialised analytics
# - We may also be able to apply more specialised checks, for example based on REGEX to check national insurance numbers etc.
#%%
# Import all of the libraries we need to use...
import pandas as pd
import azureml.dataprep as dprep
import os as os
import re as re
import collections
import seaborn as sns
import pandas_profiling as pp
import datetime
from datetime import datetime
from azureml.dataprep import value
from azureml.dataprep import col
from azureml.dataprep import Dataflow
from commonCode import savePackage, openPackage, createFullPackagePath
from commonPackageHandling import openDataFlowPackage
#%%
dataFlow = openDataFlowPackage('PEOPLE', '22', 'A')
#%%
dataFlow.head(10)
#%%
dataProfile = dataFlow.get_profile()
dataProfile
#%%
dataFlow.row_count
#%%
builder = dataFlow.builders.set_column_types()
#%%
builder.learn()
#%%
builder.conversion_candidates
#%%
builder.ambiguous_date_columns
#%%
dataFlow = builder.to_dataflow()
#%%
# Not used fo now, but should be driven by:
stageNumber = '1'
dataName = 'PEOPLE'
qualityFlag = 'A'
noMissingFlag = True
# For now, I'm cheating, just specifying file. But will use helper function to build ultimately:
dataFlowFile = './packages/PEOPLE/23/PEOPLE_A_package.dprep'
indexColumn = 'ID'
targetColumn = 'DOB'
#%%
dataFlow = Dataflow.open(dataFlowFile)
#%%
dataFlow = dataFlow.distinct(['ID'])
#%%
dataFlow = dataFlow.distinct(['NINO'])
#%%
dataFlow = dataFlow.keep_columns([indexColumn, targetColumn])
#%%
dataProfile = dataFlow.get_profile()
#%%
dataProfile
#%%
columnDataProfile = dataProfile.columns[targetColumn]
#%%
columnDataProfile
#%%
if columnDataProfile.type == 'FieldType.DATE':
print('Date Field Detected!')
# NOTE - what do I need to do to detect date field?
#%%
# Add year
columnByExampleBuilder = dataFlow.builders.derive_column_by_example(source_columns = [targetColumn], new_column_name = 'Year')
columnByExampleBuilder.add_example(source_data = {targetColumn : '2008-10-25 00:00:00'}, example_value = '2008')
columnByExampleBuilder.preview()
dataFlow = columnByExampleBuilder.to_dataflow()
#%%
# Add month
columnByExampleBuilder = dataFlow.builders.derive_column_by_example(source_columns = [targetColumn], new_column_name = 'Month')
columnByExampleBuilder.add_example(source_data = {targetColumn : '2008-10-25 00:00:00'}, example_value = 'October')
columnByExampleBuilder.preview()
dataFlow = columnByExampleBuilder.to_dataflow()
#%%
# Add day of month
columnByExampleBuilder = dataFlow.builders.derive_column_by_example(source_columns = [targetColumn], new_column_name = 'DayOfMonth')
columnByExampleBuilder.add_example(source_data = {targetColumn : '2008-10-25 00:00:00'}, example_value = '25')
columnByExampleBuilder.preview()
dataFlow = columnByExampleBuilder.to_dataflow()
#%%
# Add day of week
columnByExampleBuilder = dataFlow.builders.derive_column_by_example(source_columns = [targetColumn], new_column_name = 'DayOfWeek')
columnByExampleBuilder.add_example(source_data = {targetColumn : '2008-10-25 00:00:00'}, example_value = 'Saturday')
columnByExampleBuilder.preview()
dataFlow = columnByExampleBuilder.to_dataflow()
#%%
dataProfile = dataFlow.get_profile()
#%%
dataProfile
#%%
dataColumns = dataProfile.columns.keys()
#%%
dataColumns
#%%
for c in dataColumns:
valueCounts = dataProfile.columns[c].value_counts
if valueCounts == None:
valueCountString = 'None'
else:
valueCountString = len(valueCounts)
print('Column {0} : value count {1}'.format(c, valueCountString))
#%%
#%%
def plotValueCounts(dataProfile,columnName):
valueCounts = dataProfile.columns[columnName].value_counts
if valueCounts != None:
valueCountsDataFrame = pd.DataFrame(columns = ['ColumnName', 'Value', 'Count'])
for i in valueCounts:
valueCountsDataFrame = valueCountsDataFrame.append({ \
'Column' : c, \
'Value' : i.value, \
'Count' : i.count}, \
ignore_index = True)
if len(valueCountsDataFrame) > 40:
plot = sns.barplot(x='Value', y='Count', data=valueCountsDataFrame.head(20))
plot
plot = sns.barplot(x='Value', y='Count', data=valueCountsDataFrame.tail(20))
plot
else:
plot = sns.barplot(x='Value', y='Count', data=valueCountsDataFrame)
plot
return plot
#%%
plot = plotValueCounts(dataProfile, 'Year')
#%%
plot
#%%
plot = plotValueCounts(dataProfile, 'Month')
#%%
plot
#%%
plot = plotValueCounts(dataProfile, 'Day')
#%%
plot
#%%
dataProfileValues = dataProfile.columns.values()
#%%
dataProfileValues
#%%
df = dataFlow.to_pandas_dataframe()
#%%
df = df.sort_values([targetColumn]).reset_index(drop=True)
#%%
df
#%%
profileReport = pp.ProfileReport(df, check_correlation = False)
profileReport.to_file('./profileReport.html')
#%%
plot = sns.countplot(x="Year", data=df)
#%%
plot