Skip to content

JSON Date Handling 1.0 Regressions #30977

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

Merged
merged 7 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 14 additions & 5 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ static char *PyDateTimeToIso(PyDateTime_Date *obj, NPY_DATETIMEUNIT base,
static char *PyDateTimeToIsoCallback(JSOBJ obj, JSONTypeContext *tc,
size_t *len) {

if (!PyDateTime_Check(obj)) {
if (!PyDate_Check(obj)) {
PyErr_SetString(PyExc_TypeError, "Expected datetime object");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the error message say "date" instead of "datetime"? is the call to PyDateTimeToIso below safe if we have a date and not a datetime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to change; was thinking about that myself just didn't want to go down a rabbit hole of cleaning this all up just yet

w.r.t your second question that error check currently doesn't actually raise an error, so I guess it's a no-op (though it probably should do something)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed commented areas to date for now; note the second would segfault if you passed it something that wasn't a date object but I think out of scope to fix here

return NULL;
}
Expand Down Expand Up @@ -1541,9 +1541,12 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
break;
}

// TODO: vectorized timedelta solution
if (enc->datetimeIso &&
(type_num == NPY_TIMEDELTA || PyDelta_Check(item))) {
if (PyObject_TypeCheck(item, cls_nat)) {
len = 5; // TODO: shouldn't require extra space for terminator
cLabel = PyObject_Malloc(len);
strncpy(cLabel, "null", len);
} else if (enc->datetimeIso &&
(type_num == NPY_TIMEDELTA || PyDelta_Check(item))) {
PyObject *td = PyObject_CallFunction(cls_timedelta, "(O)", item);
if (td == NULL) {
Py_DECREF(item);
Expand Down Expand Up @@ -1575,7 +1578,13 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
enc->npyType);
}
castfunc(dataptr, &longVal, 1, NULL, NULL);
if (enc->datetimeIso) {

if (longVal == get_nat()) {
PRINTMARK();
len = 5; // TODO: shouldn't require extra space for terminator
cLabel = PyObject_Malloc(len);
strncpy(cLabel, "null", len);
} else if (enc->datetimeIso) {
cLabel = int64ToIso(longVal, base, &len);
} else {
if (!scaleNanosecToUnit(&longVal, base)) {
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import OrderedDict
import datetime
from datetime import timedelta
from io import StringIO
import json
Expand Down Expand Up @@ -810,6 +811,31 @@ def test_convert_dates(self):
result = read_json(json, typ="series")
tm.assert_series_equal(result, ts)

@pytest.mark.parametrize("date_format", ["epoch", "iso"])
@pytest.mark.parametrize("as_object", [True, False])
@pytest.mark.parametrize(
"date_typ", [datetime.date, datetime.datetime, pd.Timestamp]
)
def test_date_index_and_values(self, date_format, as_object, date_typ):
data = [date_typ(year=2020, month=1, day=1), pd.NaT]
if as_object:
data.append("a")

ser = pd.Series(data, index=data)
result = ser.to_json(date_format=date_format)

if date_format == "epoch":
expected = '{"1577836800000":1577836800000,"null":null}'
else:
expected = (
'{"2020-01-01T00:00:00.000Z":"2020-01-01T00:00:00.000Z","null":null}'
)

if as_object:
expected = expected.replace("}", ',"a":"a"}')

assert result == expected

@pytest.mark.parametrize(
"infer_word",
[
Expand Down