@@ -244,6 +244,9 @@ def pivot_table( # pylint: disable=too-many-arguments,too-many-locals
244
244
(hierarchical indexes) on the index and columns of the result
245
245
DataFrame.
246
246
247
+ To provide consistent behaviour with different aggregation functions,
248
+ 'empty' rows or columns -i.e. that are all NaN or 0 (count,sum) are removed.
249
+
247
250
Parameters
248
251
----------
249
252
data : DataFrame
@@ -307,6 +310,29 @@ def pivot_table( # pylint: disable=too-many-arguments,too-many-locals
307
310
sort ,
308
311
)
309
312
313
+ # delete empty rows and columns from table
314
+ deleted_rows = []
315
+ deleted_cols = []
316
+ # define empty columns and rows using boolean masks
317
+ empty_cols_mask = table .sum (axis = 0 ) == 0
318
+ empty_rows_mask = table .sum (axis = 1 ) == 0
319
+
320
+ deleted_cols = list (table .columns [empty_cols_mask ])
321
+ table = table .loc [:, ~ empty_cols_mask ]
322
+ deleted_rows = list (table .index [empty_rows_mask ])
323
+ table = table .loc [~ empty_rows_mask , :]
324
+
325
+ # create a message with the deleted column's names
326
+ comments = []
327
+ if deleted_cols :
328
+ msg_cols = ", " .join (str (col ) for col in deleted_cols )
329
+ comments .append (f"Empty columns: { msg_cols } were deleted." )
330
+ if deleted_rows :
331
+ msg_rows = ", " .join (str (row ) for row in deleted_rows )
332
+ comments .append (f"Empty rows: { msg_rows } were deleted." )
333
+ if comments :
334
+ logger .info (" " .join (comments ))
335
+
310
336
# suppression masks to apply based on the following checks
311
337
masks : dict [str , DataFrame ] = {}
312
338
@@ -387,6 +413,7 @@ def pivot_table( # pylint: disable=too-many-arguments,too-many-locals
387
413
summary = summary ,
388
414
outcome = outcome ,
389
415
output = [table ],
416
+ comments = comments ,
390
417
)
391
418
return table
392
419
0 commit comments