Beispiel #1
0
    def testMask(self):
        npDataFrame = NumpyDataFrame(dict(a=range(5), b='a b c d e'.split(' ')))
        npDataFrame.mask = [False, False, True, False, True]

        self.assertListsOrDicts(npDataFrame.asArrayDict(),
                                OrderedDict([('a', np.array([0, 1, 3])),
                                             ('b', np.array(['a', 'b', 'd']))]))
        self.assertListsOrDicts(npDataFrame.getArrayNoMask('a'), np.array([0, 1, 2, 3, 4]))
        self.assertListsOrDicts(npDataFrame.getArray('a'), np.array([0, 1, 3]))
        self.assertEquals(len(npDataFrame), 5)

        slicedDataFrame = npDataFrame[1:4]
        self.assertListsOrDicts(slicedDataFrame.mask, np.array([False, True, False]))
        self.assertListsOrDicts(slicedDataFrame.asArrayDict(),
                                OrderedDict([('a', np.array([1, 3])),
                                             ('b', np.array(['b', 'd']))]))
        self.assertListsOrDicts(slicedDataFrame.getArrayNoMask('a'), np.array([1, 2, 3]))
        self.assertListsOrDicts(slicedDataFrame.getArray('a'), np.array([1, 3]))
        self.assertEquals(len(slicedDataFrame), 3)

        npDataFrame.mask[0:2] = True

        self.assertListsOrDicts(npDataFrame.asArrayDict(),
                                OrderedDict([('a', np.array([3])), ('b', np.array(['d']))]))
        self.assertListsOrDicts(npDataFrame.getArrayNoMask('a'), np.array([0, 1, 2, 3, 4]))
        self.assertListsOrDicts(npDataFrame.getArray('a'), np.array([3]))
        self.assertEquals(len(npDataFrame), 5)
Beispiel #2
0
    def testArrayAddRemove(self):
        npDataFrame = NumpyDataFrame(dict(a=range(5)))
        npDataFrame.addArray('b', list('abcde'))
        self.assertTrue(npDataFrame.hasArray('b'))
        self.assertEquals(npDataFrame.arrayKeys(), ['a', 'b'])
        self.assertListsOrDicts(npDataFrame.asArrayDict(),
                                OrderedDict([('a', np.array(range(5))),
                                             ('b', np.array(['a', 'b', 'c', 'd', 'e']))]))
        self.assertEquals(len(npDataFrame), 5)

        self.assertRaises(ValueError, npDataFrame.addArray, 'c', range(4))
        self.assertFalse(npDataFrame.hasArray('c'))

        npDataFrame.addArray('aa', [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)])
        self.assertTrue(npDataFrame.hasArray('aa'))
        self.assertEquals(npDataFrame.arrayKeys(), ['a', 'b', 'aa'])
        self.assertListsOrDicts(npDataFrame.getArray('aa'),
                                np.array([(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]))
        self.assertEquals(len(npDataFrame), 5)

        self.assertRaises(KeyError, npDataFrame.removeArray, 'd')
        npDataFrame.removeArray('a')
        self.assertFalse(npDataFrame.hasArray('a'))
        self.assertEquals(npDataFrame.arrayKeys(), ['b', 'aa'])
        self.assertListsOrDicts(npDataFrame.asArrayDict(),
                                OrderedDict([
                                    ('b', np.array(['a', 'b', 'c', 'd', 'e'])),
                                    ('aa', np.array([(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]))
                                ]))
        self.assertEquals(len(npDataFrame), 5)
Beispiel #3
0
    def testInit(self):
        npDataFrame = NumpyDataFrame()
        self.assertEquals(npDataFrame.asArrayDict(), {})
        self.assertFalse(npDataFrame.hasArray('a'))
        self.assertEquals(npDataFrame.arrayKeys(), [])
        self.assertEquals(len(npDataFrame), 0)

        npDataFrame = NumpyDataFrame(dict(a=range(5)))
        self.assertListsOrDicts(npDataFrame.asArrayDict(),
                                OrderedDict([('a', np.array(range(5)))]))
        self.assertTrue(npDataFrame.hasArray('a'))
        self.assertEquals(npDataFrame.arrayKeys(), ['a'])
        self.assertListsOrDicts(npDataFrame.getArray('a'), np.array(range(5)))
        self.assertEquals(len(npDataFrame), 5)

        self.assertRaises(ValueError, NumpyDataFrame, dict(a=range(5), b=range(4)))
        self.assertRaises(ValueError, NumpyDataFrame, dict(a=1))