Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
40b9af1
WIP accelerated encoding with orjson
jonmmease Dec 5, 2020
f79e318
support fig to dict in io without cloning
jonmmease Dec 5, 2020
55720de
Merge branch 'master' into orjson_encoding
jonmmease Dec 5, 2020
7b3593a
fix clone default
jonmmease Dec 5, 2020
da915d6
Add pio.json.config object to configure default encoder
jonmmease Dec 5, 2020
7b235ef
default_encoder to default_engine
jonmmease Dec 5, 2020
7895b6a
blacken
jonmmease Dec 5, 2020
ce05a68
Handle Dash objects in to_json
jonmmease Dec 6, 2020
4ef6510
add JSON encoding tests
jonmmease Dec 31, 2020
101ba85
add testing of from_plotly_json
jonmmease Dec 31, 2020
67d3670
Better error message when orjson not installed and orjson engine requ…
jonmmease Dec 31, 2020
02c00da
Add orjson as optional testing dependency
jonmmease Dec 31, 2020
99ea6a1
Replace Python 3.5 CI tests with 3.8
jonmmease Dec 31, 2020
d44ec26
Try only install orjson with Python 3.6+
jonmmease Dec 31, 2020
b7d8422
Don't test orjson engine when orjson not installed
jonmmease Dec 31, 2020
ddcd6f5
Try new 3.8.7 docker image since prior guess doesn't exist
jonmmease Dec 31, 2020
33359f3
greater than!
jonmmease Dec 31, 2020
c7c1819
Bump scikit image version for Python 3.8 compatibility
jonmmease Dec 31, 2020
a8d52ab
Try to help Python 2 from getting confused about which json module to…
jonmmease Dec 31, 2020
619838f
Update pandas for Python 3
jonmmease Dec 31, 2020
7c7a272
Revert 3.8 CI updates. Too much for this PR
jonmmease Dec 31, 2020
1708703
Doh
jonmmease Dec 31, 2020
66cab10
Don't skip copying during serialization
jonmmease Dec 31, 2020
56a8945
Rename new JSON functions:
jonmmease Jan 2, 2021
0a51020
Ensure cleaned numpy arrays are contiguous
jonmmease Jan 2, 2021
4e9d64e
Use to_json_plotly in html and orca logic
jonmmease Jan 8, 2021
d4068de
Add orjson documentation dependency
jonmmease Jan 8, 2021
58b7192
Handle pandas Timestamp scalars in orjson engine
jonmmease Jan 8, 2021
974fcba
Rework date and string encoding, add and fix tests
jonmmease Jan 8, 2021
a651a63
default JSON engine to "auto"
jonmmease Jan 8, 2021
af1d88d
Fix expected JSON in html export (no spaces)
jonmmease Jan 8, 2021
1d6acc3
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Jan 8, 2021
d51fd94
blacken
jonmmease Jan 8, 2021
042c54c
Fix expected JSON in matplotlylib test
jonmmease Jan 8, 2021
ddc1b8f
Fix expected JSON in html repr test
jonmmease Jan 8, 2021
d7928b0
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Jan 13, 2021
76cc625
Don't drop timezones during serialization, just let Plotly.js ignore …
jonmmease Jan 13, 2021
453461d
Merge branch 'numpy_date_serialization' into orjson_encoding
jonmmease Jan 13, 2021
84ba4b5
no need to skip legacy tests now
jonmmease Jan 13, 2021
340aed3
Only try `datetime_as_string` on datetime kinded numpy arrays
jonmmease Jan 13, 2021
6cea61d
Don't store object or unicode numpy arrays in figure. Coerce to lists
jonmmease Jan 21, 2021
93815c1
Try orjson encoding without cleaning first
jonmmease Jan 21, 2021
242d1fa
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Jan 21, 2021
8a3a4b3
blacken
jonmmease Jan 21, 2021
1de750a
remove scratch file
jonmmease Jan 21, 2021
81f73d5
Remove unused clone
jonmmease Jan 21, 2021
80be8bd
Remove the new "json" encoder
jonmmease Jan 22, 2021
cb54f88
Reorder dict cleaning for performance
jonmmease Jan 22, 2021
1fbfa0d
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Apr 29, 2021
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
Prev Previous commit
Next Next commit
Handle pandas Timestamp scalars in orjson engine
Add test for Timestamp and lists of time values
  • Loading branch information
