From 9d647fd19388068b493e6ce7367a18b46914da68 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 11 Jan 2023 22:07:20 +0100 Subject: [PATCH] Fix websockets for dataframes with NaT entreis --- freqtrade/misc.py | 2 ++ tests/test_misc.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 93e8da6dd..34df3185b 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -269,6 +269,8 @@ def dataframe_to_json(dataframe: pd.DataFrame) -> str: def default(z): if isinstance(z, pd.Timestamp): return z.timestamp() * 1e3 + if z is pd.NaT: + return 'NaT' raise TypeError return str(orjson.dumps(dataframe.to_dict(orient='split'), default=default), 'utf-8') diff --git a/tests/test_misc.py b/tests/test_misc.py index 2da45bad9..596c7bd51 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -5,6 +5,7 @@ from copy import deepcopy from pathlib import Path from unittest.mock import MagicMock +import pandas as pd import pytest 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_frame_equal(ohlcv_history, dataframe) + ohlcv_history.at[1, 'date'] = pd.NaT + json = dataframe_to_json(ohlcv_history) + + dataframe = json_to_dataframe(json)