@@ -743,60 +743,6 @@ def test_uuid_legacy(self):
743743 transformed = bin .as_uuid (UuidRepresentation .PYTHON_LEGACY )
744744 self .assertEqual (id , transformed )
745745
746- @unittest .skipIf (not _NUMPY_AVAILABLE , "numpy optional-dependency not installed." )
747- def test_vector_from_numpy (self ):
748- """Follows test_vector except for input type numpy.ndarray"""
749- # Simple data values could be treated as any of our BinaryVectorDtypes
750- arr = np .array ([2 , 3 ])
751- # INT8
752- binary_vector_int8 = Binary .from_vector (arr , BinaryVectorDtype .INT8 )
753- # as_vector
754- vector = binary_vector_int8 .as_vector ()
755- assert isinstance (vector , BinaryVector )
756- assert vector .data == arr .tolist ()
757- # as_numpy_vector
758- vector_np = binary_vector_int8 .as_numpy_vector ()
759- assert isinstance (vector_np , BinaryVector )
760- assert np .all (vector .data == arr )
761- # PACKED_BIT
762- binary_vector_uint8 = Binary .from_vector (arr , BinaryVectorDtype .PACKED_BIT )
763- # as_vector
764- vector = binary_vector_uint8 .as_vector ()
765- assert isinstance (vector , BinaryVector )
766- assert vector .data == arr .tolist ()
767- # as_numpy_vector
768- vector_np = binary_vector_uint8 .as_numpy_vector ()
769- assert isinstance (vector_np , BinaryVector )
770- assert np .all (vector_np .data == arr )
771- # FLOAT32
772- binary_vector_float32 = Binary .from_vector (arr , BinaryVectorDtype .FLOAT32 )
773- # as_vector
774- vector = binary_vector_float32 .as_vector ()
775- assert isinstance (vector , BinaryVector )
776- assert vector .data == arr .tolist ()
777- # as_numpy_vector
778- vector_np = binary_vector_float32 .as_numpy_vector ()
779- assert isinstance (vector_np , BinaryVector )
780- assert np .all (vector_np .data == arr )
781-
782- # Invalid cases
783- with self .assertRaises (ValueError ):
784- Binary .from_vector (np .array ([- 1 ]), BinaryVectorDtype .PACKED_BIT )
785- with self .assertRaises (ValueError ):
786- Binary .from_vector (np .array ([128 ]), BinaryVectorDtype .PACKED_BIT )
787- with self .assertRaises (ValueError ):
788- Binary .from_vector (np .array ([- 198 ]), BinaryVectorDtype .INT8 )
789-
790- # Unexpected cases
791- # Creating a vector of INT8 from a list of doubles will be caught by struct.pack
792- # Numpy's default behavior is to cast to the type requested.
793- list_floats = [- 1.1 , 1.1 ]
794- cast_bin = Binary .from_vector (np .array (list_floats ), BinaryVectorDtype .INT8 )
795- vector = cast_bin .as_vector ()
796- vector_np = cast_bin .as_numpy_vector ()
797- assert vector .data != list_floats
798- assert vector .data == vector_np .data .tolist () == [- 1 , 1 ]
799-
800746 def test_vector (self ):
801747 """Tests of subtype 9"""
802748 # We start with valid cases, across the 3 dtypes implemented.
@@ -933,6 +879,60 @@ def test_binaryvector_equality(self):
933879 BinaryVector ([1 ], BinaryVectorDtype .INT8 ), BinaryVector ([2 ], BinaryVectorDtype .INT8 )
934880 )
935881
882+ @unittest .skipIf (not _NUMPY_AVAILABLE , "numpy optional-dependency not installed." )
883+ def test_vector_from_numpy (self ):
884+ """Follows test_vector except for input type numpy.ndarray"""
885+ # Simple data values could be treated as any of our BinaryVectorDtypes
886+ arr = np .array ([2 , 3 ])
887+ # INT8
888+ binary_vector_int8 = Binary .from_vector (arr , BinaryVectorDtype .INT8 )
889+ # as_vector
890+ vector = binary_vector_int8 .as_vector ()
891+ assert isinstance (vector , BinaryVector )
892+ assert vector .data == arr .tolist ()
893+ # as_numpy_vector
894+ vector_np = binary_vector_int8 .as_numpy_vector ()
895+ assert isinstance (vector_np , BinaryVector )
896+ assert np .all (vector .data == arr )
897+ # PACKED_BIT
898+ binary_vector_uint8 = Binary .from_vector (arr , BinaryVectorDtype .PACKED_BIT )
899+ # as_vector
900+ vector = binary_vector_uint8 .as_vector ()
901+ assert isinstance (vector , BinaryVector )
902+ assert vector .data == arr .tolist ()
903+ # as_numpy_vector
904+ vector_np = binary_vector_uint8 .as_numpy_vector ()
905+ assert isinstance (vector_np , BinaryVector )
906+ assert np .all (vector_np .data == arr )
907+ # FLOAT32
908+ binary_vector_float32 = Binary .from_vector (arr , BinaryVectorDtype .FLOAT32 )
909+ # as_vector
910+ vector = binary_vector_float32 .as_vector ()
911+ assert isinstance (vector , BinaryVector )
912+ assert vector .data == arr .tolist ()
913+ # as_numpy_vector
914+ vector_np = binary_vector_float32 .as_numpy_vector ()
915+ assert isinstance (vector_np , BinaryVector )
916+ assert np .all (vector_np .data == arr )
917+
918+ # Invalid cases
919+ with self .assertRaises (ValueError ):
920+ Binary .from_vector (np .array ([- 1 ]), BinaryVectorDtype .PACKED_BIT )
921+ with self .assertRaises (ValueError ):
922+ Binary .from_vector (np .array ([128 ]), BinaryVectorDtype .PACKED_BIT )
923+ with self .assertRaises (ValueError ):
924+ Binary .from_vector (np .array ([- 198 ]), BinaryVectorDtype .INT8 )
925+
926+ # Unexpected cases
927+ # Creating a vector of INT8 from a list of doubles will be caught by struct.pack
928+ # Numpy's default behavior is to cast to the type requested.
929+ list_floats = [- 1.1 , 1.1 ]
930+ cast_bin = Binary .from_vector (np .array (list_floats ), BinaryVectorDtype .INT8 )
931+ vector = cast_bin .as_vector ()
932+ vector_np = cast_bin .as_numpy_vector ()
933+ assert vector .data != list_floats
934+ assert vector .data == vector_np .data .tolist () == [- 1 , 1 ]
935+
936936 def test_unicode_regex (self ):
937937 """Tests we do not get a segfault for C extension on unicode RegExs.
938938 This had been happening.
0 commit comments