Merge pull request #8008 from freqtrade/fix/NaT_ser_deser

Fix websockets for dataframes with NaT entries
This commit is contained in:
Matthias 2023-01-12 08:10:14 +01:00 committed by GitHub
commit 1a533668b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -269,6 +269,8 @@ def dataframe_to_json(dataframe: pd.DataFrame) -> str:
def default(z): def default(z):
if isinstance(z, pd.Timestamp): if isinstance(z, pd.Timestamp):
return z.timestamp() * 1e3 return z.timestamp() * 1e3
if z is pd.NaT:
return 'NaT'
raise TypeError raise TypeError
return str(orjson.dumps(dataframe.to_dict(orient='split'), default=default), 'utf-8') return str(orjson.dumps(dataframe.to_dict(orient='split'), default=default), 'utf-8')

View File

@ -5,6 +5,7 @@ from copy import deepcopy
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pandas as pd
import pytest import pytest
from freqtrade.misc import (dataframe_to_json, decimals_per_coin, deep_merge_dicts, file_dump_json, from freqtrade.misc import (dataframe_to_json, decimals_per_coin, deep_merge_dicts, file_dump_json,
@ -231,3 +232,7 @@ def test_dataframe_json(ohlcv_history):
assert len(ohlcv_history) == len(dataframe) assert len(ohlcv_history) == len(dataframe)
assert_frame_equal(ohlcv_history, dataframe) assert_frame_equal(ohlcv_history, dataframe)
ohlcv_history.at[1, 'date'] = pd.NaT
json = dataframe_to_json(ohlcv_history)
dataframe = json_to_dataframe(json)