[7.x] Serialize only 'pd.NA' to 'None'
This commit is contained in:
@@ -67,7 +67,7 @@ class JSONSerializer(object):
|
|||||||
mimetype = "application/json"
|
mimetype = "application/json"
|
||||||
|
|
||||||
def default(self, data):
|
def default(self, data):
|
||||||
if isinstance(data, TIME_TYPES):
|
if isinstance(data, TIME_TYPES) and getattr(pd, "NaT", None) is not data:
|
||||||
return data.isoformat()
|
return data.isoformat()
|
||||||
elif isinstance(data, uuid.UUID):
|
elif isinstance(data, uuid.UUID):
|
||||||
return str(data)
|
return str(data)
|
||||||
@@ -87,7 +87,7 @@ class JSONSerializer(object):
|
|||||||
if pd:
|
if pd:
|
||||||
if isinstance(data, (pd.Series, pd.Categorical)):
|
if isinstance(data, (pd.Series, pd.Categorical)):
|
||||||
return data.tolist()
|
return data.tolist()
|
||||||
elif hasattr(pd, "NA") and pd.isna(data):
|
elif data is getattr(pd, "NA", None):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
|
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
|
||||||
|
|||||||
@@ -93,6 +93,11 @@ class TestJSONSerializer(TestCase):
|
|||||||
JSONSerializer().dumps({"d": np.zeros((2, 2), dtype=np.uint8)}),
|
JSONSerializer().dumps({"d": np.zeros((2, 2), dtype=np.uint8)}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_serializes_numpy_nan_to_nan(self):
|
||||||
|
self.assertEqual(
|
||||||
|
'{"d":NaN}', JSONSerializer().dumps({"d": np.nan}),
|
||||||
|
)
|
||||||
|
|
||||||
def test_serializes_pandas_timestamp(self):
|
def test_serializes_pandas_timestamp(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'{"d":"2010-10-01T02:30:00"}',
|
'{"d":"2010-10-01T02:30:00"}',
|
||||||
@@ -112,6 +117,11 @@ class TestJSONSerializer(TestCase):
|
|||||||
'{"d":null}', JSONSerializer().dumps({"d": pd.NA}),
|
'{"d":null}', JSONSerializer().dumps({"d": pd.NA}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_raises_serialization_error_pandas_nat(self):
|
||||||
|
if not hasattr(pd, "NaT"):
|
||||||
|
raise SkipTest("pandas.NaT required")
|
||||||
|
self.assertRaises(SerializationError, JSONSerializer().dumps, {"d": pd.NaT})
|
||||||
|
|
||||||
def test_serializes_pandas_category(self):
|
def test_serializes_pandas_category(self):
|
||||||
cat = pd.Categorical(["a", "c", "b", "a"], categories=["a", "b", "c"])
|
cat = pd.Categorical(["a", "c", "b", "a"], categories=["a", "b", "c"])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|||||||
Reference in New Issue
Block a user