Esempio n. 1
0
    def test_concat_inplace_is_true_empty_col_in_input(self):
        df = sampy.DataFrameXS()
        df['col1'] = [5, 6, 7, 8]
        df['col2'] = None

        self.df.concat(df, inplace=True)

        self.assertTrue((np.nan_to_num(self.df['col2'], nan=0.) == np.array(
            [2., 3., 4., 5., 0., 0., 0., 0.])).all())
        self.assertTrue((self.df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                      8])).all())
        self.assertEqual(str(self.df['col1'].dtype), str(df['col1'].dtype))
Esempio n. 2
0
    def test_concat_inplace_is_true_basic(self):
        df = sampy.DataFrameXS()
        df['col1'] = [5, 6, 7, 8]
        df['col2'] = [6., 7., 8., 9.]
        self.df.concat(df, inplace=True)

        # concats happened as expected
        self.assertTrue((self.df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                      8])).all())
        self.assertTrue(
            (self.df['col2'] == np.array([2., 3., 4., 5., 6., 7., 8.,
                                          9.])).all())
Esempio n. 3
0
    def test_concat_inplace_is_true_empty_col_on_empty_col(self):
        self.df['col2'] = None

        df = sampy.DataFrameXS()
        df['col1'] = [5, 6, 7, 8]
        df['col2'] = None

        self.df.concat(df, inplace=True)

        self.assertIsNone(self.df['col2'])
        self.assertTrue((self.df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                      8])).all())
        self.assertEqual(str(self.df['col1'].dtype), str(df['col1'].dtype))
Esempio n. 4
0
    def test_concat_inplace_is_true_on_empty_df(self):
        self.df['col1'] = None
        self.df['col2'] = None

        df = sampy.DataFrameXS()
        df['col1'] = [5, 6, 7, 8]
        df['col2'] = [6., 7., 8., 9.]
        self.df.concat(df, inplace=True)

        self.assertTrue((self.df['col1'] == np.array([5, 6, 7, 8])).all())
        self.assertEqual(str(self.df['col1'].dtype), str(df['col1'].dtype))
        self.assertTrue((np.nan_to_num(self.df['col2'],
                                       nan=0.) == np.array([6., 7., 8.,
                                                            9.])).all())
        self.assertEqual(str(self.df['col2'].dtype), str(df['col2'].dtype))

        self.assertEqual(self.df.nb_rows, 4)
Esempio n. 5
0
    def test_concat_inplace_is_true_extra_col(self):
        df = sampy.DataFrameXS()
        df['col1'] = [5, 6, 7, 8]
        df['col2'] = [6., 7., 8., 9.]
        df['col3'] = [1, 2, 3, 4]
        self.df.concat(df, inplace=True)

        # check that there is no col3 in the dataframe
        with self.assertRaises(KeyError):
            self.df['col3']

        # perform all the previous checks in this case.
        # concat happened as expected
        self.assertTrue((self.df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                      8])).all())
        self.assertTrue(
            (self.df['col2'] == np.array([2., 3., 4., 5., 6., 7., 8.,
                                          9.])).all())
        self.assertEqual(self.df.nb_rows, 8)
Esempio n. 6
0
 def test_concat_inplace_is_true_wrong_col_name(self):
     df = sampy.DataFrameXS()
     df['col1'] = [5, 6, 7, 8]
     df['col3'] = [6., 7., 8., 9.]
     with self.assertRaises(KeyError):
         self.df.concat(df, inplace=True)
