Title: control.timeresp.TimeResponseData.to_pandas() failing · Issue #1087 · python-control/python-control · GitHub
Open Graph Title: control.timeresp.TimeResponseData.to_pandas() failing · Issue #1087 · python-control/python-control
X Title: control.timeresp.TimeResponseData.to_pandas() failing · Issue #1087 · python-control/python-control
Description: Hi, today I was using some step responses and noticed that the .to_pandas() is not actually working. I managed to workaround it by creating my own function to translate the response into a dataframe. Example of code not working: import c...
Open Graph Description: Hi, today I was using some step responses and noticed that the .to_pandas() is not actually working. I managed to workaround it by creating my own function to translate the response into a datafram...
X Description: Hi, today I was using some step responses and noticed that the .to_pandas() is not actually working. I managed to workaround it by creating my own function to translate the response into a datafram...
Opengraph URL: https://github.com/python-control/python-control/issues/1087
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"control.timeresp.TimeResponseData.to_pandas() failing","articleBody":"Hi, today I was using some step responses and noticed that the `.to_pandas()` is not actually working.\r\n\r\nI managed to workaround it by creating my own function to translate the response into a dataframe.\r\n\r\nExample of code not working:\r\n\r\n```Python\r\nimport control as ct\r\nimport numpy as np\r\n\r\nmodel = ct.rss(states=['x0', 'x1'], outputs=['y0', 'y1'], inputs=['u0', 'u1'], name='My Model')\r\n\r\nT = np.linspace(0, 10, 100, endpoint=False)\r\nX0 = np.zeros(model.nstates)\r\n\r\nres = ct.step_response(model, T=T, X0=X0, input=0)\r\n\r\ndf = res.to_pandas()\r\n```\r\n\r\nError:\r\n\r\n```python\r\n---------------------------------------------------------------------------\r\nValueError Traceback (most recent call last)\r\nCell In[140], line 6\r\n 3 T = np.linspace(0, 10, 100, endpoint=False)\r\n 4 X0 = np.zeros(model.nstates)\r\n----\u003e 6 res = ct.step_response(model, T=T, X0=X0, input=0).to_pandas()\r\n\r\nFile ~.env/lib/python3.10/site-packages/control/timeresp.py:723, in TimeResponseData.to_pandas(self)\r\n 719 if self.nstates \u003e 0:\r\n 720 data.update(\r\n 721 {name: self.x[i] for i, name in enumerate(self.state_labels)})\r\n--\u003e 723 return pandas.DataFrame(data)\r\n\r\nFile ~.env/lib/python3.10/site-packages/pandas/core/frame.py:778, in DataFrame.__init__(self, data, index, columns, dtype, copy)\r\n 772 mgr = self._init_mgr(\r\n 773 data, axes={\"index\": index, \"columns\": columns}, dtype=dtype, copy=copy\r\n 774 )\r\n 776 elif isinstance(data, dict):\r\n 777 # GH#38939 de facto copy defaults to False only in non-dict cases\r\n--\u003e 778 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)\r\n 779 elif isinstance(data, ma.MaskedArray):\r\n 780 from numpy.ma import mrecords\r\n\r\nFile ~.env/lib/python3.10/site-packages/pandas/core/internals/construction.py:503, in dict_to_mgr(data, index, columns, dtype, typ, copy)\r\n 499 else:\r\n 500 # dtype check to exclude e.g. range objects, scalars\r\n 501 arrays = [x.copy() if hasattr(x, \"dtype\") else x for x in arrays]\r\n--\u003e 503 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)\r\n\r\nFile ~.env/lib/python3.10/site-packages/pandas/core/internals/construction.py:114, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate)\r\n 111 if verify_integrity:\r\n 112 # figure out the index, if necessary\r\n 113 if index is None:\r\n--\u003e 114 index = _extract_index(arrays)\r\n 115 else:\r\n 116 index = ensure_index(index)\r\n\r\nFile ~.env/lib/python3.10/site-packages/pandas/core/internals/construction.py:664, in _extract_index(data)\r\n 662 raw_lengths.append(len(val))\r\n 663 elif isinstance(val, np.ndarray) and val.ndim \u003e 1:\r\n--\u003e 664 raise ValueError(\"Per-column arrays must each be 1-dimensional\")\r\n 666 if not indexes and not raw_lengths:\r\n 667 raise ValueError(\"If using all scalar values, you must pass an index\")\r\n\r\nValueError: Per-column arrays must each be 1-dimensional\r\n```\r\n\r\n---\r\n\r\nThe code I'm using to workaround it is the following:\r\n\r\n```Python\r\n\r\nimport matplotlib.pyplot as plt\r\nimport control as ct\r\nimport numpy as np\r\n\r\ndef step_response_to_pandas(step_response):\r\n return pd.DataFrame(\r\n {'trace_label': np.array([[label] * (len(res.time)) for label in res.trace_labels]).ravel()} |\r\n {'time': res.time.repeat(len(res.trace_labels))} |\r\n {label: res.inputs[i].ravel() for i,label in enumerate(res.input_labels)} |\r\n {label: res.outputs[i].ravel() for i,label in enumerate(res.output_labels)} |\r\n {label: res.states[i].ravel() for i,label in enumerate(res.state_labels)}\r\n )\r\n\r\ndef plot_step_response_dataframe(df):\r\n grouped = df.groupby(level='trace_label')\r\n row_size = 1\r\n\r\n for trace_label, group in grouped:\r\n fig, axes = plt.subplots(len(group.columns), 1, figsize=(6.4, len(group.columns) * row_size), sharex=True)\r\n fig.suptitle(f'Trace: {trace_label}', fontsize=16)\r\n \r\n if len(group.columns) == 1:\r\n axes = [axes]\r\n \r\n for ax, (signal_name, signal_data) in zip(axes, group.items()):\r\n ax.plot(group.index.get_level_values('time'), signal_data, label=signal_name)\r\n ax.grid(True)\r\n ax.set_ylabel(signal_name)\r\n \r\n axes[-1].set_xlabel('Time')\r\n \r\n plt.tight_layout()\r\n plt.show()\r\n\r\n\r\nmodel = ct.rss(states=['x0', 'x1'], outputs=['y0', 'y1'], inputs=['u0', 'u1'], name='My Model')\r\n\r\nT = np.linspace(0, 10, 100, endpoint=False)\r\nX0 = np.zeros(model.nstates)\r\n\r\nres = ct.step_response(model, T=T, X0=X0)\r\n\r\ndf = step_response_to_pandas(res)\r\ndf = df.set_index(['trace_label', 'time'])\r\n\r\nplot_step_response_dataframe(df)\r\n\r\ndisplay(df)\r\n\r\n```\r\n\r\nExample of output:\r\n\r\n\r\n\r\n\r\n\r\n\r\n---\r\n\r\nThanks!\r\n","author":{"url":"https://github.com/joaoantoniocardoso","@type":"Person","name":"joaoantoniocardoso"},"datePublished":"2024-12-30T19:41:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1087/python-control/issues/1087"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:aaecc92e-8586-c8f6-bc2e-777fa16e8bb2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D764:2F02EE:2A12D2C:39209C8:6979B906 |
| html-safe-nonce | 8e40d55a1c024314dcda64aa17360954f97ace36dc61285e37f8fdd5a0f2d2a2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzY0OjJGMDJFRToyQTEyRDJDOjM5MjA5Qzg6Njk3OUI5MDYiLCJ2aXNpdG9yX2lkIjoiMzc2OTc5OTk3MjgyOTA4NDIyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | df0c0a487364635aac24d7d7a44b2bd5e00dba43bc150e177921708dfed5c184 |
| hovercard-subject-tag | issue:2763624510 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-control/python-control/1087/issue_layout |
| twitter:image | https://opengraph.githubassets.com/daf1a22120acc89e873c6bc97888ccfc86be8c832a858db57383e01bcce4c5e7/python-control/python-control/issues/1087 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/daf1a22120acc89e873c6bc97888ccfc86be8c832a858db57383e01bcce4c5e7/python-control/python-control/issues/1087 |
| og:image:alt | Hi, today I was using some step responses and noticed that the .to_pandas() is not actually working. I managed to workaround it by creating my own function to translate the response into a datafram... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | joaoantoniocardoso |
| hostname | github.com |
| expected-hostname | github.com |
| None | c049b65ec7e54cbf2521f5a560b6527714c612b0bd169188e2ea6e16f83bd5f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-control/python-control git https://github.com/python-control/python-control.git |
| octolytics-dimension-user_id | 2285872 |
| octolytics-dimension-user_login | python-control |
| octolytics-dimension-repository_id | 22791752 |
| octolytics-dimension-repository_nwo | python-control/python-control |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 22791752 |
| octolytics-dimension-repository_network_root_nwo | python-control/python-control |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 87b137883e35e2766c3d0f6a257c4044f6390b83 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width