-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrom_profiling_pandas.py
More file actions
141 lines (120 loc) · 4.91 KB
/
Copy pathfrom_profiling_pandas.py
File metadata and controls
141 lines (120 loc) · 4.91 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
from urllib.parse import urlparse
from pathlib import Path
import pandas as pd
from sweetviz.config import config
# This file contains modified functions from the profiling-pandas library,
# which you should check out at the following URL:
# https://github.com/pandas-profiling/pandas-profiling
#
# Used under the following license:
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Jos Polfliet, 2019-2020 Simon Brugman
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def is_boolean(series: pd.Series, counts: dict) -> bool:
keys = counts["value_counts_without_nan"].keys()
if pd.api.types.is_bool_dtype(keys):
return True
elif (
1 <= counts["distinct_count_without_nan"] <= 2
and pd.api.types.is_numeric_dtype(series)
and series[~series.isnull()].between(0, 1).all()
):
return True
elif 1 <= counts["distinct_count_without_nan"] <= 4:
unique_values = set([str(value).lower() for value in keys.values])
accepted_combinations = [
["y", "n"],
["yes", "no"],
["true", "false"],
["t", "f"],
]
if len(unique_values) == 2 and any(
[unique_values == set(bools) for bools in
accepted_combinations]
):
return True
return False
def is_categorical(series: pd.Series, counts: dict) -> bool:
keys = counts["value_counts_without_nan"].keys()
# TODO: CHECK THIS CASE ACTUALLY WORKS
if pd.api.types.is_categorical_dtype(keys):
return True
elif pd.api.types.is_numeric_dtype(series) and \
counts["distinct_count_without_nan"] \
<= config["Type_Detection"].getint("max_numeric_distinct_to_be_categorical"):
return True
else:
if counts["num_rows_with_data"] == 0:
return False
num_distinct = counts["distinct_count_without_nan"]
fraction_distinct = num_distinct / float(counts["num_rows_with_data"])
if fraction_distinct \
> config["Type_Detection"].getfloat("max_text_fraction_distinct_to_be_categorical"):
return False
if num_distinct <= config["Type_Detection"].getint("max_text_distinct_to_be_categorical"):
return True
return False
def is_numeric(series: pd.Series, counts: dict) -> bool:
return pd.api.types.is_numeric_dtype(series) and \
counts["distinct_count_without_nan"] \
> config["Type_Detection"].getint("max_numeric_distinct_to_be_categorical")
# For coercion, might need more testing!
def could_be_numeric(series: pd.Series) -> bool:
return pd.api.types.is_numeric_dtype(series)
def is_url(series: pd.Series, counts: dict) -> bool:
if counts["distinct_count_without_nan"] > 0:
try:
result = series[~series.isnull()].astype(str).apply(urlparse)
return result.apply(
lambda x: all([x.scheme, x.netloc, x.path])).all()
except ValueError:
return False
else:
return False
def str_is_path(p: str) -> bool:
"""Detects if the variable contains absolute paths. If so, we distinguish
paths that exist and paths that are images.
Args:
p: the Path
Returns:
True is is an absolute path
"""
try:
path = Path(p)
if path.is_absolute():
return True
else:
return False
except TypeError:
return False
def is_path(series, counts) -> bool:
if counts["distinct_count_without_nan"] > 0:
try:
result = series[~series.isnull()].astype(str).apply(str_is_path)
return result.all()
except ValueError:
return False
else:
return False
def is_date(series) -> bool:
is_date_value = pd.api.types.is_datetime64_dtype(series)
return is_date_value