fix: work around to_json pandas deprecation
This commit is contained in:
@@ -214,6 +214,12 @@ def dataframe_to_json(dataframe: pd.DataFrame) -> str:
|
|||||||
:param dataframe: A pandas DataFrame
|
:param dataframe: A pandas DataFrame
|
||||||
:returns: A JSON string of the pandas DataFrame
|
:returns: A JSON string of the pandas DataFrame
|
||||||
"""
|
"""
|
||||||
|
date_columns = dataframe.select_dtypes(include=["datetime", "datetime64", "datetimetz"])
|
||||||
|
# Explicit conversion to ms
|
||||||
|
# This used to be part of to_json, but was deprecated in pandas 3
|
||||||
|
for date_column in date_columns:
|
||||||
|
dataframe[date_column] = date_columns[date_column].dt.as_unit("ms").astype("int64")
|
||||||
|
|
||||||
return dataframe.to_json(orient="split")
|
return dataframe.to_json(orient="split")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -230,7 +230,7 @@ def test_deep_merge_dicts():
|
|||||||
def test_dataframe_json(ohlcv_history):
|
def test_dataframe_json(ohlcv_history):
|
||||||
from pandas.testing import assert_frame_equal
|
from pandas.testing import assert_frame_equal
|
||||||
|
|
||||||
json = dataframe_to_json(ohlcv_history)
|
json = dataframe_to_json(ohlcv_history.copy())
|
||||||
dataframe = json_to_dataframe(json)
|
dataframe = json_to_dataframe(json)
|
||||||
|
|
||||||
assert list(ohlcv_history.columns) == list(dataframe.columns)
|
assert list(ohlcv_history.columns) == list(dataframe.columns)
|
||||||
@@ -238,6 +238,6 @@ def test_dataframe_json(ohlcv_history):
|
|||||||
|
|
||||||
assert_frame_equal(ohlcv_history, dataframe)
|
assert_frame_equal(ohlcv_history, dataframe)
|
||||||
ohlcv_history.at[1, "date"] = pd.NaT
|
ohlcv_history.at[1, "date"] = pd.NaT
|
||||||
json = dataframe_to_json(ohlcv_history)
|
json = dataframe_to_json(ohlcv_history.copy())
|
||||||
|
|
||||||
dataframe = json_to_dataframe(json)
|
dataframe = json_to_dataframe(json)
|
||||||
|
|||||||
Reference in New Issue
Block a user