4
4
from typing import TYPE_CHECKING
5
5
from warnings import warn
6
6
7
- import numpy # type: ignore
8
- import pandas # type: ignore
9
-
10
7
import redshift_connector
11
8
from redshift_connector .config import table_type_clauses
12
- from redshift_connector .error import InterfaceError , ProgrammingError
9
+ from redshift_connector .error import (
10
+ MISSING_MODULE_ERROR_MSG ,
11
+ InterfaceError ,
12
+ ProgrammingError ,
13
+ )
13
14
14
15
if TYPE_CHECKING :
15
16
from redshift_connector .core import Connection
16
17
18
+ try :
19
+ import numpy
20
+ import pandas
21
+ except :
22
+ pass
23
+
17
24
18
25
class Cursor :
19
26
"""A cursor object is returned by the :meth:`~Connection.cursor` method of
@@ -275,8 +282,13 @@ def __next__(self: "Cursor"):
275
282
else :
276
283
raise StopIteration ()
277
284
278
- def fetch_dataframe (self : "Cursor" , num : typing .Optional [int ] = None ) -> typing .Optional [pandas .DataFrame ]:
285
+ def fetch_dataframe (self : "Cursor" , num : typing .Optional [int ] = None ) -> typing .Optional [" pandas.DataFrame" ]:
279
286
"""Return a dataframe of the last query results."""
287
+ try :
288
+ import pandas
289
+ except ModuleNotFoundError :
290
+ raise ModuleNotFoundError (MISSING_MODULE_ERROR_MSG .format (module = "pandas" ))
291
+
280
292
columns : typing .Optional [typing .List [typing .Union [str , bytes ]]] = None
281
293
try :
282
294
columns = [column [0 ].decode ().lower () for column in self .description ]
@@ -299,18 +311,28 @@ def fetch_dataframe(self: "Cursor", num: typing.Optional[int] = None) -> typing.
299
311
return None
300
312
return pandas .DataFrame (result , columns = columns )
301
313
302
- def write_dataframe (self : "Cursor" , df : pandas .DataFrame , table : str ) -> None :
314
+ def write_dataframe (self : "Cursor" , df : " pandas.DataFrame" , table : str ) -> None :
303
315
"""write same structure dataframe into Redshift database"""
304
- arrays : numpy .ndarray = df .values
316
+ try :
317
+ import pandas
318
+ except ModuleNotFoundError :
319
+ raise ModuleNotFoundError (MISSING_MODULE_ERROR_MSG .format (module = "pandas" ))
320
+
321
+ arrays : "numpy.ndarray" = df .values
305
322
placeholder : str = ", " .join (["%s" ] * len (arrays [0 ]))
306
323
sql : str = "insert into {table} values ({placeholder})" .format (table = table , placeholder = placeholder )
307
324
if len (arrays ) == 1 :
308
325
self .execute (sql , arrays [0 ])
309
326
elif len (arrays ) > 1 :
310
327
self .executemany (sql , arrays )
311
328
312
- def fetch_numpy_array (self : "Cursor" , num : typing .Optional [int ] = None ) -> numpy .ndarray :
329
+ def fetch_numpy_array (self : "Cursor" , num : typing .Optional [int ] = None ) -> " numpy.ndarray" :
313
330
"""Return a numpy array of the last query results."""
331
+ try :
332
+ import numpy
333
+ except ModuleNotFoundError :
334
+ raise ModuleNotFoundError (MISSING_MODULE_ERROR_MSG .format (module = "numpy" ))
335
+
314
336
if num :
315
337
fetched : typing .Tuple = self .fetchmany (num )
316
338
else :
0 commit comments