Add plot_waterfall helper for dataframe-based waterfall charts#93
Add plot_waterfall helper for dataframe-based waterfall charts#93
plot_waterfall helper for dataframe-based waterfall charts#93Conversation
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/e6e8e7c7-c0a1-47b3-9763-5ebb04affcb9 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/e6e8e7c7-c0a1-47b3-9763-5ebb04affcb9 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/e6e8e7c7-c0a1-47b3-9763-5ebb04affcb9 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/e6e8e7c7-c0a1-47b3-9763-5ebb04affcb9 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/e6e8e7c7-c0a1-47b3-9763-5ebb04affcb9 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
plot_waterfall helper for dataframe-based waterfall charts
teachpyx/tools/pandas.py
Outdated
| if len(colors) != 3: | ||
| raise ValueError(f"colors must contain 3 values, not {len(colors)}.") | ||
|
|
||
| try: |
There was a problem hiding this comment.
Fait dans le commit 90a5382 : le try/except autour de pandas.to_numeric a été supprimé dans plot_waterfall.
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/51a3850a-5547-4962-a7d6-829066c3c6ff Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
|
@copilot ajoute un vrai exemple dans la documentation |
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/242bb04a-1d52-4f54-a254-e242a90f9ed5 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Agent-Logs-Url: https://github.com/sdpython/teachpyx/sessions/242bb04a-1d52-4f54-a254-e242a90f9ed5 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
C’est fait dans |
There was a problem hiding this comment.
Pull request overview
Adds a new dataframe-based helper for producing waterfall charts in teachpyx.tools.pandas, along with documentation and unit tests to validate both computation and error handling.
Changes:
- Added
plot_waterfallto compute waterfall bar geometry (start/end) and render a Matplotlib bar chart, returning both(ax, plot_df). - Added unit tests covering nominal geometry/labels and all documented validation/error branches.
- Updated Sphinx API docs with a runnable
.. plot::example for the new helper.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
teachpyx/tools/pandas.py |
Introduces plot_waterfall with input validation, geometry computation, and Matplotlib plotting. |
_unittests/ut_tools/test_pandas.py |
Adds tests for waterfall computation and validation branches. |
_doc/api/tools/pandas.rst |
Documents plot_waterfall via a Sphinx plot example and includes it in module API output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -14,6 +16,39 @@ def test_read_csv_cached(self): | |||
| self.assertEqual(df.shape, df2.shape) | |||
| self.assertEqual(list(df.columns), list(df2.columns)) | |||
|
|
|||
| def test_plot_waterfall(self): | |||
| df = pandas.DataFrame( | |||
| { | |||
| "name": ["A", "B", "C"], | |||
| "delta": [10, -3, 5], | |||
| } | |||
| ) | |||
| ax, plot_df = plot_waterfall(df, "delta", "name", total_label="TOTAL") | |||
| self.assertIsInstance(ax, Axes) | |||
| self.assertEqual(list(plot_df["label"]), ["A", "B", "C", "TOTAL"]) | |||
| self.assertEqual(list(plot_df["start"]), [0.0, 10.0, 7.0, 0.0]) | |||
| self.assertEqual(list(plot_df["end"]), [10.0, 7.0, 12.0, 12.0]) | |||
There was a problem hiding this comment.
plot_waterfall creates a new Matplotlib figure when ax is None, and this unit test will exercise that path. In headless CI environments, importing/using matplotlib.pyplot can fail or pick an interactive backend unless a non-interactive backend (e.g., Agg) is forced by the test runner/environment. Consider explicitly forcing a non-interactive backend for this test suite (or in this test) and closing the created figure to avoid accumulating open figures across tests.
| :param colors: positive, negative, total colors | ||
| :return: axis, computed dataframe used to draw the chart | ||
|
|
||
| .. versionadded:: 0.6.1 |
There was a problem hiding this comment.
The docstring marks this helper as .. versionadded:: 0.6.1, but the project version in pyproject.toml is currently 0.6.0. Please align the versionadded tag with the release version that will actually contain this function (either bump the package version as part of this change, or adjust the tag) to avoid misleading API docs.
| .. versionadded:: 0.6.1 | |
| .. versionadded:: 0.6.0 |
This issue asked for a function to generate a waterfall graph.
This PR adds a reusable plotting helper in
teachpyx.toolswith focused input validation and test coverage.New plotting API (
teachpyx.tools.pandas)plot_waterfall(data, value_column, label_column=None, total_label="total", ax=None, colors=(...))axis not provided(ax, plot_df)so computed geometry can be inspected/reusedInput validation
ValueErrorwhen:value_columnis missinglabel_columnis provided but missingvalue_columncannot be converted to numericcolorsdoes not contain exactly 3 entries (positive / negative / total)Unit tests (
_unittests/ut_tools/test_pandas.py)Example usage: