pandas-plots 0.8.7__tar.gz → 0.8.8__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.
- {pandas-plots-0.8.7/src/pandas_plots.egg-info → pandas-plots-0.8.8}/PKG-INFO +1 -1
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/setup.cfg +1 -1
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots/tbl.py +25 -30
- {pandas-plots-0.8.7 → pandas-plots-0.8.8/src/pandas_plots.egg-info}/PKG-INFO +1 -1
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/LICENSE +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/README.md +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/pyproject.toml +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots/__init__.py +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots/pls.py +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots/sql.py +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots/txt.py +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots/ven.py +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots.egg-info/SOURCES.txt +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots.egg-info/dependency_links.txt +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots.egg-info/requires.txt +0 -0
- {pandas-plots-0.8.7 → pandas-plots-0.8.8}/src/pandas_plots.egg-info/top_level.txt +0 -0
@@ -4,20 +4,18 @@ warnings.filterwarnings("ignore")
|
|
4
4
|
|
5
5
|
import math
|
6
6
|
import os
|
7
|
-
from typing import Literal
|
7
|
+
from typing import Literal, get_args
|
8
8
|
|
9
9
|
import numpy as np
|
10
10
|
import pandas as pd
|
11
11
|
import plotly.express as px
|
12
12
|
from plotly.subplots import make_subplots
|
13
13
|
from scipy import stats
|
14
|
-
|
15
14
|
# pd.options.mode.chained_assignment = None
|
16
|
-
from . import txt
|
17
15
|
|
18
|
-
|
19
|
-
assert pd.__version__ > "2.0.0", "pandas version must be >= 2.0.0"
|
16
|
+
from . import txt
|
20
17
|
|
18
|
+
AGG_FUNC=Literal["sum", "mean", "median", "min", "max", "std", "var", "skew", "kurt"]
|
21
19
|
|
22
20
|
def describe_df(
|
23
21
|
df: pd.DataFrame,
|
@@ -189,8 +187,9 @@ def pivot_df(
|
|
189
187
|
data_bar_axis: Literal["x", "y", "xy", None] = "xy",
|
190
188
|
pct_axis: Literal["x", "xy", None] = "xy",
|
191
189
|
precision: int = 0,
|
192
|
-
show_total: bool = True,
|
193
190
|
heatmap_axis: Literal["x", "y", "xy", None] = None,
|
191
|
+
total_mode: AGG_FUNC = "sum",
|
192
|
+
total_axis: Literal["x", "y", "xy", None] = "xy",
|
194
193
|
) -> pd.DataFrame:
|
195
194
|
"""
|
196
195
|
A function to pivot a DataFrame based on specified parameters and return the result as a new DataFrame.
|
@@ -204,8 +203,9 @@ def pivot_df(
|
|
204
203
|
data_bar_axis (Literal["x", "y", "xy", None], optional): The axis for displaying data bars. Defaults to "xy".
|
205
204
|
pct_axis (Literal["x", "xy", None], optional): The axis for displaying percentages. Defaults to None.
|
206
205
|
precision (int, optional): The precision for displaying values. Defaults to 0.
|
207
|
-
show_total (bool, optional): Whether to show totals in the result. Defaults to False.
|
208
206
|
heatmap_axis (Literal["x","y","xy", None], optional): The axis for displaying heatmaps. Defaults to None.
|
207
|
+
total_mode (Literal["sum", "mean", "median", "min", "max", "std", "var", "skew", "kurt"], optional): The aggregation mode for displaying totals. Defaults to "sum".
|
208
|
+
total_axis (Literal["x", "y", "xy", None], optional): The axis for displaying totals. Defaults to "xy".
|
209
209
|
|
210
210
|
Returns:
|
211
211
|
pd.DataFrame: The pivoted DataFrame.
|
@@ -224,6 +224,10 @@ def pivot_df(
|
|
224
224
|
if not pd.api.types.is_numeric_dtype(df.iloc[:, 2]):
|
225
225
|
print("❌ 3rd column must be numeric")
|
226
226
|
return
|
227
|
+
|
228
|
+
if total_mode and total_mode not in get_args(AGG_FUNC):
|
229
|
+
print(f"❌ total_mode '{total_mode}' not supported")
|
230
|
+
return
|
227
231
|
|
228
232
|
df = df.copy()
|
229
233
|
|
@@ -273,7 +277,8 @@ def pivot_df(
|
|
273
277
|
|
274
278
|
return show_num_df(
|
275
279
|
df,
|
276
|
-
|
280
|
+
total_mode=total_mode,
|
281
|
+
total_axis=total_axis,
|
277
282
|
data_bar_axis=data_bar_axis,
|
278
283
|
pct_axis=pct_axis,
|
279
284
|
swap=swap,
|
@@ -284,10 +289,8 @@ def pivot_df(
|
|
284
289
|
|
285
290
|
def show_num_df(
|
286
291
|
df,
|
287
|
-
|
288
|
-
|
289
|
-
"sum", "mean", "median", "min", "max", "std", "var", "skew", "kurt"
|
290
|
-
] = "sum",
|
292
|
+
total_mode: AGG_FUNC = "sum",
|
293
|
+
total_axis: Literal["x", "y", "xy", None] = "xy",
|
291
294
|
heatmap_axis: Literal["x", "y", "xy", None] = None,
|
292
295
|
data_bar_axis: Literal["x", "y", "xy", None] = None,
|
293
296
|
pct_axis: Literal["x", "xy", None] = None,
|
@@ -299,8 +302,9 @@ def show_num_df(
|
|
299
302
|
|
300
303
|
Parameters:
|
301
304
|
- df: the DataFrame to display
|
302
|
-
- show_total: a boolean indicating whether to show totals
|
303
305
|
- total_mode: a Literal indicating the mode for aggregating totals ["sum", "mean", "median", "min", "max", "std", "var", "skew", "kurt"]
|
306
|
+
- total_axis (Literal["x", "y", "xy", None], optional): The axis for displaying totals. Defaults to "xy".
|
307
|
+
|
304
308
|
- heatmap_axis (Literal["x","y","xy", None], optional): The axis for displaying heatmaps. Defaults to None.
|
305
309
|
- data_bar_axis: a Literal indicating the axis for applying data bar coloring ["x","y","xy", None]
|
306
310
|
- pct_axis: a Literal indicating the directions for displaying percentages ["x","xy", None]. "x" means sum up pct per column
|
@@ -322,18 +326,8 @@ def show_num_df(
|
|
322
326
|
print(f"❌ axis not supported")
|
323
327
|
return
|
324
328
|
|
325
|
-
if total_mode and total_mode not in
|
326
|
-
"
|
327
|
-
"mean",
|
328
|
-
"median",
|
329
|
-
"min",
|
330
|
-
"max",
|
331
|
-
"std",
|
332
|
-
"var",
|
333
|
-
"skew",
|
334
|
-
"kurt",
|
335
|
-
]:
|
336
|
-
print(f"❌ total mode '{total_mode}' not supported")
|
329
|
+
if total_mode and total_mode not in get_args(AGG_FUNC):
|
330
|
+
print(f"❌ total_mode '{total_mode}' not supported")
|
337
331
|
return
|
338
332
|
|
339
333
|
theme = os.getenv("THEME") or "light"
|
@@ -341,15 +335,16 @@ def show_num_df(
|
|
341
335
|
# * copy df, do not reference original
|
342
336
|
df_ = df.copy() if not swap else df.T.copy()
|
343
337
|
|
344
|
-
# * alter
|
345
|
-
if
|
338
|
+
# * alter df_, add totals
|
339
|
+
if total_mode and total_axis in ['x','xy']:
|
346
340
|
df_.loc["Total"] = df_.agg(total_mode, axis=0)
|
341
|
+
if total_mode and total_axis in ['y','xy']:
|
347
342
|
df_.loc[:, "Total"] = df_.agg(total_mode, axis=1)
|
348
343
|
|
349
344
|
# * derive style
|
350
345
|
out = df_.style
|
351
346
|
|
352
|
-
color_highlight = "lightblue" if theme == "light" else "
|
347
|
+
color_highlight = "lightblue" if theme == "light" else "#666666"
|
353
348
|
color_zeros = "grey" if theme == "light" else "grey"
|
354
349
|
color_pct = "grey" if theme == "light" else "yellow"
|
355
350
|
color_values = "black" if theme == "light" else "white"
|
@@ -378,7 +373,7 @@ def show_num_df(
|
|
378
373
|
# * build pct formatting
|
379
374
|
if pct_axis == "x":
|
380
375
|
# * totals on either axis influence the sum
|
381
|
-
divider = 2 if
|
376
|
+
divider = 2 if total_axis in ['x','xy'] else 1
|
382
377
|
# * cell formatting to each column instead of altering values w/ df.apply
|
383
378
|
# * uses dictionary comprehension, and a lambda function with two input variables
|
384
379
|
col_sums = df_.sum() / divider
|
@@ -395,7 +390,7 @@ def show_num_df(
|
|
395
390
|
# }
|
396
391
|
|
397
392
|
elif pct_axis == "xy":
|
398
|
-
divider = 4 if
|
393
|
+
divider = 4 if total_axis == 'xy' else 2 if total_axis in ['x','y'] else 1
|
399
394
|
n = df_.sum().sum() / divider
|
400
395
|
formatter = {
|
401
396
|
col: lambda x, col=col: format_cell(x, n, pct_axis) for col in df_.columns
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|