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

Function test_datetime_arrays

tests/test_io/test_to_from_plotly_json.py:183–221  ·  view source on GitHub ↗
(datetime_array, engine, pretty)

Source from the content-addressed store, hash-verified

181
182
183def test_datetime_arrays(datetime_array, engine, pretty):
184 value = build_test_dict(datetime_array)
185 result = pio.to_json_plotly(value, engine=engine)
186
187 def to_str(v):
188 try:
189 v = v.isoformat(sep="T")
190 except (TypeError, AttributeError):
191 pass
192
193 return str(v)
194
195 if isinstance(datetime_array, list):
196 dt_values = [to_str(d) for d in datetime_array]
197 elif isinstance(datetime_array, pd.Series):
198 with warnings.catch_warnings():
199 warnings.simplefilter("ignore", FutureWarning)
200 # Series.dt.to_pydatetime will return Index[object]
201 # https://github.com/pandas-dev/pandas/pull/52459
202 dt_values = [
203 to_str(d) for d in np.array(datetime_array.dt.to_pydatetime()).tolist()
204 ]
205 elif isinstance(datetime_array, pd.DatetimeIndex):
206 dt_values = [to_str(d) for d in datetime_array.to_pydatetime().tolist()]
207 else: # numpy datetime64 array
208 dt_values = [to_str(d) for d in datetime_array]
209
210 array_str = to_json_test(dt_values)
211 expected = build_test_dict_string(array_str)
212 if orjson:
213 # orjson always serializes datetime64 to ns, but json will return either
214 # full seconds or microseconds, if the rest is zeros.
215 # we don't care about any trailing zeros
216 trailing_zeros = re.compile(r'[.]?0+"')
217 result = trailing_zeros.sub('"', result)
218 expected = trailing_zeros.sub('"', expected)
219
220 assert result == expected
221 check_roundtrip(result, engine=engine, pretty=pretty)
222
223
224def test_object_array(engine, pretty):

Callers

nothing calls this directly

Calls 7

build_test_dictFunction · 0.85
to_strFunction · 0.85
to_json_testFunction · 0.85
build_test_dict_stringFunction · 0.85
check_roundtripFunction · 0.85
tolistMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected