示例#1
0
    def test_neg(self):
        #                        - This column
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  1.88   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  1.83   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  1.83   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  1.70   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   |  1.905  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  1.83   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        dcf = BatFrame(DatasetTest.dataset_url)
        s = (-dcf["Height"]).toPandas()
        self.assertAlmostEqual(s["Batman"], -1.88)
        self.assertAlmostEqual(s["Wonder Woman"], -1.83)
        self.assertAlmostEqual(s["Flash"], -1.83)
        self.assertAlmostEqual(s["Catwoman"], -1.70)
        self.assertAlmostEqual(s["Superman"], -1.905)
        self.assertAlmostEqual(s["Joker"], -1.83)
示例#2
0
 def test_rows(self):
     bf = BatFrame(DatasetTest.dataset_url)
     rows = bf.rows
     self.assertEqual(len(rows), 6, rows)
     self.assertTrue("Batman" in rows)
     self.assertTrue("Wonder Woman" in rows)
     self.assertTrue("Flash" in rows)
     self.assertTrue("Catwoman" in rows)
     self.assertTrue("Superman" in rows)
     self.assertTrue("Joker" in rows)
示例#3
0
 def test_columns(self):
     bf = BatFrame(DatasetTest.dataset_url)
     columns = bf.columns
     self.assertEqual(len(columns), 8, columns)
     self.assertTrue("Gender" in columns)
     self.assertTrue("Height" in columns)
     self.assertTrue("Weight" in columns)
     self.assertTrue("Eyes" in columns)
     self.assertTrue("Hair" in columns)
     self.assertTrue("Skin" in columns)
     self.assertTrue("Good" in columns)
     self.assertTrue("Bad" in columns)
示例#4
0
    def test_alignment(self):
        bf = BatFrame(DatasetTest.dataset_url)
        height = bf['Height'].values
        weight = bf['Weight'].values
        msg = "Got heights: {} \nand weights: {}\n Length of {} and {}"
        msg = msg.format(height, weight, len(height), len(weight))
        self.assertTrue(len(height) == len(weight), msg=msg)

        expected = [("Batman", 188, 210), ("Wonder Woman", 183, 130),
                    ("Flash", 183, 190), ("Catwoman", 170, 133),
                    ("Superman", 190.5, 235), ("Joker", 183, 183)]

        for t in zip(bf.rows, height, weight):
            msg = "{} is not in the expected values {}".format(t, expected)
            self.assertTrue(t in expected, msg=msg)
    def test_unique(self):
        bf = BatFrame(DatasetTest.dataset_url)

        height = bf['Height'].unique()
        e_height = [1.88, 1.83, 1.70, 1.905]
        msg = "Got {} different values ({}) but expected 4".format(
            len(height), height)
        self.assertEqual(len(height), 4, msg=msg)
        for h in height:
            msg = "{} not in {}".format(h, e_height)
            self.assertTrue(h in e_height, msg=msg)

        weight = bf['Weight'].unique()
        e_weight = [210, 130, 190, 133, 235, 183]
        msg = "Got {} different values ({}) but expected 6".format(
            len(weight), weight)
        self.assertEqual(len(weight), 6, msg=msg)
        for w in weight:
            msg = "{} not in {}".format(w, e_weight)
            self.assertTrue(w in e_weight, msg=msg)
    def test_min(self):
        #                                min(This column)
        #                                       ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  1.88   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  1.83   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  1.83   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  1.70   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   |  1.905  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  1.83   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        bf = BatFrame(DatasetTest.dataset_url)
        self.assertEqual(bf["Weight"].min(), 130)
        self.assertEqual(min(bf["Weight"]), 130)
 def test_head(self):
     bf = BatFrame(DatasetTest.dataset_url)
     head = bf['Gender'].head()
     self.assertEqual(len(head), 5)
