コード例 #1
0
 def test_mismatched_dimensions(self):
     x_and_ys=[
         (PintArray.from_1darray_quantity(ureg.Quantity([5], "m")), [1, 1]),
         (PintArray.from_1darray_quantity(ureg.Quantity([5, 5, 5], "m")), [1, 1]),
         (PintArray.from_1darray_quantity(self.Q_([5, 5], "m")), [1]),
     ]
     for x, y in x_and_ys:
         for op in comparative_ops + arithmetic_ops:
             self.assertRaises(ValueError, op, x, y)
コード例 #2
0
 def test_pintarray_creation(self):
     x = ureg.Quantity([1, 2, 3],"m")
     ys = [
         PintArray.from_1darray_quantity(x),
         PintArray._from_sequence([item for item in x])
     ]
     for y in ys:
         self.assertQuantityAlmostEqual(x,y.quantity)
コード例 #3
0
 def test_initialisation(self, data):
     # fails with plain array
     # works with PintArray
     df = pd.DataFrame({
     "length" : pd.Series([2,3], dtype="pint[m]"),
     "width" : PintArray([2,3], dtype="pint[m]"),
     "distance" : PintArray([2,3], dtype="m"),
     "height" : PintArray([2,3], dtype=ureg.m),
     "depth" : PintArray.from_1darray_quantity(ureg.Quantity([2,3],ureg.m)),
 })
     
     for col in df.columns:
         assert all(df[col] == df.length)
コード例 #4
0
    def test_pintarray_operations(self):
        # Perform operations with Quantities and PintArrays
        # The resulting Quantity and PintArray.Data should be the same
        # a op b == c
        # warnings ignored here as it these tests are to ensure
        # pint array behaviour is the same as quantity
        def test_op(a_pint, a_pint_array, b_, coerce=True):
            try:
                result_pint = op(a_pint, b_)
                if coerce:
                    # a PintArray is returned from arithmetics, so need the data
                    c_pint_array = op(a_pint_array, b_).quantity
                else:
                    # a boolean array is returned from comparatives
                    c_pint_array = op(a_pint_array, b_)

                self.assertQuantityAlmostEqual(result_pint, c_pint_array)

            except Exception as caught_exception:
                self.assertRaises(type(caught_exception), op, a_pint_array, b_)


        a_pints = [
            ureg.Quantity([3, 4], "m"),
            ureg.Quantity([3, 4], ""),
        ]

        a_pint_arrays = [PintArray.from_1darray_quantity(q) for q in a_pints]

        bs = [
            2,
            ureg.Quantity(3, "m"),
            [1., 3.],
            [3.3, 4.4],
            ureg.Quantity([6, 6], "m"),
            ureg.Quantity([7., np.nan]),
        ]

        for a_pint, a_pint_array in zip(a_pints, a_pint_arrays):
            for b in bs:
                for op in arithmetic_ops:
                    test_op(a_pint, a_pint_array, b)
                for op in comparative_ops:
                    test_op(a_pint, a_pint_array, b, coerce=False)