showstats 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- showstats/__init__.py +3 -0
- showstats/showstats.py +208 -0
- showstats/utils.py +37 -0
- showstats-0.0.1.dist-info/METADATA +60 -0
- showstats-0.0.1.dist-info/RECORD +7 -0
- showstats-0.0.1.dist-info/WHEEL +4 -0
- showstats-0.0.1.dist-info/licenses/LICENSE +21 -0
showstats/__init__.py
ADDED
showstats/showstats.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# Central functions for table making
|
|
2
|
+
from typing import TYPE_CHECKING, Dict, Union
|
|
3
|
+
|
|
4
|
+
import polars as pl
|
|
5
|
+
|
|
6
|
+
from showstats.utils import _format_num_rows
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
import pandas
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _make_tables(
|
|
13
|
+
df: Union[pl.DataFrame, "pandas.DataFrame"],
|
|
14
|
+
) -> Dict[str, pl.DataFrame]:
|
|
15
|
+
"""
|
|
16
|
+
Calculate summary statistics for a DataFrame.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
df (pl.DataFrame): The input DataFrame. If not a polars.DataFrame, will try
|
|
20
|
+
to cast
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
Dict[str, pl.DataFrame]: A dictionary of summary statistics DataFrames for each data type.
|
|
24
|
+
"""
|
|
25
|
+
from polars import selectors as cs
|
|
26
|
+
|
|
27
|
+
functions = {}
|
|
28
|
+
functions_all = ["null_count", "min", "max"]
|
|
29
|
+
|
|
30
|
+
# Map vars to functions
|
|
31
|
+
vars = {}
|
|
32
|
+
cols_num = df.select(
|
|
33
|
+
pl.col(
|
|
34
|
+
pl.Decimal,
|
|
35
|
+
pl.Float32,
|
|
36
|
+
pl.Float64,
|
|
37
|
+
pl.Int16,
|
|
38
|
+
pl.Int32,
|
|
39
|
+
pl.Int64,
|
|
40
|
+
pl.Int8,
|
|
41
|
+
pl.UInt16,
|
|
42
|
+
pl.UInt32,
|
|
43
|
+
pl.UInt64,
|
|
44
|
+
pl.UInt8,
|
|
45
|
+
pl.Boolean,
|
|
46
|
+
)
|
|
47
|
+
).columns
|
|
48
|
+
if len(cols_num) > 0:
|
|
49
|
+
vars["num"] = cols_num
|
|
50
|
+
functions["num"] = functions_all + ["mean", "median", "std"]
|
|
51
|
+
|
|
52
|
+
vars_cat = df.select(
|
|
53
|
+
pl.col(pl.String), pl.col(pl.Enum), pl.col(pl.Categorical)
|
|
54
|
+
).columns
|
|
55
|
+
if len(vars_cat) > 0:
|
|
56
|
+
vars["cat"] = vars_cat
|
|
57
|
+
functions["cat"] = functions_all + ["n_unique"]
|
|
58
|
+
|
|
59
|
+
vars_datetime = df.select(cs.datetime()).columns
|
|
60
|
+
if len(vars_datetime) > 0:
|
|
61
|
+
vars["datetime"] = vars_datetime
|
|
62
|
+
functions["datetime"] = functions_all + ["mean", "median"]
|
|
63
|
+
|
|
64
|
+
vars_date = df.select(cs.date()).columns
|
|
65
|
+
if len(vars_date) > 0:
|
|
66
|
+
vars["date"] = vars_date
|
|
67
|
+
functions["date"] = functions_all
|
|
68
|
+
|
|
69
|
+
vars_null = df.select(pl.col(pl.Null)).columns
|
|
70
|
+
if len(vars_null) > 0:
|
|
71
|
+
vars["null"] = vars_null
|
|
72
|
+
functions["null"] = ["null_count"]
|
|
73
|
+
|
|
74
|
+
exprs = []
|
|
75
|
+
for var_type in vars:
|
|
76
|
+
functions_var_type = functions[var_type]
|
|
77
|
+
vars_var_type = vars[var_type]
|
|
78
|
+
for var in vars_var_type:
|
|
79
|
+
for fun in functions_var_type:
|
|
80
|
+
varname = f"{fun}_{var}"
|
|
81
|
+
expr = getattr(pl.col(var), fun)().alias(varname)
|
|
82
|
+
exprs.append(expr)
|
|
83
|
+
|
|
84
|
+
# Compute summary statistics in one go, leveraging Polars' query planner
|
|
85
|
+
stats = df.select(exprs).row(0, named=True)
|
|
86
|
+
|
|
87
|
+
# Make split summary tables
|
|
88
|
+
dfs = {}
|
|
89
|
+
for var_type in vars:
|
|
90
|
+
functions_var_type = functions[var_type]
|
|
91
|
+
vars_var_type = vars[var_type]
|
|
92
|
+
rows = []
|
|
93
|
+
for var in vars_var_type:
|
|
94
|
+
row = {"Variable": var}
|
|
95
|
+
for fun in functions_var_type:
|
|
96
|
+
row[fun] = stats[f"{fun}_{var}"]
|
|
97
|
+
rows.append(row)
|
|
98
|
+
dfs[var_type] = pl.DataFrame(rows)
|
|
99
|
+
|
|
100
|
+
return dfs
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def make_stats_df(df: Union[pl.DataFrame, "pandas.DataFrame"]) -> pl.DataFrame:
|
|
104
|
+
"""
|
|
105
|
+
Create a summary table for the given DataFrame.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
df (pl.DataFrame): The input DataFrame.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
pl.DataFrame: A summary table with statistics for each variable.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
if isinstance(df, pl.DataFrame) is False:
|
|
115
|
+
print("Attempting to convert input to polars.DataFrame")
|
|
116
|
+
try:
|
|
117
|
+
df = pl.DataFrame(df)
|
|
118
|
+
except Exception as e:
|
|
119
|
+
print(f"Error occurred during attempted conversion: {e}")
|
|
120
|
+
|
|
121
|
+
dfs = _make_tables(df)
|
|
122
|
+
num_rows = df.height
|
|
123
|
+
varnames = [
|
|
124
|
+
"Variable",
|
|
125
|
+
"null_count",
|
|
126
|
+
"mean",
|
|
127
|
+
"median",
|
|
128
|
+
"std",
|
|
129
|
+
"min",
|
|
130
|
+
"max",
|
|
131
|
+
]
|
|
132
|
+
var_types = [x for x in ["num", "datetime", "date", "cat", "null"] if x in dfs]
|
|
133
|
+
|
|
134
|
+
# Order
|
|
135
|
+
for var_type in var_types:
|
|
136
|
+
df_var_type = dfs[var_type].lazy()
|
|
137
|
+
df_var_type = (
|
|
138
|
+
df_var_type.with_columns(pl.selectors.float().round(2))
|
|
139
|
+
.with_columns(
|
|
140
|
+
pl.col("null_count")
|
|
141
|
+
.truediv(num_rows)
|
|
142
|
+
.alias("perc_missing")
|
|
143
|
+
.mul(100)
|
|
144
|
+
.round(1)
|
|
145
|
+
)
|
|
146
|
+
.with_columns(
|
|
147
|
+
pl.format("{} ({}%)", pl.col("null_count"), pl.col("perc_missing"))
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
# Special conversion for datetimes
|
|
151
|
+
if var_type == "datetime":
|
|
152
|
+
df_var_type = df_var_type.with_columns(
|
|
153
|
+
pl.col("mean", "median", "min", "max").dt.to_string("%Y-%m-%d %H:%M:%S")
|
|
154
|
+
)
|
|
155
|
+
else:
|
|
156
|
+
df_var_type = df_var_type.with_columns(pl.col("*").cast(pl.String))
|
|
157
|
+
# Add missing values as ""
|
|
158
|
+
for col_name in varnames:
|
|
159
|
+
if col_name not in dfs[var_type].columns:
|
|
160
|
+
df_var_type = df_var_type.with_columns(pl.lit("").alias(col_name))
|
|
161
|
+
dfs[var_type] = df_var_type.select(varnames)
|
|
162
|
+
|
|
163
|
+
thr = 100_000
|
|
164
|
+
if num_rows < thr:
|
|
165
|
+
name_var = f"Var; N = {_format_num_rows(num_rows, thr)}"
|
|
166
|
+
else:
|
|
167
|
+
name_var = f"Var; N \u2248 {_format_num_rows(num_rows, thr)}"
|
|
168
|
+
return (
|
|
169
|
+
pl.concat([dfs[key] for key in var_types])
|
|
170
|
+
.rename(
|
|
171
|
+
{
|
|
172
|
+
"Variable": name_var,
|
|
173
|
+
"null_count": "Missing",
|
|
174
|
+
"mean": "Mean",
|
|
175
|
+
"median": "Median",
|
|
176
|
+
"std": "Std.",
|
|
177
|
+
"min": "Min",
|
|
178
|
+
"max": "Max",
|
|
179
|
+
}
|
|
180
|
+
)
|
|
181
|
+
.collect()
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def show_stats(df: Union[pl.DataFrame, "pandas.DataFrame"]) -> None:
|
|
186
|
+
"""
|
|
187
|
+
Print a summary table for the given DataF‚rame.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
df (pl.DataFrame): The input DataFrame.
|
|
191
|
+
"""
|
|
192
|
+
from polars import Config
|
|
193
|
+
|
|
194
|
+
if df.height == 0 or df.width == 0:
|
|
195
|
+
raise ValueError("Input data frame must have rows and columns")
|
|
196
|
+
|
|
197
|
+
stats_df = make_stats_df(df)
|
|
198
|
+
cfg = Config(
|
|
199
|
+
tbl_hide_dataframe_shape=True,
|
|
200
|
+
tbl_formatting="ASCII_MARKDOWN",
|
|
201
|
+
tbl_hide_column_data_types=True,
|
|
202
|
+
float_precision=2,
|
|
203
|
+
fmt_str_lengths=100,
|
|
204
|
+
set_tbl_rows=stats_df.height,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
with cfg:
|
|
208
|
+
print(stats_df)
|
showstats/utils.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Utility functions
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _format_num_rows(num: int, thr: float) -> str:
|
|
5
|
+
"""
|
|
6
|
+
Formats a number nicely, using scientific notation for large numbers.
|
|
7
|
+
|
|
8
|
+
Args:
|
|
9
|
+
num (int): The number to format.
|
|
10
|
+
thr (int): The threshold above which to use scientific notation.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
str: The formatted number as a string.
|
|
14
|
+
"""
|
|
15
|
+
import math
|
|
16
|
+
|
|
17
|
+
if num < thr:
|
|
18
|
+
return f"{num:,.0f}"
|
|
19
|
+
|
|
20
|
+
exponent = int(math.floor(math.log10(abs(num))))
|
|
21
|
+
coefficient = num / 10**exponent
|
|
22
|
+
|
|
23
|
+
# Unicode superscript digits
|
|
24
|
+
superscripts = "⁰¹²³⁴⁵⁶⁷⁸⁹"
|
|
25
|
+
|
|
26
|
+
# Convert exponent to superscript
|
|
27
|
+
exp_superscript = "".join(superscripts[int(d)] for d in str(abs(exponent)))
|
|
28
|
+
if exponent < 0:
|
|
29
|
+
exp_superscript = "⁻" + exp_superscript
|
|
30
|
+
|
|
31
|
+
return f"{coefficient:.2f}×10{exp_superscript}"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _is_pkg_available(pkg: str) -> None:
|
|
35
|
+
import importlib
|
|
36
|
+
|
|
37
|
+
return importlib.util.find_spec(pkg) is not None
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: showstats
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Vertical summary statistics for data frames
|
|
5
|
+
Project-URL: Homepage, https://github.com/matthiaskaeding/showstats
|
|
6
|
+
Project-URL: Issues, https://github.com/matthiaskaeding/showstats/issues
|
|
7
|
+
Author: Matthias Kaeding
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Requires-Dist: polars>=0.20.21
|
|
14
|
+
Provides-Extra: pandas
|
|
15
|
+
Requires-Dist: pandas>=1.5.3; extra == 'pandas'
|
|
16
|
+
Requires-Dist: pyarrow>=10.0.0; extra == 'pandas'
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# showstats: quick and compact summary statistics
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
**showstats** produces summary statistic tables with vertical
|
|
23
|
+
orientation.
|
|
24
|
+
|
|
25
|
+
``` python
|
|
26
|
+
from showstats import show_stats
|
|
27
|
+
|
|
28
|
+
show_stats(df)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
| Var; N = 100 | Missing | Mean | Median | Std. | Min | Max |
|
|
32
|
+
|---------------|--------------|--------------|--------------|-------|--------------|--------------|
|
|
33
|
+
| int_col | 0 (0.0%) | 49.5 | 49.5 | 29.01 | 0.0 | 99.0 |
|
|
34
|
+
| int_with_miss | 20 (20.0%) | 57.0 | 59.5 | 27.6 | 0.0 | 99.0 |
|
|
35
|
+
| ing | | | | | | |
|
|
36
|
+
| float_col | 0 (0.0%) | 0.5 | 0.5 | 0.29 | 0.0 | 0.99 |
|
|
37
|
+
| float_col_wit | 0 (0.0%) | 2.0 | 2.06 | 0.94 | -0.79 | 4.44 |
|
|
38
|
+
| h_mean_2 | | | | | | |
|
|
39
|
+
| float_col_wit | 0 (0.0%) | -0.11 | 0.02 | 2.0 | -6.01 | 5.06 |
|
|
40
|
+
| h_std_2 | | | | | | |
|
|
41
|
+
| float_col_wit | 0 (0.0%) | 9.79 | 9.85 | 0.94 | 7.0 | 12.22 |
|
|
42
|
+
| h_min_7 | | | | | | |
|
|
43
|
+
| float_col_wit | 0 (0.0%) | 14.56 | 14.62 | 0.94 | 11.78 | 17.0 |
|
|
44
|
+
| h_max_17 | | | | | | |
|
|
45
|
+
| bool_col | 0 (0.0%) | 0.5 | 0.5 | 0.5 | 0.0 | 1.0 |
|
|
46
|
+
| datetime_col | 0 (0.0%) | 2022-01-01 | 2022-01-01 | | 2022-01-01 | 2022-01-01 |
|
|
47
|
+
| | | 00:00:49 | 00:00:49 | | 00:00:00 | 00:01:39 |
|
|
48
|
+
| datetime_col_ | 0 (0.0%) | 1995-01-01 | 1995-01-01 | | 1995-01-01 | 1995-01-01 |
|
|
49
|
+
| 2 | | 00:00:49 | 00:00:49 | | 00:00:00 | 00:01:39 |
|
|
50
|
+
| date_col | 0 (0.0%) | | | | 2022-01-01 | 2022-04-10 |
|
|
51
|
+
| date_col_2 | 0 (0.0%) | | | | 1500-01-01 | 1500-04-10 |
|
|
52
|
+
| str_col | 0 (0.0%) | | | | ABC | foo |
|
|
53
|
+
| enum_col | 0 (0.0%) | | | | low | high |
|
|
54
|
+
| categorical_c | 0 (0.0%) | | | | low | medium |
|
|
55
|
+
| ol | | | | | | |
|
|
56
|
+
| null_col | 100 (100.0%) | | | | | |
|
|
57
|
+
|
|
58
|
+
Primarily built for polars data frames. **showstats** converts other
|
|
59
|
+
inputs, for compatibility with pandas.DataFrames install as
|
|
60
|
+
`pip install showstats[pandas]`
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
showstats/__init__.py,sha256=OWa_lNYDUR_q_socZ9lO86SMrtGOvVlPP6BaqySs9ng,60
|
|
2
|
+
showstats/showstats.py,sha256=LnhwYAJM9tmqwENp7tg3KekX2_rDipbGVxIQKWpccrc,5978
|
|
3
|
+
showstats/utils.py,sha256=yCNdxuD_sGobCwkFdsTy50kOax5mU07RJkYmTIKtliY,949
|
|
4
|
+
showstats-0.0.1.dist-info/METADATA,sha256=_KwudXoOTmxppDfrHsL3sDvWSJ0IdrsHI0UJaZcgg7U,3741
|
|
5
|
+
showstats-0.0.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
+
showstats-0.0.1.dist-info/licenses/LICENSE,sha256=eFm_5oC7njV7uQ08GSk6w0n-y6sCH2N6US5NsyLj8E8,1072
|
|
7
|
+
showstats-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Matthias Kaeding
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|