def test_wide_to_long_mixed_value_types(self): in_table = pd.DataFrame({ 'X': ['x', 'y'], 'A': [1, 2], 'B': ['y', np.nan] }) result = render(in_table, P('widetolong', 'X'), **DefaultKwargs) assert_frame_equal(result['dataframe'], pd.DataFrame({ 'X': ['x', 'x', 'y', 'y'], 'variable': ['A', 'B', 'A', 'B'], 'value': ['1', 'y', '2', np.nan], })) self.assertEqual(result['error'], ( 'Columns "A" were auto-converted to Text because the value column ' 'cannot have multiple types.' )) self.assertEqual(result['quick_fixes'], [{ 'text': 'Convert "A" to text', 'action': 'prependModule', 'args': [ 'converttotext', {'colnames': ['A']}, ], }]) self.assertIsInstance(result['quick_fixes'][0]['args'][1]['colnames'], list)
def test_transpose(self): # (Most tests are in the `transpose` module....) in_table = pd.DataFrame({ 'Name': ['Date', 'Attr'], 'Dolores': ['2018-04-22', '10'], 'Robert': ['2016-10-02', None], 'Teddy': ['2018-04-22', '8'] }) result = render(in_table, P('transpose'), input_columns={ 'Name': Column('Name', 'text'), 'Dolores': Column('Dolores', 'text'), 'Robert': Column('Robert', 'text'), 'Teddy': Column('Teddy', 'text'), }) # Keeping the old header for the first column can be confusing. # First column header doesnt usually classify rest of headers. # Renaming first column header 'New Column' expected = pd.DataFrame({ 'New Column': ['Dolores', 'Robert', 'Teddy'], 'Date': ['2018-04-22', '2016-10-02', '2018-04-22'], 'Attr': ['10', None, '8'] }) assert_frame_equal(result, expected)
def test_long_to_wide_convert_to_str(self): in_table = pd.DataFrame({ 'x': [1, 1, 2, 2, 3, 3], 'variable': [4, 5, 4, 5, 4, 5], 'value': list('adbecf'), }) result = render(in_table, P('longtowide', 'x', 'variable'), **DefaultKwargs) assert_frame_equal(result['dataframe'], pd.DataFrame({ 'x': [1, 2, 3], '4': ['a', 'b', 'c'], '5': ['d', 'e', 'f'], })) self.assertEqual(result['error'], ( 'Column "variable" was auto-converted to Text because column ' 'names must be text.' )) self.assertEqual(result['quick_fixes'], [{ 'text': 'Convert "variable" to text', 'action': 'prependModule', 'args': [ 'converttotext', {'colnames': ['variable']}, ], }])
def test_long_to_wide_error_not_enough_columns(self): in_table = pd.DataFrame({"x": [1, 2], "variable": ["y", "y"]}) result = render(in_table, P("longtowide", "x", "variable"), **DefaultKwargs) self.assertEqual( result, "There is no Value column. All but one table column must be a Row or Column variable.", )
def test_long_to_wide_duplicate_key(self): in_table = pd.DataFrame({ "x": [1, 1], "variable": ["A", "A"], "value": ["x", "y"] }) out = render(in_table, P("longtowide", "x", "variable")) self.assertEqual(out, "Cannot reshape: some variables are repeated")
def test_long_to_wide_duplicate_key(self): in_table = pd.DataFrame({ 'x': [1, 1], 'variable': ['A', 'A'], 'value': ['x', 'y'], }) out = render(in_table, P('longtowide', 'x', 'variable')) self.assertEqual(out, 'Cannot reshape: some variables are repeated')
def test_wide_to_long_no_values_or_variables_categorical_id_var(self): result = render(pd.DataFrame({'A': []}, dtype='category'), P('widetolong', 'A')) assert_frame_equal(result, pd.DataFrame({ 'A': [], 'variable': [], 'value': [], }, dtype=str))
def test_long_to_wide_mulicolumn(self): # two ID columns params = { 'direction': 1, 'colnames': 'idcol,date', 'varcol': 'variable' } out = render(self.long2, params) self.assertTrue(out.equals(self.wide2))
def test_long_to_wide_varcol_in_key(self): in_table = pd.DataFrame({ "x": ["1", "2"], "variable": ["A", "B"], "value": ["a", "b"] }) out = render(in_table, P("longtowide", ["x"], ltw_varcolname="x"), **DefaultKwargs) self.assertEqual(out, i18n_message("error.sameColumnAndRowVariables"))
def test_long_to_wide_duplicate_key(self): in_table = pd.DataFrame({ "x": [1, 1], "variable": ["A", "A"], "value": ["x", "y"] }) out = render(in_table, P("longtowide", ["x"], ltw_varcolname="variable"), **DefaultKwargs) self.assertEqual(out, i18n_message("long_to_wide.error.repeatedVariables"))
def test_wide_to_long_no_values_or_variables_categorical_id_var(self): result = render(pd.DataFrame({"A": []}, dtype="category"), P("widetolong", "A")) assert_frame_equal( result, pd.DataFrame({ "A": [], "variable": [], "value": [] }, dtype=str))
def test_long_to_wide_varcol_in_key(self): in_table = pd.DataFrame({ 'x': ['1', '2'], 'variable': ['A', 'B'], 'value': ['a', 'b'], }) out = render(in_table, P('longtowide', 'x', 'x')) self.assertEqual(out, ( 'Cannot reshape: column and row variables must be different' ))
def test_long_to_wide_varcol_in_key(self): in_table = pd.DataFrame({ "x": ["1", "2"], "variable": ["A", "B"], "value": ["a", "b"] }) out = render(in_table, P("longtowide", "x", "x")) self.assertEqual( out, ("Cannot reshape: column and row variables must be different"))
def test_long_to_wide_treat_empty_string_category_as_empty_string(self): # https://www.pivotaltracker.com/story/show/174929289 in_table = pd.DataFrame({ "A": ["a", "b"], "B": pd.Series(["", ""], dtype="category"), "C": [1, 2], }) out = render(in_table, P("longtowide", ["A"], ltw_varcolname="B"), **DefaultKwargs) assert_frame_equal(out[0], pd.DataFrame({"A": pd.Series([], dtype=object)}))
def test_long_to_wide_varcol_in_key(self): in_table = pd.DataFrame({ 'x': ['1', '2'], 'variable': ['A', 'B'], 'value': ['a', 'b'], }) params = {'direction': 'longtowide', 'colnames': 'x', 'varcol': 'x'} out = render(in_table, params) self.assertEqual( out, ('Cannot reshape: column and row variables must be different'))
def test_long_to_wide_error_too_many_columns(self): in_table = pd.DataFrame({ "x": [1, 2], "variable": ["y", "y"], "value": ["a", "b"], "other": ["", ""], }) result = render(in_table, P("longtowide", ["x"], ltw_varcolname="variable"), **DefaultKwargs) self.assertEqual( result, i18n_message("long_to_wide.error.tooManyValueColumns"))
def test_long_to_wide(self): in_table = pd.DataFrame({ 'x': [1, 1, 2, 2, 3, 3], 'variable': list('ABABAB'), 'value': list('adbecf'), }) out = render(in_table, P('longtowide', 'x', 'variable')) assert_frame_equal(out, pd.DataFrame({ 'x': [1, 2, 3], 'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], }))
def test_long_to_wide_nix_empty_leaving_empty_table(self): in_table = pd.DataFrame({ "x": [1, 2], "variable": ["", np.nan], "value": ["a", "b"] }) result = render(in_table, P("longtowide", "x", "variable"), **DefaultKwargs) assert_frame_equal(result["dataframe"], pd.DataFrame({"x": pd.Series([], dtype=int)})) self.assertEqual(result["error"], '2 input rows with empty "variable" were removed.')
def test_wide_to_long(self): in_table = pd.DataFrame({ 'x': [1, 2, 3], 'A': [4, 5, 6], 'B': [7, 8, 9], }) out = render(in_table, P('widetolong', 'x')) assert_frame_equal(out, pd.DataFrame({ 'x': [1, 1, 2, 2, 3, 3], 'variable': list('ABABAB'), 'value': [4, 7, 5, 8, 6, 9], }))
def test_long_to_wide_nix_empty_leaving_empty_table(self): in_table = pd.DataFrame({ 'x': [1, 2], 'variable': ['', np.nan], 'value': ['a', 'b'] }) result = render(in_table, P('longtowide', 'x', 'variable'), **DefaultKwargs) assert_frame_equal(result['dataframe'], pd.DataFrame({ 'x': pd.Series([], dtype=int), })) self.assertEqual(result['error'], '2 input rows with empty "variable" were removed.')
def test_long_to_wide_error_too_many_columns(self): in_table = pd.DataFrame({ "x": [1, 2], "variable": ["y", "y"], "value": ["a", "b"], "other": ["", ""], }) result = render(in_table, P("longtowide", "x", "variable"), **DefaultKwargs) self.assertEqual( result, "There are too many Value columns. All but one table column must be a Row or Column variable. Please drop extra columns before reshaping.", )
def test_long_to_wide_categoricals(self): in_table = pd.DataFrame({ 'x': list('112233'), 'variable': list('ABABAB'), 'value': list('adbecf'), }, dtype='category') out = render(in_table, P('longtowide', 'x', 'variable'), **DefaultKwargs) assert_frame_equal(out, pd.DataFrame({ 'x': pd.Series(['1', '2', '3'], dtype='category'), 'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], }))
def test_long_to_wide_duplicate_key(self): in_table = pd.DataFrame({ 'x': [1, 1], 'variable': ['A', 'A'], 'value': ['x', 'y'], }) params = { 'direction': 'longtowide', 'colnames': 'x', 'varcol': 'variable' } out = render(in_table, params) self.assertEqual(out, 'Cannot reshape: some variables are repeated')
def test_wide_to_long_valcolname_conflict(self): out = render( pd.DataFrame({ "A": [1], "B": [2], "C": [3] }), P("widetolong", ["A"], wtl_varcolname="C", wtl_valcolname="A"), **DefaultKwargs) self.assertEqual( out, (None, [i18n_message("wide_to_long.badColumns.valcolname.conflict")]), )
def test_long_to_wide_checkbox_but_no_second_key(self): """has_second_key does nothing if no second column is chosen.""" in_table = pd.DataFrame({ 'x': [1, 1, 2, 2, 3, 3], 'variable': list('ABABAB'), 'value': list('adbecf'), }) out = render(in_table, P('longtowide', 'x', 'variable', has_second_key=True)) assert_frame_equal(out, pd.DataFrame({ 'x': [1, 2, 3], 'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], }))
def test_long_to_wide_nix_empty(self): in_table = pd.DataFrame({ 'x': [1, 2, 3], 'variable': ['', np.nan, 'foo'], 'value': ['a', 'b', 'c'] }) result = render(in_table, P('longtowide', 'x', 'variable'), **DefaultKwargs) assert_frame_equal(result['dataframe'], pd.DataFrame({ 'x': [3], 'foo': ['c'], })) self.assertEqual(result['error'], '2 input rows with empty "variable" were removed.')
def test_long_to_wide_nix_empty(self): in_table = pd.DataFrame({ "x": [1, 2, 3], "variable": ["", np.nan, "foo"], "value": ["a", "b", "c"] }) result = render(in_table, P("longtowide", "x", "variable"), **DefaultKwargs) assert_frame_equal(result["dataframe"], pd.DataFrame({ "x": [3], "foo": ["c"] })) self.assertEqual(result["error"], '2 input rows with empty "variable" were removed.')
def test_long_to_wide_two_keys(self): """Long-to-wide with second_key: identical to two colnames.""" in_table = pd.DataFrame({ 'x': [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], 'y': [4, 4, 5, 5, 4, 4, 5, 5, 4, 4, 5, 5], 'variable': list('ABABABABABAB'), 'value': list('abcdefghijkl'), }) out = render(in_table, P('longtowide', 'x', 'variable', True, 'y')) assert_frame_equal(out, pd.DataFrame({ 'x': [1, 1, 2, 2, 3, 3], 'y': [4, 5, 4, 5, 4, 5], 'A': list('acegik'), 'B': list('bdfhjl'), }))
def test_long_to_wide(self): in_table = pd.DataFrame({ "x": [1, 1, 2, 2, 3, 3], "variable": list("ABABAB"), "value": list("adbecf"), }) out = render(in_table, P("longtowide", "x", "variable")) assert_frame_equal( out, pd.DataFrame({ "x": [1, 2, 3], "A": ["a", "b", "c"], "B": ["d", "e", "f"] }), )
def test_wide_to_long(self): in_table = pd.DataFrame({ "x": [1, 2, 3], "A": [4, 5, 6], "B": [7, 8, 9] }) out = render(in_table, P("widetolong", "x")) assert_frame_equal( out, pd.DataFrame({ "x": [1, 1, 2, 2, 3, 3], "variable": list("ABABAB"), "value": [4, 7, 5, 8, 6, 9], }), )