Title: [BUG]: Timezone-aware pandas DatetimeIndex as x makes figure creation and to_json() ~30x slower · Issue #5864 · plotly/plotly.py · GitHub
Open Graph Title: [BUG]: Timezone-aware pandas DatetimeIndex as x makes figure creation and to_json() ~30x slower · Issue #5864 · plotly/plotly.py
X Title: [BUG]: Timezone-aware pandas DatetimeIndex as x makes figure creation and to_json() ~30x slower · Issue #5864 · plotly/plotly.py
Description: Description Plotting a timeseries the usual way, go.Scatter(x=series.index, y=series), is very slow if the index is a timezone-aware pd.DatetimeIndex. For 100,000 points, go.Figure(...) plus fig.to_json() takes about 2.3 s. With the same...
Open Graph Description: Description Plotting a timeseries the usual way, go.Scatter(x=series.index, y=series), is very slow if the index is a timezone-aware pd.DatetimeIndex. For 100,000 points, go.Figure(...) plus fig.to...
X Description: Description Plotting a timeseries the usual way, go.Scatter(x=series.index, y=series), is very slow if the index is a timezone-aware pd.DatetimeIndex. For 100,000 points, go.Figure(...) plus fig.to...
Opengraph URL: https://github.com/plotly/plotly.py/issues/5864
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: Timezone-aware pandas DatetimeIndex as x makes figure creation and to_json() ~30x slower","articleBody":"### Description\n\nPlotting a timeseries the usual way, `go.Scatter(x=series.index, y=series)`, is very slow if the index is a\ntimezone-aware `pd.DatetimeIndex`. For 100,000 points, `go.Figure(...)` plus `fig.to_json()` takes about 2.3 s.\nWith the same timestamps as a timezone-aware `pd.Series`, or as a timezone-naive `DatetimeIndex`, it takes\nabout 0.05 to 0.08 s.\n\nTimezone-aware indexes are common: data from databases, APIs or `pd.to_datetime(..., utc=True)` usually comes with\n`datetime64[ns, UTC]` timestamps, and `series.index` is what most code passes as `x`.\n\n#4250 reported that a `pandas.Index` slows down figure creation. It was closed after the switch to narwhals in\nplotly 6, based on measurements with a timezone-naive index. The reporter had mentioned timezone-aware\nindexes, but that case was not measured again. It is still slow in plotly 6.9.0 and 7.1.0.\n\n### Screenshots/Video\n\nNot applicable, this is about performance.\n\n### Steps to reproduce\n\nRun this script:\n\n```python\nimport time\n\nimport numpy as np\nimport pandas as pd\nimport plotly\nimport plotly.graph_objects as go\nimport plotly.io as pio\n\nn = 100_000\nindex = pd.date_range(\"2024-01-01\", periods=n, freq=\"min\", tz=\"Europe/Berlin\")\ny = np.random.default_rng(0).standard_normal(n)\n\n\ndef bench(label, x):\n t0 = time.perf_counter()\n fig = go.Figure(go.Scatter(x=x, y=y))\n t1 = time.perf_counter()\n fig.to_json()\n t2 = time.perf_counter()\n print(f\"{label:\u003c24} x dtype: {str(fig.data[0].x.dtype):\u003c15} Figure(): {t1 - t0:6.3f} s to_json(): {t2 - t1:6.3f} s\")\n\n\nprint(\"plotly\", plotly.__version__, \"| pandas\", pd.__version__, \"| json engine\", pio.json.config.default_engine)\nbench(\"tz-naive DatetimeIndex\", index.tz_localize(None))\nbench(\"tz-aware Series\", index.to_series())\nbench(\"tz-aware DatetimeIndex\", index)\n```\n\nOutput (Python 3.14, Linux):\n\n```\nplotly 6.9.0 | pandas 3.1.0 | json engine auto\ntz-naive DatetimeIndex x dtype: datetime64[us] Figure(): 0.035 s to_json(): 0.042 s\ntz-aware Series x dtype: datetime64[us] Figure(): 0.008 s to_json(): 0.039 s\ntz-aware DatetimeIndex x dtype: object Figure(): 0.902 s to_json(): 1.363 s\n\nplotly 7.1.0 | pandas 3.1.0 | json engine auto\ntz-naive DatetimeIndex x dtype: datetime64[us] Figure(): 0.033 s to_json(): 0.042 s\ntz-aware Series x dtype: datetime64[us] Figure(): 0.008 s to_json(): 0.038 s\ntz-aware DatetimeIndex x dtype: object Figure(): 0.894 s to_json(): 1.372 s\n```\n\nIt is the same with pandas 2.3.3 (0.42 s + 0.72 s for the timezone-aware index), and with orjson installed\n(0.91 s + 1.01 s).\n\n### Notes\n\n#### Cause\n\n`copy_to_readonly_numpy_array` in `_plotly_utils/basevalidators.py` converts the values with narwhals.\nFor a timezone-aware datetime Series, it calls `replace_time_zone(None)` (\"so that local time is displayed\")\nand gets a `datetime64` array. A `pd.Index` is not a narwhals Series, so `nw.from_native(..., pass_through=True)`\nreturns it unchanged, and `np.array(v)` then gives an object array of `pd.Timestamp`.\n\nThat object array is handled one element at a time:\n\n- `copy.deepcopy` copies every `Timestamp` in `BaseFigure.__init__` and again in `to_dict()`, which `to_json()` calls.\n- `PlotlyJSONEncoder.default` encodes every `Timestamp`, going through `encode_as_pandas`, `encode_as_numpy` and\n `encode_as_datetime` for each one.\n\n#### Expected behavior\n\nA timezone-aware `DatetimeIndex` should be as fast as a timezone-aware Series.\n\nThe plot would not change. Today the index is serialized as strings with UTC offset, like\n`\"2024-01-01T00:00:00+01:00\"`. plotly.js ignores the offset and shows the wall-clock time, which is exactly what the\nSeries path produces with `replace_time_zone(None)`. Only the JSON would change, losing the offset suffix.\n\n#### Possible fix\n\nIn `copy_to_readonly_numpy_array`, handle a timezone-aware pandas `DatetimeIndex` like the Series path does:\n\n```python\npd = get_module(\"pandas\", should_load=False)\nif pd is not None and isinstance(v, pd.DatetimeIndex) and v.tz is not None:\n # Remove time zone so that local time is displayed, like for Series\n v = v.tz_localize(None)\n```\n\nPlaced directly before `nw.from_native(...)`, this brings the timezone-aware index in the example above down to\n0.002 s for `Figure()` and 0.038 s for `to_json()`. Alternatively, convert any pandas `Index` to a Series before\n`nw.from_native`, so that indexes always take the same path as Series.\n\nWorkaround until then: pass `x=series.index.tz_localize(None)` or `x=series.index.to_series()`.\n\n#### Related\n\n- #4250: \"Usage of a pandas.Index slows down figure generation of plotly\". Closed for plotly 6.0.0rc0, measured with a\n timezone-naive index.\n- #4836 / #4790: switched the Series path to narwhals and `datetime64`. This made Series and naive indexes fast, but\n not timezone-aware indexes.\n- #5813 (open): serializes figures without deep copying them. This would reduce the `to_json()` part, but the index\n would still be an object array of Timestamps, deep-copied in `Figure()` and encoded one by one.\n\n#### Disclaimer\nClaude Code helped in finding, analyzing, reproducing and in writting this issue. I personally checked the reproduction code and will answer personally to questions!","author":{"url":"https://github.com/stewit","@type":"Person","name":"stewit"},"datePublished":"2026-11-02T08:08:06Z","url":"https://github.com/plotly/plotly.py/issues/5864"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:23a0efd0-da67-908b-1a98-0ed2b49d1730 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C196:D5AA4:1DC4A30:279D1E9:6B0162B2 |
| html-safe-nonce | fdcf43f149f13e802f80d96337de003870f6dc1a808b418f57d6612bc8d2825b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTk2OkQ1QUE0OjFEQzRBMzA6Mjc5RDFFOTo2QjAxNjJCMiIsInZpc2l0b3JfaWQiOiI1NzYxMTUzOTU3Nzg0MDUwNDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5305f64cb36afeb891ee6d3fc787c576f7176d271c44acaad1d8a98113a79bd9 |
| hovercard-subject-tag | issue:6158676468 |
| 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/plotly/plotly.py/5864/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9cff279d63151c40c5af35788faec6ff1cce90786831aa84805003cb4c74d653/plotly/plotly.py/issues/5864 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9cff279d63151c40c5af35788faec6ff1cce90786831aa84805003cb4c74d653/plotly/plotly.py/issues/5864 |
| og:image:alt | Description Plotting a timeseries the usual way, go.Scatter(x=series.index, y=series), is very slow if the index is a timezone-aware pd.DatetimeIndex. For 100,000 points, go.Figure(...) plus fig.to... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stewit |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/plotly/plotly.py git https://github.com/plotly/plotly.py.git |
| octolytics-dimension-user_id | 5997976 |
| octolytics-dimension-user_login | plotly |
| octolytics-dimension-repository_id | 14579099 |
| octolytics-dimension-repository_nwo | plotly/plotly.py |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14579099 |
| octolytics-dimension-repository_network_root_nwo | plotly/plotly.py |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width