示例#8
0
    def test_ix(self):

        #
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------


        bf = BatFrame(DatasetTest.dataset_url)
        Wonder_Woman = bf.ix["Wonder Woman"]
        self.assertEqual(Wonder_Woman['Gender'].values[0], "Female")
        self.assertEqual(Wonder_Woman['Height'].values[0], 183)
        self.assertEqual(Wonder_Woman['Weight'].values[0], 130)
        self.assertEqual(Wonder_Woman['Eyes'].values[0], "Blue")
        self.assertEqual(Wonder_Woman['Hair'].values[0], "Black")
        self.assertTrue(Wonder_Woman['Good'].values[0])
        self.assertFalse(Wonder_Woman['Bad'].values[0])

        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        trinity = bf.ix[["Wonder Woman", "Batman", "Superman"]]
        self.assertTupleEqual(trinity.toPandas().shape, (3, 7))

        self.assertItemsEqual(trinity['Gender'].values, ["Female", "Male", "Male"])
        self.assertItemsEqual(trinity['Height'].values, [183, 188, 190.5])
        self.assertItemsEqual(trinity['Weight'].values, [130, 210, 235])
        self.assertItemsEqual(trinity['Eyes'].values, ["Blue"]*3)
        self.assertItemsEqual(trinity['Hair'].values, ["Black"]*3)
        self.assertItemsEqual(trinity['Good'].values, [True]*3)
        self.assertItemsEqual(trinity['Bad'].values, [False]*3)


        #                   These columns
        #                    ⇓        ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        trinity = bf.ix[["Batman", "Superman"], ["Gender", "Height"]].toPandas()
        self.assertTupleEqual(trinity.shape, (2, 2))

        self.assertItemsEqual(trinity['Gender'].values, ["Male", "Male"])
        self.assertItemsEqual(trinity['Height'].values, [188, 190.5])
    def test_le(self):
        bf = BatFrame(DatasetTest.dataset_url)

        # Testing INTEGER lesser than or equal

        #                       This column <= 183
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------

        def helper(rowName):
            df.ix[rowName]

        df = bf[bf["Height"] <= 183].toPandas()
        self.assertEqual(len(df), 4)
        # Only Catwoman is smaller than 183 cm
        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Flash"].shape, (8, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Joker"].shape, (8, ))

        # Batman and Superman are the biggest
        self.assertRaises(KeyError, helper, "Batman")
        self.assertRaises(KeyError, helper, "Superman")

        # Testing FLOAT lesser than or equal

        #                        This column <= 190.5
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Height"] <= 190.5].toPandas()
        self.assertEqual(len(df), 6)
        # All but Superman are smaller than 190.5 cm
        self.assertTupleEqual(df.ix["Batman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Flash"].shape, (8, ))
        self.assertTupleEqual(df.ix["Joker"].shape, (8, ))
        self.assertTupleEqual(df.ix["Superman"].shape, (8, ))

        # No one is taller than 190.5

        # Testing Column le

        #                            This le This
        #                             ⇓   <=   ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Height"] <= bf["Weight"]].toPandas()
        self.assertEqual(len(df), 4)

        # The bulky ones (Height > Weight)
        self.assertTupleEqual(df.ix["Batman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Flash"].shape, (8, ))
        self.assertTupleEqual(df.ix["Superman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Joker"].shape, (8, ))

        # The thin and tall
        self.assertRaises(KeyError, helper, "Wonder Woman")
        self.assertRaises(KeyError, helper, "Catwoman")
    def test_ge(self):
        bf = BatFrame(DatasetTest.dataset_url)

        # Testing INTEGER greater than or equal

        #                       This column >= 183
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------

        def helper(rowName):
            df.ix[rowName]

        df = bf[bf["Height"] >= 183].toPandas()
        self.assertEqual(len(df), 5)
        # Batman and Superman are taller or equal to 183 cm
        self.assertTupleEqual(df.ix["Batman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Flash"].shape, (8, ))
        self.assertTupleEqual(df.ix["Joker"].shape, (8, ))
        self.assertTupleEqual(df.ix["Superman"].shape, (8, ))

        # Only Catwoman is stricly shorter than 183 cm
        self.assertRaises(KeyError, helper, "Catwoman")

        # Testing FLOAT greater than or equal

        #                        This column >= 190.5
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Height"] >= 190.5].toPandas()
        self.assertEqual(len(df), 1)
        # Only Superman is taller or equal to 190.5 cm
        self.assertTupleEqual(df.ix["Superman"].shape, (7, ))

        # Everyone else are smaller
        self.assertRaises(KeyError, helper, "Batman")
        self.assertRaises(KeyError, helper, "Catwoman")
        self.assertRaises(KeyError, helper, "Wonder Woman")
        self.assertRaises(KeyError, helper, "Flash")
        self.assertRaises(KeyError, helper, "Joker")

        # Testing Column ge

        #                            This ge  This
        #                             ⇓   >=   ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Height"] >= bf["Weight"]].toPandas()
        self.assertEqual(len(df), 3)

        # The tall and thin (Height > Weight)
        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Joker"].shape, (8, ))

        # the bulky ones
        self.assertRaises(KeyError, helper, "Batman")
        self.assertRaises(KeyError, helper, "Flash")
        self.assertRaises(KeyError, helper, "Superman")
    def test_ne(self):

        #              This column != Male
        #                     ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        # Testing string equality
        bf = BatFrame(DatasetTest.dataset_url)
        df = bf[bf["Gender"] != "Male"].toPandas()
        self.assertEqual(len(df), 2)

        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (7, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (7, ))

        def helper(rowName):
            df.ix[rowName]

        self.assertRaises(KeyError, helper, "Batman")
        self.assertRaises(KeyError, helper, "Flash")
        self.assertRaises(KeyError, helper, "Joker")
        self.assertRaises(KeyError, helper, "Superman")

        # Testing INTEGER equality

        #                       This column != 183
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Height"] != 183].toPandas()
        self.assertEqual(len(df), 3)
        # Wonder Woman, Flash and Joker are all 183cm tall
        self.assertTupleEqual(df.ix["Batman"].shape, (7, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (7, ))
        self.assertTupleEqual(df.ix["Superman"].shape, (7, ))

        # Batman, Catwoman and Superman are either taller or smaller
        self.assertRaises(KeyError, helper, "Wonder Woman")
        self.assertRaises(KeyError, helper, "Flash")
        self.assertRaises(KeyError, helper, "Joker")

        # Testing FLOAT equality

        #                        This column != 190.5
        #                             ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Height"] != 190.5].toPandas()
        self.assertEqual(len(df), 5)
        # Only Superman is 190.5 cm tall
        self.assertTupleEqual(df.ix["Batman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (8, ))
        self.assertTupleEqual(df.ix["Flash"].shape, (8, ))
        self.assertTupleEqual(df.ix["Joker"].shape, (8, ))

        # Everyone else are either taller or smaller
        self.assertRaises(KeyError, helper, "Superman")

        # Testing Column inequality

        #                                         Those column not equal
        #                                               ⇓  !=  ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        df = bf[bf["Eyes"] != bf["Hair"]].toPandas()
        self.assertEqual(len(df), 5)
        # Everyone but the Joker don't have matching hair and eye color
        self.assertTupleEqual(df.ix["Batman"].shape, (7, ))
        self.assertTupleEqual(df.ix["Wonder Woman"].shape, (7, ))
        self.assertTupleEqual(df.ix["Flash"].shape, (7, ))
        self.assertTupleEqual(df.ix["Catwoman"].shape, (7, ))
        self.assertTupleEqual(df.ix["Superman"].shape, (7, ))

        # Joker, all alone in his corner
        self.assertRaises(KeyError, helper, "Joker")
示例#12
0
    def test_complex_slicing(self):
        #                                                                              True
        #                                                                               ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |  ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |  ⇐ This row
        #  ---------------------------------------------------------------------------------

        bf = BatFrame(DatasetTest.dataset_url)
        filtered = bf[bf["Bad"]]
        print(filtered)
        self.assertTupleEqual(filtered.toPandas().shape, (2, 8))
        rowNames = filtered.rows
        expected = ["Catwoman", "Joker"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        #                                                                           Not True
        #                                                                               ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        filtered = bf[~bf["Bad"]]
        print(filtered)
        self.assertTupleEqual(filtered.toPandas().shape, (4, 7))
        rowNames = filtered.rows
        expected = ["Batman", "Wonder Woman", "Flash", "Superman"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        #                                                                     True  & True
        #                                                                       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |  ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------
        filtered = bf[bf["Bad"] & bf["Good"]]
        print(filtered)
        self.assertTupleEqual(filtered.toPandas().shape, (1, 7))
        rowNames = filtered.rows
        expected = ["Catwoman"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        #                                                                     True  & Not True
        #                                                                       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        filtered = bf[bf["Bad"] & ~bf["Good"]]
        print(filtered)
        self.assertTupleEqual(filtered.toPandas().shape, (1, 8))
        rowNames = filtered.rows
        expected = ["Joker"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        #                                                                     True  |  True
        #                                                                       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        filtered = bf[bf["Bad"] | bf["Good"]]
        print(filtered)
        self.assertTupleEqual(filtered.toPandas().shape, (6, 8))
        rowNames = filtered.rows
        expected = [
            "Catwoman", "Batman", "Wonder Woman", "Flash", "Superman", "Joker"
        ]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        #                                                                     True  | Not True
        #                                                                       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        filtered = bf[bf["Bad"] | ~bf["Good"]]
        print(filtered)
        self.assertTupleEqual(filtered.toPandas().shape, (2, 8))
        rowNames = filtered.rows
        expected = ["Catwoman", "Joker"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)
示例#13
0
    def test_sort(self):

        #                             Sort on this column
        #                                      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        bf = BatFrame(DatasetTest.dataset_url)
        sort = bf.sort("Weight")  # ascending=True is default
        rows = sort.rows
        expected_rows = [
            "Wonder Woman", "Catwoman", "Joker", "Flash", "Batman", "Superman"
        ]
        for row, e_row in zip(rows, expected_rows):
            self.assertEqual(row, e_row)

        values = sort["Weight"].values
        expected_values = [130, 133, 183, 190, 210, 235]
        for value, e_value in zip(values, expected_values):
            self.assertEqual(value, e_value)

        # Reverse sort aka descending
        sort = bf.sort("Weight", ascending=False)
        rows = sort.rows
        for row, e_row in zip(rows, reversed(expected_rows)):
            self.assertEqual(row, e_row)

        values = sort["Weight"].values
        for value, e_value in zip(values, reversed(expected_values)):
            self.assertEqual(value, e_value)

        #                         Sort on those column
        #                              ⇓       ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        bf = BatFrame(DatasetTest.dataset_url)
        sort = bf.sort(["Height", "Weight"])  # ascending=True is default
        rows = sort.rows
        expected_rows = [
            "Catwoman", "Wonder Woman", "Joker", "Flash", "Batman", "Superman"
        ]
        for row, e_row in zip(rows, expected_rows):
            self.assertEqual(row, e_row)

        heights = sort["Height"].values
        expected_height = [170, 183, 183, 183, 188, 190.5]
        for height, e_height in zip(heights, expected_height):
            self.assertEqual(height, e_height)

        weights = sort["Weight"].values
        expected_weight = [133, 130, 183, 190, 210, 235]
        for weight, e_weight in zip(weights, expected_weight):
            self.assertEqual(weight, e_weight)

        # Reverse sort aka descending. Both are descending
        sort = bf.sort(["Height", "Weight"], ascending=False)
        rows = sort.rows
        for row, e_row in zip(rows, reversed(expected_rows)):
            self.assertEqual(row, e_row)

        heights = sort["Height"].values
        for height, e_height in zip(heights, reversed(expected_height)):
            self.assertEqual(height, e_height)

        weights = sort["Weight"].values
        for weight, e_weight in zip(weights, reversed(expected_weight)):
            self.assertEqual(weight, e_weight)

        # Different sorting order
        sort = bf.sort(["Height", "Weight"], [True, False])
        rows = sort.rows
        expected_rows = [
            "Catwoman", "Flash", "Joker", "Wonder Woman", "Batman", "Superman"
        ]
        for row, e_row in zip(rows, expected_rows):
            self.assertEqual(row, e_row)

        heights = sort["Height"].values
        expected_height = [170, 183, 183, 183, 188, 190.5]
        for height, e_height in zip(heights, expected_height):
            self.assertEqual(height, e_height)

        weights = sort["Weight"].values
        expected_weight = [133, 190, 183, 130, 210, 235]
        for weight, e_weight in zip(weights, expected_weight):
            self.assertEqual(weight, e_weight)

        #                                                  Sort on this column
        #                                                              ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        sort = bf.sort("Skin")
        rows = sort.rows
        expected_rows = [
            "Superman", "Wonder Woman", "Catwoman", "Batman", "Flash", "Joker"
        ]

        for row, e_row in zip(rows, expected_rows):
            self.assertEqual(row, e_row)

        heights = sort["Height"].values
        expected_height = [190.5, 183, 170, 188, 183, 183]
        for height, e_height in zip(heights, expected_height):
            self.assertEqual(height, e_height)

        weights = sort["Weight"].values
        expected_weight = [235, 130, 133, 210, 190, 183]
        for weight, e_weight in zip(weights, expected_weight):
            self.assertEqual(weight, e_weight)
示例#14
0
 def test_head(self):
     bf = BatFrame(DatasetTest.dataset_url)
     head = bf.head().toPandas()
     self.assertTupleEqual(head.shape, (5, 8))
示例#15
0
    def test_slicing(self):
        # VERY IMPORTANT
        # Slicing calls the *limit* REST parameter which will limit the number
        # of rows returned based on the **hash** of the row name. The insertion
        # order **does not** matter

        bf = BatFrame(DatasetTest.dataset_url)

        # Testing slicing [:3]

        #                                ALL THE COLUMNS
        #                    ⇓        ⇓       ⇓       ⇓       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        df = bf[:3].toPandas()
        # Right now the column Skin does not come out because no result have
        # that column
        self.assertTupleEqual(df.shape, (3, 7))
        rowNames = df.index.values.tolist()
        expected = expected = ["Superman", "Wonder Woman", "Catwoman"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        # for i, hero in enumerate(expected):
        #     msg = "\n-------------Expecting---------------\n"
        #     msg += " ".join(expected)
        #     msg += "\n\n--------------Instead got--------------\n"
        #     msg += str(" ".join(rowNames))
        #     self.assertEqual(rowNames[i], hero, msg=msg)

        # Testing slicing [2:5]

        #                                ALL THE COLUMNS
        #                    ⇓        ⇓       ⇓       ⇓       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        df = bf[2:5].toPandas()
        self.assertTupleEqual(df.shape, (4, 8))
        rowNames = df.index.values.tolist()
        expected = ["Flash", "Batman", "Catwoman", "Joker"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)

        # Testing slicing [2:5]

        #                                ALL THE COLUMNS
        #                    ⇓        ⇓       ⇓       ⇓       ⇓      ⇓
        #  ---------------------------------------------------------------------------------
        # |              | Gender | Height | Weight | Eyes  | Hair  | Skin  | Good  | Bad   |
        #  ---------------------------------------------------------------------------------
        # | Batman       | Male   |  188   |  210   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Wonder Woman | Female |  183   |  130   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Flash        | Male   |  183   |  190   | Green | Red   |       | True  | False |
        #  ---------------------------------------------------------------------------------
        # | Catwoman     | Female |  170   |  133   | Green | Black |       | True  | True  |
        #  ---------------------------------------------------------------------------------
        # | Superman     | Male   | 190.5  |  235   | Blue  | Black |       | True  | False | ⇐ This row
        #  ---------------------------------------------------------------------------------
        # | Joker        | Male   |  183   |  183   | Green | Green | White | False | True  |
        #  ---------------------------------------------------------------------------------

        df = bf[4:].toPandas()
        self.assertTupleEqual(df.shape, (2, 8))
        rowNames = df.index.values.tolist()
        expected = ["Flash", "Joker"]
        for hero in expected:
            msg = "{} expected to be in the results".format(hero)
            self.assertTrue(hero in rowNames, msg=msg)