Skip to content

Commit 42f55ee

Browse files
smtakedaankit-bhatnagar167
authored andcommitted
SNOW-67593: Added numpy bool_ support
1 parent 2f7841b commit 42f55ee

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
NUM_DATA_TYPES = [numpy.int8, numpy.int16, numpy.int32, numpy.int64,
2323
numpy.float16, numpy.float32, numpy.float64,
24-
numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64]
24+
numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.bool_]
2525
except:
2626
numpy = None
2727

converter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
u'bytes': u'BINARY',
5050
u'bytearray': u'BINARY',
5151
u'bool': u'BOOLEAN',
52+
u'bool_': u'BOOLEAN',
5253
u'nonetype': u'ANY',
5354
u'datetime': u'TIMESTAMP_NTZ',
5455
u'sfdatetime': u'TIMESTAMP_NTZ',
@@ -519,6 +520,9 @@ def _bytes_to_snowflake(self, value):
519520
def _bool_to_snowflake(self, value):
520521
return value
521522

523+
def _bool__to_snowflake(self, value):
524+
return bool(value)
525+
522526
def _nonetype_to_snowflake(self, _):
523527
return None
524528

test/test_numpy_binding.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,31 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
1313
all_data = [{
1414
'tz': 'America/Los_Angeles',
1515
'float': '1.79769313486e+308',
16+
'numpy_bool': np.True_,
1617
'epoch_time': epoch_time,
1718
'current_time': current_datetime64,
1819
'specific_date': np.datetime64('2005-02-25T03:30'),
1920
'expected_specific_date': np.datetime64('2005-02-25T03:30').astype(datetime.datetime)
2021
}, {
2122
'tz': 'Asia/Tokyo',
2223
'float': '-1.79769313486e+308',
24+
'numpy_bool': np.False_,
2325
'epoch_time': epoch_time,
2426
'current_time': current_datetime64,
2527
'specific_date': np.datetime64('1970-12-31T05:00:00'),
2628
'expected_specific_date': np.datetime64('1970-12-31T05:00:00').astype(datetime.datetime)
2729
}, {
2830
'tz': 'America/New_York',
2931
'float': '-1.79769313486e+308',
32+
'numpy_bool': np.True_,
3033
'epoch_time': epoch_time,
3134
'current_time': current_datetime64,
3235
'specific_date': np.datetime64('1969-12-31T05:00:00'),
3336
'expected_specific_date': np.datetime64('1969-12-31T05:00:00').astype(datetime.datetime)
3437
}, {
3538
'tz': 'UTC',
3639
'float': '-1.79769313486e+308',
40+
'numpy_bool': np.False_,
3741
'epoch_time': epoch_time,
3842
'current_time': current_datetime64,
3943
'specific_date': np.datetime64('1968-11-12T07:00:00.123'),
@@ -53,7 +57,8 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
5357
c8 timestamp_ntz, -- datetime64
5458
c9 date, -- datetime64
5559
c10 timestamp_ltz, -- datetime64,
56-
c11 timestamp_tz) -- datetime64
60+
c11 timestamp_tz, -- datetime64
61+
c12 boolean) -- numpy.bool_
5762
""".format(name=db_parameters['name']))
5863
for data in all_data:
5964
cnx.cursor().execute("""
@@ -70,7 +75,8 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
7075
c8,
7176
c9,
7277
c10,
73-
c11
78+
c11,
79+
c12
7480
)
7581
VALUES(
7682
%s,
@@ -83,6 +89,7 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
8389
%s,
8490
%s,
8591
%s,
92+
%s,
8693
%s)""".format(
8794
name=db_parameters['name']), (
8895
np.iinfo(np.int8).max,
@@ -96,6 +103,7 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
96103
data['current_time'],
97104
data['current_time'],
98105
data['specific_date'],
106+
data['numpy_bool']
99107
))
100108
rec = cnx.cursor().execute("""
101109
SELECT
@@ -109,7 +117,8 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
109117
c8,
110118
c9,
111119
c10,
112-
c11
120+
c11,
121+
c12
113122
FROM {name}""".format(
114123
name=db_parameters['name'])).fetchone()
115124
assert np.int8(rec[0]) == np.iinfo(np.int8).max
@@ -123,6 +132,7 @@ def test_numpy_datatype_binding(conn_cnx, db_parameters):
123132
assert str(rec[8]) == str(data['current_time'])[0:10]
124133
assert rec[9] == datetime.datetime.fromtimestamp(epoch_time, rec[9].tzinfo)
125134
assert rec[10] == data['expected_specific_date'].replace(tzinfo=rec[10].tzinfo)
135+
assert isinstance(rec[11], bool) and rec[11] == data['numpy_bool'] and np.bool_(rec[11]) == data['numpy_bool']
126136
cnx.cursor().execute("""
127137
DELETE FROM {name}""".format(name=db_parameters['name']))
128138
finally:

0 commit comments

Comments
 (0)