MCPcopy Index your code
hub / github.com/plotly/plotly.py / _mean

Function _mean

plotly/shapeannotation.py:31–49  ·  view source on GitHub ↗
(x)

Source from the content-addressed store, hash-verified

29
30
31def _mean(x):
32 if len(x) == 0:
33 raise ValueError("x must have positive length")
34 try:
35 return float(sum(x)) / len(x)
36 except TypeError:
37 # Handle non-numeric types like datetime strings or datetime objects
38 if all(_is_date_string(v) for v in x):
39 ms_values = [_datetime_str_to_ms(v) for v in x]
40 mean_ms = sum(ms_values) / len(ms_values)
41 return _ms_to_datetime_str(mean_ms)
42 # Handle datetime.datetime, pd.Timestamp, or similar objects
43 if all(hasattr(v, "timestamp") for v in x):
44 ts_values = [v.timestamp() * 1000 for v in x]
45 mean_ms = sum(ts_values) / len(ts_values)
46 return datetime.datetime.fromtimestamp(
47 mean_ms / 1000, tz=datetime.timezone.utc
48 ).isoformat()
49 raise
50
51
52def _argmin(x):

Callers 2

Calls 3

_is_date_stringFunction · 0.85
_datetime_str_to_msFunction · 0.85
_ms_to_datetime_strFunction · 0.85

Tested by

no test coverage detected