Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def display_dataframe(df):
selected_x = select_x(df)
selected_y = select_y(df, selected_x)
encoding = Encoding(chart_type=Encoding.chart_type_table, x=selected_x, y=selected_y,
y_aggregation=Encoding.y_agg_max)
return AutoVizWidget(df, encoding)
# over empty dataframes. We're just going to explicitly block against that here.
if len(df) == 0:
raise InvalidEncodingError("Cannot display graph for an empty data set.")
try:
df_grouped = df.groupby(x_column)
except TypeError:
raise InvalidEncodingError("Cannot group by X column '{}' because of its type: '{}'."
.format(df[x_column].dtype))
else:
try:
if y_aggregation == Encoding.y_agg_avg:
df_transformed = df_grouped.mean()
elif y_aggregation == Encoding.y_agg_min:
df_transformed = df_grouped.min()
elif y_aggregation == Encoding.y_agg_max:
df_transformed = df_grouped.max()
elif y_aggregation == Encoding.y_agg_sum:
df_transformed = df_grouped.sum()
elif y_aggregation == Encoding.y_agg_count:
df_transformed = df_grouped.count()
else:
raise ValueError("Y aggregation '{}' not supported.".format(y_aggregation))
except (DataError, ValueError) as err:
raise InvalidEncodingError("Cannot aggregate column '{}' with aggregation function '{}' because:\n\t'{}'."
.format(y_column, y_aggregation, err))
except TypeError:
raise InvalidEncodingError("Cannot aggregate column '{}' with aggregation function '{}' because the type\n"
"cannot be aggregated over."
.format(y_column, y_aggregation))
else:
df_transformed = df_transformed.reset_index()
# Y
options_y_view = {text(i): text(i) for i in self.df.columns}
options_y_view["-"] = None
y_column_view = self.ipywidget_factory.get_dropdown(options=options_y_view,
description="Y", value=self.encoding.y)
y_column_view.on_trait_change(self._y_changed_callback, 'value')
y_column_view.layout.width = "200px"
# Y aggregator
value_for_view = self._get_value_for_aggregation(self.encoding.y_aggregation)
self.y_agg_view = self.ipywidget_factory.get_dropdown(
options={"-": Encoding.y_agg_none,
Encoding.y_agg_avg: Encoding.y_agg_avg,
Encoding.y_agg_min: Encoding.y_agg_min,
Encoding.y_agg_max: Encoding.y_agg_max,
Encoding.y_agg_sum: Encoding.y_agg_sum,
Encoding.y_agg_count: Encoding.y_agg_count},
description="Func.",
value=value_for_view)
self.y_agg_view.on_trait_change(self._y_agg_changed_callback, 'value')
self.y_agg_view.layout.width = "200px"
# Y view
self.y_view = self.ipywidget_factory.get_hbox()
self.y_view.children = [y_column_view, self.y_agg_view]
# Logarithmic X axis
self.logarithmic_x_axis = self.ipywidget_factory.get_checkbox(
description="Log scale X", value=encoding.logarithmic_x_axis)
self.logarithmic_x_axis.on_trait_change(self._logarithmic_x_callback, "value")