Title: [BUG]: NaT inside a datetime64 array serializes as the string "NaT" instead of null · Issue #5829 · plotly/plotly.py · GitHub
Open Graph Title: [BUG]: NaT inside a datetime64 array serializes as the string "NaT" instead of null · Issue #5829 · plotly/plotly.py
X Title: [BUG]: NaT inside a datetime64 array serializes as the string "NaT" instead of null · Issue #5829 · plotly/plotly.py
Description: Description At 38374ac, a NaT inside a datetime64 array serializes as the string "NaT" on both engines. The same missing value as a list element or a scalar becomes null, and from_json reads the string back as data. $ python -I repro.py ...
Open Graph Description: Description At 38374ac, a NaT inside a datetime64 array serializes as the string "NaT" on both engines. The same missing value as a list element or a scalar becomes null, and from_json reads the st...
X Description: Description At 38374ac, a NaT inside a datetime64 array serializes as the string "NaT" on both engines. The same missing value as a list element or a scalar becomes null, and from_json re...
Opengraph URL: https://github.com/plotly/plotly.py/issues/5829
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: NaT inside a datetime64 array serializes as the string \"NaT\" instead of null","articleBody":"### Description\n\nAt `38374ac`, a `NaT` inside a datetime64 array serializes as the string `\"NaT\"` on both engines. The same missing value as a list element or a scalar becomes `null`, and `from_json` reads the string back as data.\n\n```\n$ python -I repro.py # 38374ac, pandas 3.0.6, numpy 2.5.4, orjson 3.13.0\nstored : ndarray datetime64[us]\njson series -> ['2020-01-01T00:00:00.000000', 'NaT']\njson list -> ['2020-01-01T00:00:00', None]\njson scalar -> null\njson round -> ['2020-01-01T00:00:00.000000', 'NaT']\norjson series -> ['2020-01-01T00:00:00.000000', 'NaT']\norjson list -> ['2020-01-01T00:00:00', None]\norjson scalar -> null\norjson round -> ['2020-01-01T00:00:00.000000', 'NaT']\n```\n\nplotly.js happens to draw a gap for the string, so a chart looks right. Anything else that reads the figure JSON, such as Dash figure State, gets a string where `null` belongs.\n\n### Screenshots/Video\n\nNone: the defect is in the serialized JSON, quoted above.\n\n### Steps to reproduce\n\n```python\nimport json\nimport pandas as pd\nimport plotly.graph_objects as go\nimport plotly.io as pio\nfrom plotly.io._json import to_json_plotly\n\nseries = pd.Series(pd.to_datetime([\"2020-01-01\", None]))\nfig = go.Figure(go.Scatter(x=series))\nprint(\"stored :\", type(fig.data[0].x).__name__, fig.data[0].x.dtype)\nfig2 = go.Figure(go.Scatter(x=[pd.Timestamp(\"2020-01-01\"), pd.NaT]))\nfor e in (\"json\", \"orjson\"):\n print(e, \"series ->\", json.loads(pio.to_json(fig, engine=e))[\"data\"][0][\"x\"])\n print(e, \"list ->\", json.loads(pio.to_json(fig2, engine=e))[\"data\"][0][\"x\"])\n print(e, \"scalar ->\", to_json_plotly(pd.NaT, engine=e))\n print(e, \"round ->\", list(pio.from_json(pio.to_json(fig, engine=e), engine=e).data[0].x))\n```\n\n### Notes\n\n`contract: tests/test_optional/test_utils/test_utils.py:276` already asserts `null` for `pd.NaT` inside an object-dtype Series, and `:291` asserts `\"[null]\"` for `[pd.NaT]`. The datetime64 dtype of that same Series breaks the promise. The scalar case is `tests/test_io/test_to_from_plotly_json.py:265`.\n\nThe `json` engine (the `auto` fallback when orjson is absent) goes through `encode_as_numpy` at `_plotly_utils/utils.py:297`, and orjson goes through `clean_to_json_compatible` at `plotly/io/_json.py:517`. Both call `datetime_as_string` unfiltered:\n\n```diff\n- return numpy.datetime_as_string(obj).tolist()\n+ return [\n+ None if s == \"NaT\" else s\n+ for s in numpy.datetime_as_string(obj).tolist()\n+ ]\n```\n\n```\n$ python -I repro.py | grep -E \"series|round\" # filter at utils.py:297 only\njson series -> ['2020-01-01T00:00:00.000000', None]\njson round -> ['2020-01-01T00:00:00.000000', None]\norjson series -> ['2020-01-01T00:00:00.000000', 'NaT']\norjson round -> ['2020-01-01T00:00:00.000000', 'NaT']\n$ python -I repro.py | grep -E \"series|round\" # same filter at _json.py:517 too\njson series -> ['2020-01-01T00:00:00.000000', None]\njson round -> ['2020-01-01T00:00:00.000000', None]\norjson series -> ['2020-01-01T00:00:00.000000', None]\norjson round -> ['2020-01-01T00:00:00.000000', None]\n# reverted: all four lines print 'NaT' again\n```\n\nNo CODEOWNERS entry covers these paths. @camdecoster, would you take a PR with the filter at both sites and a datetime64 case next to `test_pandas_json_encoding`?\n","author":{"url":"https://github.com/chrikrah","@type":"Person","name":"chrikrah"},"datePublished":"2026-10-20T03:46:50Z","url":"https://github.com/plotly/plotly.py/issues/5829"}
| 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:39b3aa79-12c8-ad00-ce6a-768a90c2e4ac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE26:213839:242772E:2E31C45:6B0177E4 |
| html-safe-nonce | f2e884b0c923d5946bca832c5c832c367a5ae643f780958a9a8abd5faf09ddea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTI2OjIxMzgzOToyNDI3NzJFOjJFMzFDNDU6NkIwMTc3RTQiLCJ2aXNpdG9yX2lkIjoiNjMyODgxODYyNTY1NTI1NDc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9991c45e3759cde038acd6e3b355faa5d60aed7babac250212a3e29cd3607e5c |
| hovercard-subject-tag | issue:5935562948 |
| 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/5829/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1f5f5c0bd25bbc117ab5b5da9e99e36ceb1f5435df1d90f6274098b7b5965289/plotly/plotly.py/issues/5829 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1f5f5c0bd25bbc117ab5b5da9e99e36ceb1f5435df1d90f6274098b7b5965289/plotly/plotly.py/issues/5829 |
| og:image:alt | Description At 38374ac, a NaT inside a datetime64 array serializes as the string "NaT" on both engines. The same missing value as a list element or a scalar becomes null, and from_json reads the st... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chrikrah |
| 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