pandas-plots 0.8.7__py3-none-any.whl → 0.8.8__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.
pandas_plots/tbl.py CHANGED
@@ -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
- # ! check pandas version
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
- show_total=show_total,
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
- show_total: bool = False,
288
- total_mode: Literal[
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
- "sum",
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 _df, add totals
345
- if show_total:
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 "darkgrey"
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 show_total else 1
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 show_total else 1
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pandas-plots
3
- Version: 0.8.7
3
+ Version: 0.8.8
4
4
  Summary: A collection of helper for table handling and vizualization
5
5
  Home-page: https://github.com/smeisegeier/pandas-plots
6
6
  Author: smeisegeier
@@ -1,11 +1,11 @@
1
1
  pandas_plots/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  pandas_plots/pls.py,sha256=CUh2lskJ6eLO_ccAg_UXXpRoWvZ7-Q3xKcUSEnKhK9U,23349
3
3
  pandas_plots/sql.py,sha256=SHrmwhmzq0QYygvaoKwv7neiwf_Rv87VmdUkADYPdR8,2485
4
- pandas_plots/tbl.py,sha256=WQ5SoNazLejLr88d1Sj8Ee4AHKyAwMeNZIBUe56suho,15605
4
+ pandas_plots/tbl.py,sha256=-Clf01gUetNw3KieqjpFRL0-2MJpIB3mfKU36Tzeij0,16027
5
5
  pandas_plots/txt.py,sha256=LnW9OF3mSX2fp9JajefF3Mz3LuCA8MaqlFZYjT_jaQw,1537
6
6
  pandas_plots/ven.py,sha256=nDKS7cTIHOJhIXKnAxAkEoqPgVZCUPJld5CvSiB2JC4,11721
7
- pandas_plots-0.8.7.dist-info/LICENSE,sha256=6KQ5KVAAhRaB-JJKpX4cefKvRZRgI7GUPc92_2d31XY,1051
8
- pandas_plots-0.8.7.dist-info/METADATA,sha256=Tyi_1lPEXYj0tmYs8pO6WRn-DMEXdeR_Zi_8ch7XFTY,5478
9
- pandas_plots-0.8.7.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
10
- pandas_plots-0.8.7.dist-info/top_level.txt,sha256=XnaNuIHBqMmCeh_U7nKOYTwFue_SIA0wxuDgdPmnnSk,13
11
- pandas_plots-0.8.7.dist-info/RECORD,,
7
+ pandas_plots-0.8.8.dist-info/LICENSE,sha256=6KQ5KVAAhRaB-JJKpX4cefKvRZRgI7GUPc92_2d31XY,1051
8
+ pandas_plots-0.8.8.dist-info/METADATA,sha256=1r7P6xc2coVUrwDz8b0e5cEwJVw7T38jFnxEnrmZwJE,5478
9
+ pandas_plots-0.8.8.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
10
+ pandas_plots-0.8.8.dist-info/top_level.txt,sha256=XnaNuIHBqMmCeh_U7nKOYTwFue_SIA0wxuDgdPmnnSk,13
11
+ pandas_plots-0.8.8.dist-info/RECORD,,