Skip to content

Improve corner case handling in datetime formatter#14473

Merged
mattpap merged 1 commit into
branch-3.8from
mattpap/14413_tz_nan
May 7, 2025
Merged

Improve corner case handling in datetime formatter#14473
mattpap merged 1 commit into
branch-3.8from
mattpap/14413_tz_nan

Conversation

@mattpap

@mattpap mattpap commented Apr 29, 2025

Copy link
Copy Markdown
Contributor

The formatter returns some bogus output when unsupported inputs are provided. This PR fixes that by intercepting arguments that aren't finite numbers and returns "NaN" in such cases.

fixes #14413

@mattpap mattpap added this to the 3.8 milestone Apr 29, 2025
@mattpap mattpap force-pushed the mattpap/14413_tz_nan branch from 47bc0d8 to e34dc10 Compare May 6, 2025 12:48
@mattpap mattpap requested a review from hoxbro May 6, 2025 12:48

@hoxbro hoxbro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Left one comment, but not a blocker for me.

*/
export function datetime(value: unknown, format?: string): string {
if (isNumber(value) && isFinite(value)) {
return tz(value, format)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another edge case can be out-of-range integers. I'm not sure if it is worth adding a check here to ensure that the output does not contain multiple NaNs and, if it does, returns a single NaN.

image

from datetime import datetime

import numpy as np
from bokeh.io import output_notebook, show
from bokeh.models import Select
from bokeh.models.dom import HTML, ValueOf
import pandas as pd

output_notebook()
select_any_value = Select(
    value=datetime(2025, 1, 1),
    options=[        
        (datetime(2025, 1, 1), "datetime"),
        (pd.Timestamp("2100-01-01").value, 'big')
    ],
)
select_any_value.title = HTML(
    html=[
        "Selected value: <b>",
        ValueOf(obj=select_any_value, attr="value", formatter="datetime", format="%Y-%m-%d %H:%M:%S"),
        ", ",
        ValueOf(obj=select_any_value, attr="value", formatter="basic"),
        "</b>",
    ]
)

show(select_any_value)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will resolve this separately.

@hoxbro hoxbro mentioned this pull request May 7, 2025
13 tasks
@mattpap mattpap modified the milestones: 3.8, 3.7.3 May 7, 2025
@mattpap mattpap merged commit 1c7b008 into branch-3.8 May 7, 2025
@mattpap mattpap deleted the mattpap/14413_tz_nan branch May 7, 2025 18:52
mattpap added a commit that referenced this pull request May 8, 2025
* Update switcher.json

* Fix Legend's glyph rendering for `dpr != 1` (#14443)

* Fix Legend's glyph rendering for dpr != 1

* Allow to override screen scaling in testing

* Add regression tests

* Fix Legend's inactive visuals in CSS mode (#14454)

* Fix Legend's inactive visuals in CSS mode

* Add visual regression tests

* Update child views without removing their elements (#14459)

* Implement LayoutDOM.update_children without explicitly removing nodes

* Fix lint

* Fix logic

* More lint

* Simplify for loop

* No tabs

* Also update handling of element views

* Remove empty line

* Fix self_target children lookup

* Fix type errors

* Fix and simplify logic

* Fix element views

* Apply suggestions from code review

* Simplify further

* Fix lint

* Revert to append (instead of appendChild)

Rever

* Compare DOM nodes non-structurally by identity

* Add regression tests

* Unify all rebuilding of child views

---------

Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>

* update docs for DatetimeTickFormatter (#14452)

* Updated formatters.py

---------

Co-authored-by: Chinmay <chinmay.cc.06@gmail.com>

* fix links to code pen (#14471)

* add jquery to fix links to code pen

* remove jquery dependency

* remove patched show function

* use Node.COMMENT_NODE instead of number

* avoid line breaks in templates and code pen

* fix missing title

* Fix positioning of DOM rendered Legend annotations (#14457)

* Fix positioning of DOM rendered Legend annotations

* Treat inner canvas panels equally to outer

* Robustify resize of canvas after layout

* Update visual baselines

* Make sticky toolbar work correctly

* Refactor PlotView._update_layout()

* Invalidate layout if renderers change

* Add more regression tests

* Update visual baselines

* Always repaint Legend's glyphs after rendering

* Update visual baselines

* Implement move semantics in Plot.add_layout()

* Tighten regressions' baseline viewports

* Fix types of splattable figure's attributes (#14401)

* Fix types of splattable figure's attributes

* Add rudimentary typing tests

* Python 3.10 compatibility

* Improve corner case handling in datetime formatter (#14473)

* Add release notes

---------

Co-authored-by: Philipp Rudiger <prudiger@anaconda.com>
Co-authored-by: Moritz Schreiber <68053396+mosc9575@users.noreply.github.com>
Co-authored-by: Chinmay <chinmay.cc.06@gmail.com>
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueRef formatter does not handle datetime nullable well

2 participants