def test_period_index(self): df = pd.DataFrame({ 'x': [1., 2.], 'y': [3., 4.] }, index=pd.date_range('1/1/2015', freq='M', periods=2).to_period()) tf = tbl.TableFormatter(df) # expected values vcopy = tf.formatted_values.copy() vcopy.iloc[1, 1] = '1 ' vcopy.iloc[2, 1] = '2 ' vcopy.iloc[1, 2] = '3 ' vcopy.iloc[2, 2] = '4 ' vcopy.iloc[1, 0] = '01/2015' vcopy.iloc[2, 0] = '02/2015' # buld the format tf.cells.int_format() tf.index.apply_format(lambda x: x.strftime('%m/%Y')) pdtest.assert_frame_equal(vcopy, tf.formatted_values) # Test when it is the columns dfT = df.T tfT = tbl.TableFormatter(dfT) vcopy = tfT.formatted_values.copy() vcopy.iloc[1, 1] = '1 ' vcopy.iloc[1, 2] = '2 ' vcopy.iloc[2, 1] = '3 ' vcopy.iloc[2, 2] = '4 ' vcopy.iloc[0, 1] = '01/2015' vcopy.iloc[0, 2] = '02/2015' # buld the format tfT.cells.int_format() tfT.header.apply_format(lambda x: x.strftime('%m/%Y')) pdtest.assert_frame_equal(vcopy, tfT.formatted_values)
def test_period_index(self): df = pd.DataFrame( { "x": [1.0, 2.0], "y": [3.0, 4.0] }, index=pd.date_range("1/1/2015", freq="M", periods=2).to_period(), ) tf = tbl.TableFormatter(df) # expected values vcopy = tf.formatted_values.copy() vcopy.iloc[1, 1] = "1 " vcopy.iloc[2, 1] = "2 " vcopy.iloc[1, 2] = "3 " vcopy.iloc[2, 2] = "4 " vcopy.iloc[1, 0] = "01/2015" vcopy.iloc[2, 0] = "02/2015" # buld the format tf.cells.int_format() tf.index.apply_format(lambda x: x.strftime("%m/%Y")) pdtest.assert_frame_equal(vcopy, tf.formatted_values) # Test when it is the columns dfT = df.T tfT = tbl.TableFormatter(dfT) vcopy = tfT.formatted_values.copy() vcopy.iloc[1, 1] = "1 " vcopy.iloc[1, 2] = "2 " vcopy.iloc[2, 1] = "3 " vcopy.iloc[2, 2] = "4 " vcopy.iloc[0, 1] = "01/2015" vcopy.iloc[0, 2] = "02/2015" # buld the format tfT.cells.int_format() tfT.header.apply_format(lambda x: x.strftime("%m/%Y")) pdtest.assert_frame_equal(vcopy, tfT.formatted_values)
def test_detect_spans(self): tf = tbl.TableFormatter(self.mdf1) tf.header.detect_colspans() self.assertEqual(['SPAN', (2, 0), (3, 0)], tf.style_cmds[0]) self.assertEqual(['SPAN', (4, 0), (5, 0)], tf.style_cmds[1]) tf = tbl.TableFormatter(self.mdf1.T) tf.index.detect_rowspans() self.assertEqual(['SPAN', (0, 2), (0, 3)], tf.style_cmds[0]) self.assertEqual(['SPAN', (0, 4), (0, 5)], tf.style_cmds[1])
def test_region_formatter_iloc(self): tf = tbl.TableFormatter(self.df1) region = tf.cells region.apply_format(lambda x: 'A') expected = pd.DataFrame([['A', 'A'], ['A', 'A']], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected) # # Use the location # region = region.iloc[:, 1] region.apply_format(lambda x: 'B') expected = pd.DataFrame([['A', 'B'], ['A', 'B']], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected) # Get single cell region = region.iloc[1] region.apply_format(lambda x: 'D') expected = pd.DataFrame([['A', 'B'], ['A', 'D']], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected) # Get single cell region = tf.cells.iloc[1, 0] region.apply_format(lambda x: 'C') expected = pd.DataFrame([['A', 'B'], ['C', 'D']], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected)
def test_region_formatter_iloc(self): tf = tbl.TableFormatter(self.df1) region = tf.cells region.apply_format(lambda x: "A") expected = pd.DataFrame([["A", "A"], ["A", "A"]], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected) # # Use the location # region = region.iloc[:, 1] region.apply_format(lambda x: "B") expected = pd.DataFrame([["A", "B"], ["A", "B"]], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected) # Get single cell region = region.iloc[1] region.apply_format(lambda x: "D") expected = pd.DataFrame([["A", "B"], ["A", "D"]], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected) # Get single cell region = tf.cells.iloc[1, 0] region.apply_format(lambda x: "C") expected = pd.DataFrame([["A", "B"], ["C", "D"]], index=[1, 2], columns=[1, 2]) pdtest.assert_frame_equal(tf.cells.formatted_values, expected)
def test_match(self): tf = tbl.TableFormatter(self.mdf1) vcopy = tf.formatted_values.copy() tf.cells.match_column_labels(['A']).percent_format(precision=1) vcopy.iloc[2, 4] = '55.0% ' # padded for neg vcopy.iloc[3, 4] = '65.0% ' vcopy.iloc[2, 2] = '55.0% ' vcopy.iloc[3, 2] = '65.0% ' pdtest.assert_frame_equal(vcopy, tf.formatted_values)
def test_region_empty(self): tf = tbl.TableFormatter(self.df1) empty = tf['ALL'].empty_frame() empty.apply_format(lambda x: x)