jonmmease committed Jan 8, 2021
commit 58b7192c8f0d01d1e910bec7c0563b2af8b61cc4
13 changes: 9 additions & 4 deletions packages/python/plotly/plotly/io/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,9 @@ def clean_to_json_compatible(obj, **kwargs):
return np.ascontiguousarray(obj.values)
elif obj.dtype.kind == "M":
if isinstance(obj, pd.Series):
dt_values = obj.dt.to_pydatetime().tolist()
dt_values = obj.dt.tz_localize(None).dt.to_pydatetime().tolist()
else: # DatetimeIndex
dt_values = obj.to_pydatetime().tolist()
dt_values = obj.tz_localize(None).to_pydatetime().tolist()

if not datetime_allowed:
# Note: We don't need to handle dropping timezones here because
Expand All @@ -514,13 +514,18 @@ def clean_to_json_compatible(obj, **kwargs):
if np and isinstance(obj, np.datetime64):
return str(obj)
else:
res = None
try:
# Need to drop timezone for scalar datetimes. Don't need to convert
# to string since engine can do that
return obj.replace(tzinfo=None)
except AttributeError:
res = obj.replace(tzinfo=None)
res = res.to_pydatetime()
except(TypeError, AttributeError):
pass

if res is not None:
return res

# Try .tolist() convertible, do not recurse inside
try:
return obj.tolist()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ def object_numpy_array(request):
datetime.datetime(2003, 7, 12, 8, 34, 22),
datetime.datetime.now(),
np.datetime64(datetime.datetime.utcnow()),
pd.Timestamp(datetime.datetime.now()),
eastern.localize(datetime.datetime(2003, 7, 12, 8, 34, 22)),
eastern.localize(datetime.datetime.now()),
pd.Timestamp(datetime.datetime.now(), tzinfo=eastern),
],
)
def datetime_value(request):
Expand All @@ -112,6 +114,7 @@ def datetime_value(request):

@pytest.fixture(
params=[
list, # plain list of datetime values
lambda a: pd.DatetimeIndex(a), # Pandas DatetimeIndex
lambda a: pd.Series(pd.DatetimeIndex(a)), # Pandas Datetime Series
lambda a: pd.DatetimeIndex(a).values, # Numpy datetime64 array
Expand Down Expand Up @@ -162,13 +165,31 @@ def test_datetime(datetime_value, engine, pretty):


def test_datetime_arrays(datetime_array, engine, pretty):
if engine == "legacy":
pytest.skip("legacy encoder doesn't strip timezone from datetimes arrays")

value = build_test_dict(datetime_array)
result = pio.to_json_plotly(value, engine=engine)

if isinstance(datetime_array, pd.Series):
dt_values = [d.isoformat() for d in datetime_array.dt.to_pydatetime().tolist()]
def to_str(v):
try:
v = v.replace(tzinfo=None)
except (TypeError, AttributeError):
pass

try:
v = v.isoformat(sep="T")
except (TypeError, AttributeError):
pass

return str(v)

if isinstance(datetime_array, list):
dt_values = [to_str(d) for d in datetime_array]
elif isinstance(datetime_array, pd.Series):
dt_values = [to_str(d) for d in datetime_array.dt.to_pydatetime().tolist()]
elif isinstance(datetime_array, pd.DatetimeIndex):
dt_values = [d.isoformat() for d in datetime_array.to_pydatetime().tolist()]
dt_values = [to_str(d) for d in datetime_array.to_pydatetime().tolist()]
else: # numpy datetime64 array
dt_values = datetime_array.tolist()

Expand Down