Esempio n. 7
0
    def test_concat_inplace_is_false(self):
        with self.subTest():
            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col2'] = [6., 7., 8., 9.]
            c_df = self.df.concat(df, inplace=False)

            # concats happened as expected
            self.assertTrue((c_df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                       8])).all())
            self.assertTrue(
                (c_df['col2'] == np.array([2., 3., 4., 5., 6., 7., 8.,
                                           9.])).all())

            # columns are new copied columns
            c_df['col1'][0] = 0
            self.assertFalse(c_df['col1'][0] == self.df['col1'][0])

        with self.subTest():
            # same test as the previous one, but there is an extra column
            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col2'] = [6., 7., 8., 9.]
            df['col3'] = [1, 2, 3, 4]
            c_df = self.df.concat(df, inplace=False)

            # check that there is no col3 in the dataframe
            with self.assertRaises(KeyError):
                c_df['col3']

            # perform all the previous checks in this case.
            # concat happened as expected
            self.assertTrue((c_df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                       8])).all())
            self.assertTrue(
                (c_df['col2'] == np.array([2., 3., 4., 5., 6., 7., 8.,
                                           9.])).all())

            # columns are new copied columns
            c_df['col1'][0] = 0
            self.assertFalse(c_df['col1'][0] == self.df['col1'][0])

        # check that an error is raised when the df don't have the right col_names
        with self.subTest():
            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col3'] = [6., 7., 8., 9.]
            with self.assertRaises(KeyError):
                self.df.concat(df, inplace=False)

        # check that appending with an empty dataframe returns a copy
        with self.subTest():
            df = sampy.DataFrameXS()
            c_df = self.df.concat(df, inplace=False)
            self.assertTrue((c_df['col1'] == self.df['col1']).all())
            self.assertTrue((c_df['col2'] == self.df['col2']).all())

            c_df['col1'][0] = 0
            self.assertFalse(c_df['col1'][0] == self.df['col1'][0])

        # check that having a null column in the input is correctly dealt with
        with self.subTest():
            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col2'] = None
            c_df = self.df.concat(df, inplace=False)
            self.assertTrue((c_df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                       8])).all())
            self.assertTrue((np.nan_to_num(c_df['col2'], nan=0.) == np.array(
                [2., 3., 4., 5., 0., 0., 0., 0.])).all())
            self.assertEqual(str(c_df['col2'].dtype),
                             str(self.df['col2'].dtype))

        # same thing but in reverse
        with self.subTest():
            df_in = sampy.DataFrameXS()
            df_in['col1'] = [1, 2, 3, 4]
            df_in['col2'] = None

            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col2'] = [6., 7., 8., 9.]
            c_df = df_in.concat(df, inplace=False)
            self.assertTrue((c_df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                       8])).all())
            self.assertTrue((np.nan_to_num(c_df['col2'], 0.) == np.array(
                [0., 0., 0., 0., 6., 7., 8., 9.])).all())
            self.assertEqual(str(c_df['col2'].dtype), str(df['col2'].dtype))

        # When the first df is empty
        with self.subTest():
            df_in = sampy.DataFrameXS()
            df_in['col1'] = None
            df_in['col2'] = None

            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col2'] = [6., 7., 8., 9.]
            c_df = df_in.concat(df, inplace=False)

            self.assertTrue((c_df['col1'] == np.array([5, 6, 7, 8])).all())
            self.assertEqual(str(c_df['col1'].dtype), str(df['col1'].dtype))
            self.assertTrue(
                (np.nan_to_num(c_df['col2'],
                               nan=0.) == np.array([6., 7., 8., 9.])).all())
            self.assertEqual(str(c_df['col2'].dtype), str(df['col2'].dtype))

            # check that a copy is retrieved
            c_df['col1'][0] = 0
            self.assertFalse((c_df['col1'] == df['col1']).all())

        # adding an empty column on an empty column
        with self.subTest():
            df_in = sampy.DataFrameXS()
            df_in['col1'] = [1, 2, 3, 4]
            df_in['col2'] = None

            df = sampy.DataFrameXS()
            df['col1'] = [5, 6, 7, 8]
            df['col2'] = None

            c_df = df_in.concat(df, inplace=False)

            self.assertIsNone(c_df['col2'])
            self.assertTrue((c_df['col1'] == np.array([1, 2, 3, 4, 5, 6, 7,
                                                       8])).all())
            self.assertEqual(str(c_df['col1'].dtype), str(df['col1'].dtype))
Esempio n. 8
0
 def setUp(self):
     self.df = sampy.DataFrameXS()
     self.df['col1'] = [1, 2, 3, 4]
     self.df['col2'] = [2., 3., 4., 5.]
Esempio n. 9
0
 def setUp(self):
     self.df = sampy.DataFrameXS()