Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add datetime support to _mean()#5368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
m-ad wants to merge4 commits intoplotly:main
base:main
Choose a base branch
Loading
fromm-ad:fix_add_vline_with_annotation_to_datetime_axis
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletionplotly/shapeannotation.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
# some functions defined here to avoid numpy import

from datetime import datetime
from numbers import Number


def _mean(x):
if len(x) == 0:
raise ValueError("x must have positive length")
return float(sum(x)) / len(x)

# Numeric sequence: default behaviour
if all(isinstance(v, Number) for v in x):
return float(sum(x)) / len(x)

# Datetime sequence: delegate to _mean_datetime
if all(isinstance(v, datetime) for v in x):
return _mean_datetime(x)

# Fallback for non-numeric and non-datetime types: return first element
return x[0]


def _mean_datetime(x):
"""Return midpoint of a sequence of datetime objects (assumes homogenous)."""

timestamps = [v.timestamp() for v in x] # convert to POSIX timestamps
avg = sum(timestamps) / len(timestamps)
tzinfo = x[0].tzinfo # extract timezone info from first element
return datetime.fromtimestamp(avg, tz=tzinfo)


def _argmin(x):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
"""Tests for annotated axis-spanning shapes with datetime coordinates.

These tests cover the regression described in GitHub issue #3065:
https://github.com/plotly/plotly.py/issues/3065

"""

from datetime import datetime

import plotly.express as px
import plotly.graph_objects as go


def test_add_vline_with_date_annotation_express():
"""MWE from Github issue https://github.com/plotly/plotly.py/issues/3065"""
df = px.data.stocks(indexed=True)
fig = px.line(df)
fig.add_vline(x="2018-09-24", annotation_text="test")


def test_add_vline_with_date_annotation():
fig = go.Figure()
# Provide a couple of traces so axis type is inferred as date from data
dates = [datetime(2025, 9, 23), datetime(2025, 9, 24), datetime(2025, 9, 25)]
fig.add_scatter(x=dates, y=[1, 2, 3])
fig.add_vline(x=dates[1], annotation_text="Test")

# Ensure one annotation was added and x coordinate preserved
annotations = getattr(fig.layout, "annotations", [])
assert len(annotations) == 1
ann = annotations[0]
assert ann.x == dates[1]
assert ann.text == "Test"


def test_add_hline_with_date_annotation():
fig = go.Figure()
# Provide a couple of traces so axis type is inferred as date from data
dates = [datetime(2025, 9, 23), datetime(2025, 9, 24), datetime(2025, 9, 25)]
fig.add_scatter(y=dates, x=[1, 2, 3])
fig.add_hline(y=dates[1], annotation_text="Test")

# Ensure one annotation was added and x coordinate preserved
annotations = getattr(fig.layout, "annotations", [])
assert len(annotations) == 1
ann = annotations[0]
assert ann.y == dates[1]
assert ann.text == "Test"

[8]ページ先頭

©2009-2025 Movatter.jp