Skip to content

Commit dd710b5

Browse files
committed
Merge pull request #5137 from Komnomnomnom/json-time-suport
ENH: Json support for datetime.time
2 parents e6b99f4 + 6348e6e commit dd710b5

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Improvements to existing features
9797
overlapping color and style arguments (:issue:`4402`)
9898
- Significant table writing performance improvements in ``HDFStore``
9999
- JSON date serialisation now performed in low-level C code.
100+
- JSON support for encoding datetime.time
100101
- Add ``drop_level`` argument to xs (:issue:`4180`)
101102
- Can now resample a DataFrame with ohlc (:issue:`2320`)
102103
- ``Index.copy()`` and ``MultiIndex.copy()`` now accept keyword arguments to

pandas/io/tests/test_json/test_ujson.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from numpy.testing import (assert_array_equal,
2323
assert_array_almost_equal_nulp,
2424
assert_approx_equal)
25+
import pytz
2526
from pandas import DataFrame, Series, Index, NaT, DatetimeIndex
2627
import pandas.util.testing as tm
2728

@@ -356,6 +357,17 @@ def test_encodeDateConversion(self):
356357
self.assertEquals(int(expected), json.loads(output))
357358
self.assertEquals(int(expected), ujson.decode(output))
358359

360+
def test_encodeTimeConversion(self):
361+
tests = [
362+
datetime.time(),
363+
datetime.time(1, 2, 3),
364+
datetime.time(10, 12, 15, 343243),
365+
datetime.time(10, 12, 15, 343243, pytz.utc)]
366+
for test in tests:
367+
output = ujson.encode(test)
368+
expected = '"%s"' % test.isoformat()
369+
self.assertEquals(expected, output)
370+
359371
def test_nat(self):
360372
input = NaT
361373
assert ujson.encode(input) == 'null', "Expected null"

pandas/src/ujson/python/objToJSON.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,30 @@ static void *NpyDatetime64ToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue
309309
return PandasDateTimeStructToJSON(&dts, tc, outValue, _outLen);
310310
}
311311

312+
static void *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *outLen)
313+
{
314+
PyObject *obj = (PyObject *) _obj;
315+
PyObject *str;
316+
PyObject *tmp;
317+
318+
str = PyObject_CallMethod(obj, "isoformat", NULL);
319+
if (str == NULL) {
320+
PRINTMARK();
321+
PyErr_SetString(PyExc_ValueError, "Failed to convert time");
322+
return NULL;
323+
}
324+
if (PyUnicode_Check(str))
325+
{
326+
tmp = str;
327+
str = PyUnicode_AsUTF8String(str);
328+
Py_DECREF(tmp);
329+
}
330+
outValue = (void *) PyString_AS_STRING (str);
331+
*outLen = strlen ((char *) outValue);
332+
Py_DECREF(str);
333+
return outValue;
334+
}
335+
312336
//=============================================================================
313337
// Numpy array iteration functions
314338
//=============================================================================
@@ -1361,6 +1385,13 @@ void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
13611385
return;
13621386
}
13631387
else
1388+
if (PyTime_Check(obj))
1389+
{
1390+
PRINTMARK();
1391+
pc->PyTypeToJSON = PyTimeToJSON; tc->type = JT_UTF8;
1392+
return;
1393+
}
1394+
else
13641395
if (obj == Py_None)
13651396
{
13661397
PRINTMARK();

0 commit comments

Comments
 (0)