Exemplo n.º 1
0
    def test_str_ordering(self):
        # Tests ordering of a string column as strings
        self.column_pval.value = 'name'
        self.column_pval.save()
        # dtype is string
        self.dtype_pval.value = 0
        self.dtype_pval.save()

        # If direction is "Select", NOP
        self.direction_pval.value = 0
        self.direction_pval.save()
        out = execute_nocache(self.wf_module)
        self.assertTrue(out.equals(self.table))

        # If direction is "Ascending"
        self.direction_pval.value = 1
        self.direction_pval.save()
        out = execute_nocache(self.wf_module)
        ref_order = [1, 0, 3, 2]
        ref_table = reorder_table(self.table, ref_order)
        self.assertTrue(out.equals(ref_table))

        # If direction is "Descending"
        self.direction_pval.value = 2
        self.direction_pval.save()
        out = execute_nocache(self.wf_module)
        ref_order = [2, 0, 3, 1]
        ref_table = reorder_table(self.table, ref_order)
        self.assertTrue(out.equals(ref_table))

        # Tests ordering of a numeric column as strings
        self.column_pval.value = 'float'
        self.column_pval.save()
        # dtype is string
        self.dtype_pval.value = 0
        self.dtype_pval.save()
        # We only test Ascending here; others have been covered above
        self.direction_pval.value = 1
        self.direction_pval.save()
        out = execute_nocache(self.wf_module)
        ref_order = [0, 1, 3, 2]
        ref_table = reorder_table(self.table, ref_order)
        self.assertTrue(out.equals(ref_table))

        # Test ordering of a date column as string,
        # using SortFromTable's render() directly
        mock_sort = MockModule({
            'column': 'date',
            'dtype': 0,
            # We only test Ascending here; others have been covered above
            'direction': 1
        })
        out = SortFromTable.render(mock_sort, self.dates_table.copy())
        ref_order = [2, 0, 1, 3]
        ref_table = reorder_table(self.dates_table, ref_order)
        self.assertTrue(out.equals(ref_table))
Exemplo n.º 2
0
 def test_order_str_as_number(self):
     table = pd.DataFrame({'A': ['30', '..nan', '4'], 'B': ['a', 'b', 'c']})
     params = MockParams(column='A', dtype=1, direction=1)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': ['4', '30', '..nan'],
             'B': ['c', 'a', 'b']
         }))
     self.assertEqual(result, expected)
Exemplo n.º 3
0
 def test_order_number_as_number_descending(self):
     table = pd.DataFrame({'A': [3.0, np.nan, 2.1], 'B': ['a', 'b', 'c']})
     params = MockParams(column='A', dtype=1, direction=2)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': [3.0, 2.1, np.nan],
             'B': ['a', 'c', 'b']
         }))
     self.assertEqual(result, expected)
Exemplo n.º 4
0
 def test_order_number_as_str(self):
     table = pd.DataFrame({'A': ['a', 'b', 'c'], 'B': [1, 3, 22]})
     params = MockParams(column='B', dtype=0, direction=1)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': ['a', 'c', 'b'],
             'B': [1, 22, 3]
         }))
     self.assertEqual(result, expected)
Exemplo n.º 5
0
 def test_order_str_as_str_descending(self):
     table = pd.DataFrame({'A': ['a', 'c', 'b'], 'B': [1, 2, 3]})
     params = MockParams(column='A', dtype=0, direction=2)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': ['c', 'b', 'a'],
             'B': [2, 3, 1]
         }))
     self.assertEqual(result, expected)
Exemplo n.º 6
0
 def test_order_number_as_number_ascending(self):
     table = pd.DataFrame({'A': [3.0, np.nan, 2.1], 'B': ['a', 'b', 'c']})
     wf_module = MockWfModule(column='A', dtype=1, direction=1)
     result = SortFromTable.render(wf_module, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': [2.1, 3.0, np.nan],
             'B': ['c', 'a', 'b']
         }))
     self.assertEqual(result, expected)
Exemplo n.º 7
0
 def test_order_str_as_str_ascending(self):
     table = pd.DataFrame({'A': ['a', 'c', 'b'], 'B': [1, 2, 3]})
     wf_module = MockWfModule(column='A', dtype=0, direction=1)
     result = SortFromTable.render(wf_module, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': ['a', 'b', 'c'],
             'B': [1, 3, 2]
         }))
     self.assertEqual(result, expected)
Exemplo n.º 8
0
 def test_order_date(self):
     d1 = datetime.datetime(2018, 8, 15, 1, 23, 45)
     d2 = datetime.datetime(2018, 8, 15, 1, 34, 56)
     table = pd.DataFrame({'A': [d2, d1], 'B': ['a', 'b']})
     params = MockParams(column='A', dtype=2, direction=1)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(pd.DataFrame({
         'A': [d1, d2],
         'B': ['b', 'a']
     }))
     self.assertEqual(result, expected)
Exemplo n.º 9
0
 def test_order_cat_str_as_str_ascending(self):
     table = pd.DataFrame({'A': ['a', 'c', 'b'], 'B': [1, 2, 3]})
     table['A'] = table['A'].astype('category')
     params = MockParams(column='A', dtype=0, direction=1)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': ['a', 'b', 'c'],
             'B': [1, 3, 2]
         }))
     expected.dataframe['A'] = expected.dataframe['A'].astype('category')
     self.assertEqual(result, expected)
Exemplo n.º 10
0
 def test_order_missing_direction(self):
     table = pd.DataFrame({'A': ['a', 'c', 'b'], 'B': [1, 2, 3]})
     # no-op because no direction is specified
     # TODO nix the very _possibility_ of no direction. Why would anybody
     # ever want to sort by no direction?
     params = MockParams(column='A', dtype=0, direction=0)
     result = SortFromTable.render(params, table)
     expected = ProcessResult(
         pd.DataFrame({
             'A': ['a', 'c', 'b'],
             'B': [1, 2, 3]
         }))
     self.assertEqual(result, expected)