Skip to content

Commit

Permalink
Pas datetime fixes for pastastore (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrakenhoff committed Mar 27, 2023
2 parents 45909de + 4ad5053 commit 7a9e44c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pastas/io/pas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
from collections import OrderedDict
from logging import getLogger

from pandas import DataFrame, Series, Timedelta, Timestamp, isna, read_json, to_numeric
from pandas import (
DataFrame,
Series,
Timedelta,
Timestamp,
isna,
read_json,
to_numeric,
to_timedelta,
)

logger = getLogger(__name__)

Expand Down Expand Up @@ -67,15 +76,17 @@ class PastasEncoder(json.JSONEncoder):
"""

def default(self, o):
if isinstance(o, Timestamp) or isinstance(o, datetime.datetime):
if isinstance(o, (Timestamp, datetime.datetime)):
return o.isoformat()
elif isinstance(o, Series):
return o.to_json(date_format="iso", orient="split")
elif isinstance(o, DataFrame):
# Necessary to maintain order when using the JSON format!
# Do not use o.to_json() because of float precision
return json.dumps(o.to_dict(orient="index"), indent=0)
elif isinstance(o, Timedelta):
elif isinstance(o, (Timedelta, datetime.timedelta)):
if isinstance(o, datetime.timedelta):
o = to_timedelta(o)
return o.to_timedelta64().__str__()
elif isna(o):
return None
Expand Down

0 comments on commit 7a9e44c

Please sign in to comment.