showstats 0.0.1__tar.gz
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-0.0.1/.github/workflows/lint.yaml +27 -0
- showstats-0.0.1/.github/workflows/pytest.yaml +25 -0
- showstats-0.0.1/.gitignore +185 -0
- showstats-0.0.1/.pre-commit-config.yaml +19 -0
- showstats-0.0.1/LICENSE +21 -0
- showstats-0.0.1/Makefile +72 -0
- showstats-0.0.1/PKG-INFO +60 -0
- showstats-0.0.1/README.md +42 -0
- showstats-0.0.1/README.qmd +31 -0
- showstats-0.0.1/changelog.md +12 -0
- showstats-0.0.1/data/README.md +11 -0
- showstats-0.0.1/data/external/.gitkeep +0 -0
- showstats-0.0.1/data/interim/.gitkeep +0 -0
- showstats-0.0.1/data/processed/.gitkeep +0 -0
- showstats-0.0.1/data/raw/.gitkeep +0 -0
- showstats-0.0.1/dev-requirements.txt +13 -0
- showstats-0.0.1/docs/.gitkeep +0 -0
- showstats-0.0.1/notebooks/.gitkeep +0 -0
- showstats-0.0.1/noxfile.py +31 -0
- showstats-0.0.1/pyproject.toml +31 -0
- showstats-0.0.1/references/.gitkeep +0 -0
- showstats-0.0.1/references/README.md +1 -0
- showstats-0.0.1/references/figures/.gitkeep +0 -0
- showstats-0.0.1/reports/.gitkeep +0 -0
- showstats-0.0.1/reports/figures/.gitkeep +0 -0
- showstats-0.0.1/scripts/.gitkeep +0 -0
- showstats-0.0.1/scripts/README.md +1 -0
- showstats-0.0.1/src/showstats/__init__.py +3 -0
- showstats-0.0.1/src/showstats/showstats.py +208 -0
- showstats-0.0.1/src/showstats/utils.py +37 -0
- showstats-0.0.1/tests/__init__.py +0 -0
- showstats-0.0.1/tests/conftest.py +119 -0
- showstats-0.0.1/tests/test_dfstats.py +29 -0
- showstats-0.0.1/tests/test_make_stats_df.py +29 -0
- showstats-0.0.1/tests/test_make_tables.py +71 -0
- showstats-0.0.1/tests/test_show.py +31 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: Lint
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
lint:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
|
|
16
|
+
- name: Set up Python
|
|
17
|
+
uses: actions/setup-python@v4
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.11.9"
|
|
20
|
+
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip
|
|
24
|
+
pip install ruff
|
|
25
|
+
|
|
26
|
+
- name: Run Ruff
|
|
27
|
+
run: ruff check .
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: Python Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11.9"
|
|
19
|
+
cache: "pip" # caching pip dependencies
|
|
20
|
+
- run: |
|
|
21
|
+
pip install -r dev-requirements.txt
|
|
22
|
+
pip install pytest
|
|
23
|
+
|
|
24
|
+
- name: Run tests
|
|
25
|
+
run: pytest -v -m "not integration"
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Data
|
|
2
|
+
/data/*
|
|
3
|
+
!/data/*/
|
|
4
|
+
/data/*/*
|
|
5
|
+
!/data/*/.gitkeep
|
|
6
|
+
!/data/README.md
|
|
7
|
+
|
|
8
|
+
# Mac OS-specific storage files
|
|
9
|
+
.DS_Store
|
|
10
|
+
|
|
11
|
+
# vim
|
|
12
|
+
*.swp
|
|
13
|
+
*.swo
|
|
14
|
+
|
|
15
|
+
## https://github.com/github/gitignore/blob/4488915eec0b3a45b5c63ead28f286819c0917de/Python.gitignore
|
|
16
|
+
|
|
17
|
+
# Byte-compiled / optimized / DLL files
|
|
18
|
+
__pycache__/
|
|
19
|
+
*.py[cod]
|
|
20
|
+
*$py.class
|
|
21
|
+
|
|
22
|
+
# C extensions
|
|
23
|
+
*.so
|
|
24
|
+
|
|
25
|
+
# Distribution / packaging
|
|
26
|
+
.Python
|
|
27
|
+
build/
|
|
28
|
+
develop-eggs/
|
|
29
|
+
dist/
|
|
30
|
+
downloads/
|
|
31
|
+
eggs/
|
|
32
|
+
.eggs/
|
|
33
|
+
lib/
|
|
34
|
+
lib64/
|
|
35
|
+
parts/
|
|
36
|
+
sdist/
|
|
37
|
+
var/
|
|
38
|
+
wheels/
|
|
39
|
+
share/python-wheels/
|
|
40
|
+
*.egg-info/
|
|
41
|
+
.installed.cfg
|
|
42
|
+
*.egg
|
|
43
|
+
MANIFEST
|
|
44
|
+
|
|
45
|
+
# PyInstaller
|
|
46
|
+
# Usually these files are written by a python script from a template
|
|
47
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
48
|
+
*.manifest
|
|
49
|
+
*.spec
|
|
50
|
+
|
|
51
|
+
# Installer logs
|
|
52
|
+
pip-log.txt
|
|
53
|
+
pip-delete-this-directory.txt
|
|
54
|
+
|
|
55
|
+
# Unit test / coverage reports
|
|
56
|
+
htmlcov/
|
|
57
|
+
.tox/
|
|
58
|
+
.nox/
|
|
59
|
+
.coverage
|
|
60
|
+
.coverage.*
|
|
61
|
+
.cache
|
|
62
|
+
nosetests.xml
|
|
63
|
+
coverage.xml
|
|
64
|
+
*.cover
|
|
65
|
+
*.py,cover
|
|
66
|
+
.hypothesis/
|
|
67
|
+
.pytest_cache/
|
|
68
|
+
cover/
|
|
69
|
+
|
|
70
|
+
# Translations
|
|
71
|
+
*.mo
|
|
72
|
+
*.pot
|
|
73
|
+
|
|
74
|
+
# Django stuff:
|
|
75
|
+
*.log
|
|
76
|
+
local_settings.py
|
|
77
|
+
db.sqlite3
|
|
78
|
+
db.sqlite3-journal
|
|
79
|
+
|
|
80
|
+
# Flask stuff:
|
|
81
|
+
instance/
|
|
82
|
+
.webassets-cache
|
|
83
|
+
|
|
84
|
+
# Scrapy stuff:
|
|
85
|
+
.scrapy
|
|
86
|
+
|
|
87
|
+
# MkDocs documentation
|
|
88
|
+
docs/site/
|
|
89
|
+
|
|
90
|
+
# PyBuilder
|
|
91
|
+
.pybuilder/
|
|
92
|
+
target/
|
|
93
|
+
|
|
94
|
+
# Jupyter Notebook
|
|
95
|
+
.ipynb_checkpoints
|
|
96
|
+
|
|
97
|
+
# IPython
|
|
98
|
+
profile_default/
|
|
99
|
+
ipython_config.py
|
|
100
|
+
|
|
101
|
+
# pyenv
|
|
102
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
103
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
104
|
+
# .python-version
|
|
105
|
+
|
|
106
|
+
# pipenv
|
|
107
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
108
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
109
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
110
|
+
# install all needed dependencies.
|
|
111
|
+
#Pipfile.lock
|
|
112
|
+
|
|
113
|
+
# poetry
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
115
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
116
|
+
# commonly ignored for libraries.
|
|
117
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
118
|
+
#poetry.lock
|
|
119
|
+
|
|
120
|
+
# pdm
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
122
|
+
#pdm.lock
|
|
123
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
124
|
+
# in version control.
|
|
125
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
126
|
+
.pdm.toml
|
|
127
|
+
|
|
128
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
129
|
+
__pypackages__/
|
|
130
|
+
|
|
131
|
+
# Celery stuff
|
|
132
|
+
celerybeat-schedule
|
|
133
|
+
celerybeat.pid
|
|
134
|
+
|
|
135
|
+
# SageMath parsed files
|
|
136
|
+
*.sage.py
|
|
137
|
+
|
|
138
|
+
# Environments
|
|
139
|
+
.env
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# Figures
|
|
180
|
+
references/figures/*
|
|
181
|
+
!references/figures/.gitkeep
|
|
182
|
+
.vscode/settings.json
|
|
183
|
+
|
|
184
|
+
src/experiments.py
|
|
185
|
+
.python-version
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
3
|
+
rev: v0.5.0
|
|
4
|
+
hooks:
|
|
5
|
+
- id: ruff
|
|
6
|
+
types_or: [python, pyi, jupyter]
|
|
7
|
+
args: [--fix]
|
|
8
|
+
- id: ruff-format
|
|
9
|
+
types_or: [python, pyi, jupyter]
|
|
10
|
+
|
|
11
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
12
|
+
rev: v4.5.0 # Updated to the latest version you mentioned
|
|
13
|
+
hooks:
|
|
14
|
+
- id: check-added-large-files
|
|
15
|
+
args: ["--maxkb=2000"]
|
|
16
|
+
- id: trailing-whitespace
|
|
17
|
+
exclude: ".*(.csv|.txt)$"
|
|
18
|
+
- id: end-of-file-fixer
|
|
19
|
+
exclude: ".*(.csv|.txt)$"
|
showstats-0.0.1/LICENSE
ADDED
|
@@ -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.
|
showstats-0.0.1/Makefile
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Targets
|
|
2
|
+
|
|
3
|
+
## Install Python Dependencies
|
|
4
|
+
.PHONY: reqs
|
|
5
|
+
reqs:
|
|
6
|
+
uv pip install -r dev-requirements.txt
|
|
7
|
+
|
|
8
|
+
## Delete all compiled Python files
|
|
9
|
+
.PHONY: clean
|
|
10
|
+
clean:
|
|
11
|
+
find . -type f -name "*.py[co]" -delete
|
|
12
|
+
find . -type d -name "__pycache__" -delete
|
|
13
|
+
|
|
14
|
+
## Fix code using ruff
|
|
15
|
+
.PHONY: fix
|
|
16
|
+
fix:
|
|
17
|
+
ruff check --select I --fix
|
|
18
|
+
ruff check --fix
|
|
19
|
+
ruff format
|
|
20
|
+
|
|
21
|
+
## Run pytests
|
|
22
|
+
.PHONY: test
|
|
23
|
+
test:
|
|
24
|
+
pytest
|
|
25
|
+
|
|
26
|
+
test2: reqs test
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Make README
|
|
30
|
+
.PHONY: README.md
|
|
31
|
+
README.md: README.qmd src/showstats/showstats.py
|
|
32
|
+
quarto render README.qmd
|
|
33
|
+
|
|
34
|
+
## Run nox
|
|
35
|
+
.PHONY: nox
|
|
36
|
+
nox:
|
|
37
|
+
nox
|
|
38
|
+
|
|
39
|
+
## Build package
|
|
40
|
+
.PHONY: build
|
|
41
|
+
build:
|
|
42
|
+
python3 -m build
|
|
43
|
+
|
|
44
|
+
## Upload to pypi
|
|
45
|
+
.PHONY: upload-pypi
|
|
46
|
+
upload-pypi:
|
|
47
|
+
python3 -m twine upload --repository pypi dist/*
|
|
48
|
+
|
|
49
|
+
## Upload to test-pypi
|
|
50
|
+
.PHONY: upload-testpypi
|
|
51
|
+
python3 -m twine upload --repository testpypi dist/*
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## Test install
|
|
55
|
+
.PHONY: test-inst
|
|
56
|
+
test-inst:
|
|
57
|
+
uv pip install -i https://test.pypi.org/simple/ showstats
|
|
58
|
+
|
|
59
|
+
# Self Documenting Commands #
|
|
60
|
+
.DEFAULT_GOAL := help
|
|
61
|
+
|
|
62
|
+
define PRINT_HELP_PYSCRIPT
|
|
63
|
+
import re, sys; \
|
|
64
|
+
lines = '\n'.join([line for line in sys.stdin]); \
|
|
65
|
+
matches = re.findall(r'\n## (.*)\n[\s\S]+?\n([a-zA-Z_-]+):', lines); \
|
|
66
|
+
print('Available rules:\n'); \
|
|
67
|
+
print('\n'.join(['{:25}{}'.format(*reversed(match)) for match in matches]))
|
|
68
|
+
endef
|
|
69
|
+
export PRINT_HELP_PYSCRIPT
|
|
70
|
+
|
|
71
|
+
help:
|
|
72
|
+
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
|
showstats-0.0.1/PKG-INFO
ADDED
|
@@ -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,42 @@
|
|
|
1
|
+
# showstats: quick and compact summary statistics
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
**showstats** produces summary statistic tables with vertical
|
|
5
|
+
orientation.
|
|
6
|
+
|
|
7
|
+
``` python
|
|
8
|
+
from showstats import show_stats
|
|
9
|
+
|
|
10
|
+
show_stats(df)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
| Var; N = 100 | Missing | Mean | Median | Std. | Min | Max |
|
|
14
|
+
|---------------|--------------|--------------|--------------|-------|--------------|--------------|
|
|
15
|
+
| int_col | 0 (0.0%) | 49.5 | 49.5 | 29.01 | 0.0 | 99.0 |
|
|
16
|
+
| int_with_miss | 20 (20.0%) | 57.0 | 59.5 | 27.6 | 0.0 | 99.0 |
|
|
17
|
+
| ing | | | | | | |
|
|
18
|
+
| float_col | 0 (0.0%) | 0.5 | 0.5 | 0.29 | 0.0 | 0.99 |
|
|
19
|
+
| float_col_wit | 0 (0.0%) | 2.0 | 2.06 | 0.94 | -0.79 | 4.44 |
|
|
20
|
+
| h_mean_2 | | | | | | |
|
|
21
|
+
| float_col_wit | 0 (0.0%) | -0.11 | 0.02 | 2.0 | -6.01 | 5.06 |
|
|
22
|
+
| h_std_2 | | | | | | |
|
|
23
|
+
| float_col_wit | 0 (0.0%) | 9.79 | 9.85 | 0.94 | 7.0 | 12.22 |
|
|
24
|
+
| h_min_7 | | | | | | |
|
|
25
|
+
| float_col_wit | 0 (0.0%) | 14.56 | 14.62 | 0.94 | 11.78 | 17.0 |
|
|
26
|
+
| h_max_17 | | | | | | |
|
|
27
|
+
| bool_col | 0 (0.0%) | 0.5 | 0.5 | 0.5 | 0.0 | 1.0 |
|
|
28
|
+
| datetime_col | 0 (0.0%) | 2022-01-01 | 2022-01-01 | | 2022-01-01 | 2022-01-01 |
|
|
29
|
+
| | | 00:00:49 | 00:00:49 | | 00:00:00 | 00:01:39 |
|
|
30
|
+
| datetime_col_ | 0 (0.0%) | 1995-01-01 | 1995-01-01 | | 1995-01-01 | 1995-01-01 |
|
|
31
|
+
| 2 | | 00:00:49 | 00:00:49 | | 00:00:00 | 00:01:39 |
|
|
32
|
+
| date_col | 0 (0.0%) | | | | 2022-01-01 | 2022-04-10 |
|
|
33
|
+
| date_col_2 | 0 (0.0%) | | | | 1500-01-01 | 1500-04-10 |
|
|
34
|
+
| str_col | 0 (0.0%) | | | | ABC | foo |
|
|
35
|
+
| enum_col | 0 (0.0%) | | | | low | high |
|
|
36
|
+
| categorical_c | 0 (0.0%) | | | | low | medium |
|
|
37
|
+
| ol | | | | | | |
|
|
38
|
+
| null_col | 100 (100.0%) | | | | | |
|
|
39
|
+
|
|
40
|
+
Primarily built for polars data frames. **showstats** converts other
|
|
41
|
+
inputs, for compatibility with pandas.DataFrames install as
|
|
42
|
+
`pip install showstats[pandas]`
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "showstats: quick and compact summary statistics"
|
|
3
|
+
format: gfm
|
|
4
|
+
jupyter: python3
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
**showstats** produces summary statistic tables with vertical orientation.
|
|
8
|
+
|
|
9
|
+
```{python}
|
|
10
|
+
# | output: false
|
|
11
|
+
# | echo: false
|
|
12
|
+
import polars as pl
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
sys.path.append("src/")
|
|
16
|
+
sys.path.append("tests/")
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from conftest import sample_df_
|
|
19
|
+
|
|
20
|
+
df = sample_df_()
|
|
21
|
+
import showstats
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```{python}
|
|
25
|
+
|
|
26
|
+
from showstats import show_stats
|
|
27
|
+
|
|
28
|
+
show_stats(df)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Primarily built for polars data frames. **showstats** converts other inputs, for compatibility with pandas.DataFrames install as ``pip install showstats[pandas]``
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.0.2] - 2024-08-05
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Exports
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import nox
|
|
2
|
+
|
|
3
|
+
nox.options.default_venv_backend = "uv"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@nox.session
|
|
7
|
+
def lint(session):
|
|
8
|
+
session.install("ruff")
|
|
9
|
+
session.run("ruff", "check")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@nox.session(name="python_versions", python=["3.8", "3.9", "3.10", "3.11", "3.12"])
|
|
13
|
+
def test(session):
|
|
14
|
+
session.install(
|
|
15
|
+
"pytest>=8.3.2", "polars>=0.20.21", "pandas>=1.5.3", "pyarrow>=10.0.0"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
session.run("pytest", "tests/")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@nox.parametrize("polars_version", ["0.20.21", "1.4.1"])
|
|
22
|
+
@nox.parametrize("pandas_version", ["1.5.3"])
|
|
23
|
+
@nox.session(name="polars_pandas", python="3.9")
|
|
24
|
+
def test_polars_versions(session, polars_version, pandas_version):
|
|
25
|
+
session.install(
|
|
26
|
+
"pytest>=8.3.2",
|
|
27
|
+
f"polars=={polars_version}",
|
|
28
|
+
f"pandas>={pandas_version}",
|
|
29
|
+
"pyarrow>=10.0.0",
|
|
30
|
+
)
|
|
31
|
+
session.run("pytest", "tests/")
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
dependencies = ["polars >= 0.20.21"]
|
|
7
|
+
name = "showstats"
|
|
8
|
+
description = "Vertical summary statistics for data frames"
|
|
9
|
+
authors = [{ name = "Matthias Kaeding" }]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
]
|
|
15
|
+
version = "0.0.1"
|
|
16
|
+
readme = "README.md"
|
|
17
|
+
requires-python = ">= 3.8"
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
pandas = ["pandas>=1.5.3", "pyarrow>=10.0.0"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[tool.ruff]
|
|
24
|
+
extend-include = ["*.ipynb"]
|
|
25
|
+
|
|
26
|
+
[tool.pytest.ini_options]
|
|
27
|
+
pythonpath = ["src"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/matthiaskaeding/showstats"
|
|
31
|
+
Issues = "https://github.com/matthiaskaeding/showstats/issues"
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Explanatory materials
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Utility scripts
|
|
@@ -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)
|
|
@@ -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
|
|
File without changes
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import polars as pl
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def sample_series(
|
|
6
|
+
seed: int = 1,
|
|
7
|
+
n: int = 100,
|
|
8
|
+
min: float = None,
|
|
9
|
+
max: float = None,
|
|
10
|
+
std: float = None,
|
|
11
|
+
mean: float = None,
|
|
12
|
+
) -> pl.Series:
|
|
13
|
+
"""
|
|
14
|
+
Samples a pl.Series with known moments.
|
|
15
|
+
seed (int): Random seed
|
|
16
|
+
min (float): Minimum
|
|
17
|
+
max (float): Maximum
|
|
18
|
+
std (float): Standard deviation
|
|
19
|
+
mean (float): Mean
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
import random
|
|
23
|
+
|
|
24
|
+
random.seed(seed)
|
|
25
|
+
|
|
26
|
+
data = [random.gauss(mu=0.0, sigma=1.0) for _ in range(n)]
|
|
27
|
+
sr = pl.Series(data)
|
|
28
|
+
if mean is not None:
|
|
29
|
+
sr_mean = sr.mean()
|
|
30
|
+
sr = (sr - sr_mean) + mean
|
|
31
|
+
if std is not None:
|
|
32
|
+
st_std = sr.std()
|
|
33
|
+
sr = sr * std / st_std
|
|
34
|
+
if min is not None:
|
|
35
|
+
sr_min = sr.min()
|
|
36
|
+
sr = sr - sr_min + min
|
|
37
|
+
if max is not None:
|
|
38
|
+
sr_max = sr.max()
|
|
39
|
+
sr = sr - sr_max + max
|
|
40
|
+
|
|
41
|
+
return sr
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def sample_df_(n: int = 100, seed: int = 1) -> pl.DataFrame:
|
|
45
|
+
"""
|
|
46
|
+
Generate a sample DataFrame with various data types.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
n (int): Number of rows to generate. Default is 100.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
pl.DataFrame: A DataFrame with sample data.
|
|
53
|
+
"""
|
|
54
|
+
import random
|
|
55
|
+
from datetime import date, datetime, timedelta
|
|
56
|
+
|
|
57
|
+
assert n >= 100, "There must be >= 100 rows"
|
|
58
|
+
|
|
59
|
+
random.seed(a=seed, version=2)
|
|
60
|
+
int_data = range(n)
|
|
61
|
+
float_data = [i / 100 for i in range(n)]
|
|
62
|
+
bool_data = [i % 2 == 0 for i in range(n)]
|
|
63
|
+
str_data = random.choices(["foo", "bar", "baz", "ABC"], k=n)
|
|
64
|
+
date_col = pl.date_range(
|
|
65
|
+
start=date(2022, 1, 1),
|
|
66
|
+
end=date(2022, 1, 1) + timedelta(days=n - 1),
|
|
67
|
+
eager=True,
|
|
68
|
+
)
|
|
69
|
+
date_col_2 = pl.date_range(
|
|
70
|
+
start=date(1500, 1, 1),
|
|
71
|
+
end=date(1500, 1, 1) + timedelta(days=n - 1),
|
|
72
|
+
eager=True,
|
|
73
|
+
)
|
|
74
|
+
datetime_col = pl.datetime_range(
|
|
75
|
+
start=datetime(2022, 1, 1),
|
|
76
|
+
end=datetime(2022, 1, 1) + timedelta(seconds=n - 1),
|
|
77
|
+
interval="1s",
|
|
78
|
+
eager=True,
|
|
79
|
+
)
|
|
80
|
+
datetime_col_2 = pl.datetime_range(
|
|
81
|
+
start=datetime(1995, 1, 1),
|
|
82
|
+
end=datetime(1995, 1, 1) + timedelta(seconds=n - 1),
|
|
83
|
+
interval="1s",
|
|
84
|
+
eager=True,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
cats = ["low", "medium", "high"]
|
|
88
|
+
categorical_data = random.choices(cats, k=n)
|
|
89
|
+
null_data = [None] * n
|
|
90
|
+
|
|
91
|
+
int_with_missing_data = list(int_data)
|
|
92
|
+
for i in range(10, 30):
|
|
93
|
+
int_with_missing_data[i] = None
|
|
94
|
+
|
|
95
|
+
return pl.DataFrame(
|
|
96
|
+
{
|
|
97
|
+
"int_col": int_data,
|
|
98
|
+
"int_with_missing": int_with_missing_data,
|
|
99
|
+
"float_col": float_data,
|
|
100
|
+
"float_col_with_mean_2": sample_series(n=n, seed=seed, mean=2),
|
|
101
|
+
"float_col_with_std_2": sample_series(n=n, seed=seed, std=2),
|
|
102
|
+
"float_col_with_min_7": sample_series(n=n, seed=seed, min=7),
|
|
103
|
+
"float_col_with_max_17": sample_series(n=n, seed=seed, max=17),
|
|
104
|
+
"bool_col": bool_data,
|
|
105
|
+
"str_col": str_data,
|
|
106
|
+
"date_col": date_col,
|
|
107
|
+
"date_col_2": date_col_2,
|
|
108
|
+
"datetime_col": datetime_col,
|
|
109
|
+
"datetime_col_2": datetime_col_2,
|
|
110
|
+
"categorical_col": pl.Series(categorical_data, dtype=pl.Categorical),
|
|
111
|
+
"enum_col": pl.Series(categorical_data, dtype=pl.Enum(cats)),
|
|
112
|
+
"null_col": pl.Series(null_data),
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@pytest.fixture(scope="session")
|
|
118
|
+
def sample_df():
|
|
119
|
+
return sample_df_(n=500)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import polars as pl
|
|
2
|
+
from showstats.showstats import make_stats_df
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def testmake_stats_df(sample_df):
|
|
6
|
+
summary_table = make_stats_df(sample_df)
|
|
7
|
+
col_0 = summary_table.columns[0]
|
|
8
|
+
sorted_cols = sorted(sample_df.columns)
|
|
9
|
+
assert sorted(summary_table.get_column(col_0)) == sorted_cols
|
|
10
|
+
|
|
11
|
+
assert (
|
|
12
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_mean_2")).item(0, "Mean")
|
|
13
|
+
== "2.0"
|
|
14
|
+
)
|
|
15
|
+
assert (
|
|
16
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_std_2")).item(0, "Std.")
|
|
17
|
+
== "2.0"
|
|
18
|
+
)
|
|
19
|
+
assert (
|
|
20
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_min_7")).item(0, "Min")
|
|
21
|
+
== "7.0"
|
|
22
|
+
)
|
|
23
|
+
assert (
|
|
24
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_max_17")).item(0, "Max")
|
|
25
|
+
== "17.0"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
summary_table_pandas = make_stats_df(sample_df.to_pandas())
|
|
29
|
+
assert sorted(summary_table_pandas.get_column(col_0)) == sorted_cols
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import polars as pl
|
|
2
|
+
from showstats.showstats import make_stats_df
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def testmake_stats_df(sample_df):
|
|
6
|
+
summary_table = make_stats_df(sample_df)
|
|
7
|
+
col_0 = summary_table.columns[0]
|
|
8
|
+
sorted_cols = sorted(sample_df.columns)
|
|
9
|
+
assert sorted(summary_table.get_column(col_0)) == sorted_cols
|
|
10
|
+
|
|
11
|
+
assert (
|
|
12
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_mean_2")).item(0, "Mean")
|
|
13
|
+
== "2.0"
|
|
14
|
+
)
|
|
15
|
+
assert (
|
|
16
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_std_2")).item(0, "Std.")
|
|
17
|
+
== "2.0"
|
|
18
|
+
)
|
|
19
|
+
assert (
|
|
20
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_min_7")).item(0, "Min")
|
|
21
|
+
== "7.0"
|
|
22
|
+
)
|
|
23
|
+
assert (
|
|
24
|
+
summary_table.filter(pl.col(col_0).eq("float_col_with_max_17")).item(0, "Max")
|
|
25
|
+
== "17.0"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
summary_table_pandas = make_stats_df(sample_df.to_pandas())
|
|
29
|
+
assert sorted(summary_table_pandas.get_column(col_0)) == sorted_cols
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import polars as pl
|
|
2
|
+
from showstats.showstats import _make_tables
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def test_make_tables(sample_df):
|
|
6
|
+
result = _make_tables(sample_df)
|
|
7
|
+
|
|
8
|
+
assert isinstance(result, dict)
|
|
9
|
+
assert set(result.keys()) == {"num", "cat", "datetime", "date", "null"}
|
|
10
|
+
|
|
11
|
+
for key, df in result.items():
|
|
12
|
+
assert isinstance(df, pl.DataFrame)
|
|
13
|
+
assert "Variable" in df.columns
|
|
14
|
+
assert "null_count" in df.columns
|
|
15
|
+
if key != "null":
|
|
16
|
+
assert "min" in df.columns
|
|
17
|
+
assert "max" in df.columns
|
|
18
|
+
|
|
19
|
+
# Check specific data-frames
|
|
20
|
+
num_df = result["num"]
|
|
21
|
+
assert set(num_df["Variable"]) == {
|
|
22
|
+
"int_col",
|
|
23
|
+
"float_col",
|
|
24
|
+
"bool_col",
|
|
25
|
+
"int_with_missing",
|
|
26
|
+
"float_col_with_mean_2",
|
|
27
|
+
"float_col_with_std_2",
|
|
28
|
+
"float_col_with_min_7",
|
|
29
|
+
"float_col_with_max_17",
|
|
30
|
+
}
|
|
31
|
+
assert "mean" in num_df.columns
|
|
32
|
+
assert "median" in num_df.columns
|
|
33
|
+
assert "std" in num_df.columns
|
|
34
|
+
|
|
35
|
+
cat_df = result["cat"]
|
|
36
|
+
assert set(cat_df.get_column("Variable")) == {
|
|
37
|
+
"str_col",
|
|
38
|
+
"categorical_col",
|
|
39
|
+
"enum_col",
|
|
40
|
+
}
|
|
41
|
+
assert "n_unique" in cat_df.columns
|
|
42
|
+
|
|
43
|
+
datetime_df = result["datetime"]
|
|
44
|
+
assert set(datetime_df.get_column("Variable")) == {"datetime_col", "datetime_col_2"}
|
|
45
|
+
assert "mean" in datetime_df.columns
|
|
46
|
+
assert "median" in datetime_df.columns
|
|
47
|
+
|
|
48
|
+
date_df = result["date"]
|
|
49
|
+
assert set(date_df["Variable"]) == {"date_col", "date_col_2"}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_all_null_column():
|
|
53
|
+
null_df = pl.DataFrame({"null_col": [None] * 10})
|
|
54
|
+
result = _make_tables(null_df)
|
|
55
|
+
assert isinstance(result, dict)
|
|
56
|
+
assert "cat" not in result
|
|
57
|
+
assert len(result) == 1
|
|
58
|
+
assert "null" in result
|
|
59
|
+
assert "null_col" in result["null"].get_column("Variable")
|
|
60
|
+
assert (
|
|
61
|
+
result["null"].filter(pl.col("Variable") == "null_col").item(0, "null_count")
|
|
62
|
+
== 10
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_single_column_dataframe():
|
|
67
|
+
single_col_df = pl.DataFrame({"test_col": range(10)})
|
|
68
|
+
result = _make_tables(single_col_df)
|
|
69
|
+
assert isinstance(result, dict)
|
|
70
|
+
assert "num" in result
|
|
71
|
+
assert "test_col" in result["num"]["Variable"]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import polars as pl
|
|
2
|
+
import pytest
|
|
3
|
+
from showstats.showstats import show_stats
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_print_summary(sample_df, capsys):
|
|
7
|
+
pl.Config.set_fmt_str_lengths(n=10000).set_tbl_width_chars(10000)
|
|
8
|
+
show_stats(sample_df)
|
|
9
|
+
captured = capsys.readouterr()
|
|
10
|
+
output = captured.out
|
|
11
|
+
|
|
12
|
+
# Check if the output contains expected column names
|
|
13
|
+
expected_columns = ["Missing", "Mean", "Median", "Std.", "Min", "Max"]
|
|
14
|
+
for col in expected_columns:
|
|
15
|
+
assert col in output, f"{col} not in output"
|
|
16
|
+
assert "Var" in output
|
|
17
|
+
|
|
18
|
+
# Check if all variable names are in the output
|
|
19
|
+
for col in sample_df.columns:
|
|
20
|
+
assert col in output
|
|
21
|
+
|
|
22
|
+
# Check if the output is formatted as an ASCII markdown table
|
|
23
|
+
assert "|" in output
|
|
24
|
+
assert "-" in output
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_empty_dataframe():
|
|
28
|
+
with pytest.raises(ValueError) as err:
|
|
29
|
+
empty_df = pl.DataFrame()
|
|
30
|
+
show_stats(empty_df)
|
|
31
|
+
assert "Input data frame must have rows and columns" in str(err.value)
|