def test_dataframe_with_multiindex(): idx = pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 1)], names=["I1", "I2"]) df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=idx) values = _dataframe_to_var(df) expected = [['I1', 'I2', 'A', 'B'], [0, 0, 1, 4], ['', 1, 2, 5], [1, 1, 3, 6]] assert values == expected
def test_dataframe_with_multicolumns(): cols = pd.MultiIndex.from_tuples([('a', 'b'), ('a', 'c'), ('b', 'a')], names=["I1", "I2"]) df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=cols) values = _dataframe_to_var(df) expected = [['a', '', 'b'], ['b', 'c', 'a'], [1, 2, 3], [4, 5, 6], [7, 8, 9]] assert values == expected
def test_dataframe_with_multicolumns_and_index(): idx = pd.Index([0, 1, 2], name="I") cols = pd.MultiIndex.from_tuples([('a', 'b'), ('a', 'c'), ('b', 'a')], names=["I1", "I2"]) df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=cols, index=idx) values = _dataframe_to_var(df) expected = [['I1', 'a', '', 'b'], ['I \\ I2', 'b', 'c', 'a'], [0, 1, 2, 3], [1, 4, 5, 6], [2, 7, 8, 9]] assert values == expected
def test_multiindex_innermost_level(): idx = pd.MultiIndex.from_tuples([('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'd')], names=["I1", "I2", "I3"]) cols = pd.Index(['A', 'B', 'C']) df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=idx, columns=cols) values = _dataframe_to_var(df) expected = [['I1', 'I2', 'I3', 'A', 'B', 'C'], ['a', 'b', 'c', 1, 2, 3], ['', '', 'c', 4, 5, 6], ['', '', 'd', 7, 8, 9]] assert values == expected
def test_dataframe_with_multicolumns_and_multiindex(): idx = pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 1)], names=["I1", "I2"]) cols = pd.MultiIndex.from_tuples([('a', 'b'), ('a', 'c'), ('b', 'a')], names=["C1", "C2"]) df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=cols, index=idx) values = _dataframe_to_var(df) expected = [['', 'C1', 'a', '', 'b'], ['I1', 'I2 \\ C2', 'b', 'c', 'a'], [0, 0, 1, 2, 3], ['', 1, 4, 5, 6], [1, 1, 7, 8, 9]] assert values == expected
def test_nat_df(): """Test that NaT gets converted to an ArithmeticError exception""" df = pd.DataFrame({"A": [pd.NaT]}) values = _dataframe_to_var(df) assert values[0][0] == "A" assert isinstance(values[1][0], ArithmeticError)
def test_dataframe_with_named_index(): idx = pd.Index([0, 1, 2], name="I") df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=idx) values = _dataframe_to_var(df) expected = [['I', 'A', 'B'], [0, 1, 4], [1, 2, 5], [2, 3, 6]] assert values == expected
def test_dataframe(): df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) values = _dataframe_to_var(df) expected = [['A', 'B'], [1, 4], [2, 5], [3, 6]] assert values == expected