Пример #1
0
    def test_set_fill_value(self):
        arr = SparseArray([1., np.nan, 2.], fill_value=np.nan)
        arr.fill_value = 2
        self.assertEqual(arr.fill_value, 2)

        arr = SparseArray([1, 0, 2], fill_value=0, dtype=np.int64)
        arr.fill_value = 2
        self.assertEqual(arr.fill_value, 2)

        # coerces to int
        msg = "unable to set fill_value 3\\.1 to int64 dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = 3.1

        msg = "unable to set fill_value nan to int64 dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = np.nan

        arr = SparseArray([True, False, True], fill_value=False, dtype=np.bool)
        arr.fill_value = True
        self.assertTrue(arr.fill_value)

        # coerces to bool
        msg = "unable to set fill_value 0 to bool dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = 0

        msg = "unable to set fill_value nan to bool dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = np.nan

        # invalid
        msg = "fill_value must be a scalar"
        for val in [[1, 2, 3], np.array([1, 2]), (1, 2, 3)]:
            with tm.assertRaisesRegexp(ValueError, msg):
                arr.fill_value = val
Пример #2
0
    def test_set_fill_value(self):
        arr = SparseArray([1., np.nan, 2.], fill_value=np.nan)
        arr.fill_value = 2
        self.assertEqual(arr.fill_value, 2)

        arr = SparseArray([1, 0, 2], fill_value=0, dtype=np.int64)
        arr.fill_value = 2
        self.assertEqual(arr.fill_value, 2)

        # coerces to int
        msg = "unable to set fill_value 3\\.1 to int64 dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = 3.1

        msg = "unable to set fill_value nan to int64 dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = np.nan

        arr = SparseArray([True, False, True], fill_value=False, dtype=np.bool)
        arr.fill_value = True
        self.assertTrue(arr.fill_value)

        # coerces to bool
        msg = "unable to set fill_value 0 to bool dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = 0

        msg = "unable to set fill_value nan to bool dtype"
        with tm.assertRaisesRegexp(ValueError, msg):
            arr.fill_value = np.nan

        # invalid
        msg = "fill_value must be a scalar"
        for val in [[1, 2, 3], np.array([1, 2]), (1, 2, 3)]:
            with tm.assertRaisesRegexp(ValueError, msg):
                arr.fill_value = val