Skip to content

Commit de16c12

Browse files
committed
improve help mesages
1 parent e0560d9 commit de16c12

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

pandas_pivot_table/pandas_pivot_table.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,21 @@ def getValueType(val):
7676
return val
7777
return None
7878

79-
def getColumn(name, dfcols):
79+
def getColumn(name, dfcols, value_cols=None):
80+
dfname = None
8081
if name in dfcols:
81-
return name
82+
dfname = name
8283
else:
8384
try:
84-
i = int(name)
85-
return dfcols[i]
86-
except Exception:
87-
print('%s not a column in %s' % (name, dfcols),
88-
file=sys.stderr)
89-
exit(1)
85+
i = int(name) - 1
86+
dfname = dfcols[i]
87+
except IndexError:
88+
sys.exit('%s not an index into %s' % (name, dfcols))
89+
except ValueError:
90+
sys.exit('%s not a column in %s' % (name, dfcols))
91+
if value_cols and dfname not in value_cols:
92+
sys.exit('%s not a value column in %s' % (name, value_cols))
93+
return dfname
9094

9195
def getColumns(val, dfcols):
9296
fields = [v.strip() for v in val.split(',')]
@@ -95,16 +99,15 @@ def getColumns(val, dfcols):
9599
cols.append(getColumn(name, dfcols))
96100
return cols
97101

98-
def getAggFunc(funcStr, dfcols):
102+
def getAggFunc(funcStr, dfcols, value_cols):
99103
af = funcStr
100104
try:
101105
af = json.loads(funcStr)
102106
except JSONDecodeError as de:
103-
print('"%s" is not a json string: ' % funcStr, de.msg,
104-
file=sys.stderr)
105-
exit(1)
107+
sys.exit('"%s" is not a json string: %s' % (funcStr, de.msg))
106108
if isinstance(af, dict):
107-
aggfunc = {getColumn(k, dfcols): v for k, v in af.items()}
109+
aggfunc = {getColumn(k, dfcols, value_cols): v
110+
for k, v in af.items()}
108111
elif isinstance(af, list):
109112
aggfunc = af
110113
else:
@@ -127,11 +130,12 @@ def getAggFunc(funcStr, dfcols):
127130
columns = getColumns(args.columns, df_columns)
128131
values = getColumns(args.values, df_columns)
129132
fill_value = getValueType(args.fill_value)
130-
aggfunc = getAggFunc(args.aggfunc.replace('\'', '"'), values)
133+
aggfunc = getAggFunc(args.aggfunc.replace('\'', '"'), df_columns, values)
131134
pdf = df.pivot_table(index=index, columns=columns,
132135
values=values, aggfunc=aggfunc,
133136
fill_value=fill_value)
134-
pdf_cols = ['_'.join(reversed(p)) if isinstance(p, tuple) else p
137+
pdf_cols = ['_'.join([str(x) for x in reversed(p)])
138+
if isinstance(p, tuple) else str(p)
135139
for p in pdf.columns.tolist()]
136140
pdf.to_csv(args.output,
137141
sep='\t',

pandas_pivot_table/pandas_pivot_table.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<token name="@AGGITEM@">'\S+'\s*:\s*@AGGFUNCS@</token>
88
<token name="@AGGDICT@">{@AGGITEM@(,\s*@AGGITEM@)*}</token>
99
<token name="@AGGF@">(@AGGFUNCS@|@AGGDICT@)</token>
10+
<token name="@COL_HELP@">Name of column or 1-based oridinal position of column</token>
1011
</macros>
1112
<requirements>
1213
<requirement type="package" version="@VERSION@">pandas</requirement>
@@ -56,24 +57,28 @@
5657
</conditional>
5758
<param name="skiprows" type="integer" value="0" min="0" label="Skip table rows"/>
5859
<param name="pvt_index" type="text" value="" label="Pivot table index columns">
60+
<help>@COL_HELP@</help>
5961
<validator type="regex" message="Column names separated by commas">^\S+(,\S+)*$</validator>
6062
</param>
6163
<param name="pvt_columns" type="text" value="" label="Pivot table columns to split into output columns">
64+
<help>@COL_HELP@</help>
6265
<validator type="regex" message="Column names separated by commas">^\S+(,\S+)*$</validator>
6366
</param>
6467
<param name="pvt_values" type="text" value="" label="Pivot table value columns">
68+
<help>@COL_HELP@</help>
6569
<validator type="regex" message="Column names separated by commas">^\S+(,\S+)*$</validator>
6670
</param>
6771
<param name="aggfunc" type="text" value="" label="Pivot table aggregate function">
6872
<help><![CDATA[
6973
<ul>
7074
<li>Available Number Functions: @AGGFUNC@</li>
71-
<li>Specify functions as (remember the single quotes):</li>
75+
<li>Specify functions as:</li>
7276
<ul>
7377
<li> - A single function applied to each <i>value</i> column: <b>'min'</b></li>
7478
<li> - An array of functions applied to each <i>value</i> column: <b>['min', 'max', 'mean', 'std']</b></li>
7579
<li> - A dictionary of <i>value column : function(s)</i>: <b>{'A' : 'sum', 'B' : ['min', 'max']}</b></li>
7680
</ul>
81+
<li><i>(remember the single quotes)</i></li>
7782
</ul>
7883
]]></help>
7984
<validator type="regex" message="Do not forget the single quotes">@AGGF@</validator>

0 commit comments

Comments
 (0)