2121from datetime import datetime
2222from decimal import Decimal
2323
24- import numpy as np
25- import pandas as pd
24+ try :
25+ import numpy as np
26+ import pandas as pd
27+ except ImportError :
28+ np = pd = None
2629
2730from elasticsearch .exceptions import ImproperlyConfigured , SerializationError
2831from elasticsearch .serializer import (
3538from .test_cases import SkipTest , TestCase
3639
3740
41+ def requires_numpy_and_pandas ():
42+ if np is None or pd is None :
43+ raise SkipTest ("Test requires numpy or pandas to be available" )
44+
45+
3846class TestJSONSerializer (TestCase ):
3947 def test_datetime_serialization (self ):
4048 self .assertEqual (
@@ -43,6 +51,8 @@ def test_datetime_serialization(self):
4351 )
4452
4553 def test_decimal_serialization (self ):
54+ requires_numpy_and_pandas ()
55+
4656 if sys .version_info [:2 ] == (2 , 6 ):
4757 raise SkipTest ("Float rounding is broken in 2.6." )
4858 self .assertEqual ('{"d":3.8}' , JSONSerializer ().dumps ({"d" : Decimal ("3.8" )}))
@@ -56,9 +66,13 @@ def test_uuid_serialization(self):
5666 )
5767
5868 def test_serializes_numpy_bool (self ):
69+ requires_numpy_and_pandas ()
70+
5971 self .assertEqual ('{"d":true}' , JSONSerializer ().dumps ({"d" : np .bool_ (True )}))
6072
6173 def test_serializes_numpy_integers (self ):
74+ requires_numpy_and_pandas ()
75+
6276 ser = JSONSerializer ()
6377 for np_type in (
6478 np .int_ ,
@@ -78,6 +92,8 @@ def test_serializes_numpy_integers(self):
7892 self .assertEqual (ser .dumps ({"d" : np_type (1 )}), '{"d":1}' )
7993
8094 def test_serializes_numpy_floats (self ):
95+ requires_numpy_and_pandas ()
96+
8197 ser = JSONSerializer ()
8298 for np_type in (
8399 np .float_ ,
@@ -89,12 +105,16 @@ def test_serializes_numpy_floats(self):
89105 )
90106
91107 def test_serializes_numpy_datetime (self ):
108+ requires_numpy_and_pandas ()
109+
92110 self .assertEqual (
93111 '{"d":"2010-10-01T02:30:00"}' ,
94112 JSONSerializer ().dumps ({"d" : np .datetime64 ("2010-10-01T02:30:00" )}),
95113 )
96114
97115 def test_serializes_numpy_ndarray (self ):
116+ requires_numpy_and_pandas ()
117+
98118 self .assertEqual (
99119 '{"d":[0,0,0,0,0]}' ,
100120 JSONSerializer ().dumps ({"d" : np .zeros ((5 ,), dtype = np .uint8 )}),
@@ -106,24 +126,32 @@ def test_serializes_numpy_ndarray(self):
106126 )
107127
108128 def test_serializes_numpy_nan_to_nan (self ):
129+ requires_numpy_and_pandas ()
130+
109131 self .assertEqual (
110132 '{"d":NaN}' ,
111133 JSONSerializer ().dumps ({"d" : np .nan }),
112134 )
113135
114136 def test_serializes_pandas_timestamp (self ):
137+ requires_numpy_and_pandas ()
138+
115139 self .assertEqual (
116140 '{"d":"2010-10-01T02:30:00"}' ,
117141 JSONSerializer ().dumps ({"d" : pd .Timestamp ("2010-10-01T02:30:00" )}),
118142 )
119143
120144 def test_serializes_pandas_series (self ):
145+ requires_numpy_and_pandas ()
146+
121147 self .assertEqual (
122148 '{"d":["a","b","c","d"]}' ,
123149 JSONSerializer ().dumps ({"d" : pd .Series (["a" , "b" , "c" , "d" ])}),
124150 )
125151
126152 def test_serializes_pandas_na (self ):
153+ requires_numpy_and_pandas ()
154+
127155 if not hasattr (pd , "NA" ): # pandas.NA added in v1
128156 raise SkipTest ("pandas.NA required" )
129157 self .assertEqual (
@@ -132,11 +160,15 @@ def test_serializes_pandas_na(self):
132160 )
133161
134162 def test_raises_serialization_error_pandas_nat (self ):
163+ requires_numpy_and_pandas ()
164+
135165 if not hasattr (pd , "NaT" ):
136166 raise SkipTest ("pandas.NaT required" )
137167 self .assertRaises (SerializationError , JSONSerializer ().dumps , {"d" : pd .NaT })
138168
139169 def test_serializes_pandas_category (self ):
170+ requires_numpy_and_pandas ()
171+
140172 cat = pd .Categorical (["a" , "c" , "b" , "a" ], categories = ["a" , "b" , "c" ])
141173 self .assertEqual (
142174 '{"d":["a","c","b","a"]}' ,
0 commit comments