Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: datetime profile JSON encoding bug #1101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion dataprofiler/profilers/json_encoder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Contains ProfilerEncoder class."""

import json
from datetime import datetime

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -52,7 +53,7 @@ def default(self, to_serialize):
return int(to_serialize)
elif isinstance(to_serialize, np.ndarray):
return to_serialize.tolist()
elif isinstance(to_serialize, pd.Timestamp):
elif isinstance(to_serialize, (pd.Timestamp, datetime)):
return to_serialize.isoformat()
elif isinstance(to_serialize, BaseDataLabeler):
# TODO: This does not allow the user to serialize a model if it is loaded
Expand Down
41 changes: 41 additions & 0 deletions dataprofiler/tests/profilers/test_datetime_column_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,47 @@ def test_json_encode_after_update(self):

self.assertEqual(serialized, expected)

def test_json_encode_datetime(self):
data = ["1209214"]
df = pd.Series(data)
profiler = DateTimeColumn("0")

expected_date_formats = [
"%Y-%m-%d %H:%M:%S",
"%b %d, %Y",
"%m/%d/%y %H:%M",
]
with patch.object(
profiler, "_combine_unique_sets", return_value=expected_date_formats
):
with patch("time.time", return_value=0.0):
profiler.update(df)

serialized = json.dumps(profiler, cls=ProfileEncoder)

expected = json.dumps(
{
"class": "DateTimeColumn",
"data": {
"name": "0",
"col_index": np.nan,
"sample_size": 1,
"metadata": {},
"times": defaultdict(float, {"datetime": 0.0}),
"thread_safe": True,
"match_count": 1,
"date_formats": expected_date_formats,
"min": "1209214",
"max": "1209214",
"_dt_obj_min": "9214-01-20T00:00:00",
"_dt_obj_max": "9214-01-20T00:00:00",
"_DateTimeColumn__calculations": dict(),
},
}
)

self.assertEqual(serialized, expected)

def test_json_decode(self):
fake_profile_name = None
expected_profile = DateTimeColumn(fake_profile_name)
Expand Down
Loading