def test_dataframemodel_set_data_complex(monkeypatch): """ Test that editing complex dtypes is handled gracefully in df editor. Unit regression test for issue #6115 . """ MockQMessageBox = Mock() attr_to_patch = ('spyder_modelx.widgets' + '.mxdataview.QMessageBox') monkeypatch.setattr(attr_to_patch, MockQMessageBox) test_params = [(1, numpy.complex128), (2, numpy.complex64), (3, complex)] for count, complex_type in test_params: test_df = DataFrame(numpy.arange(10, 15), dtype=complex_type) model = DataFrameModel(test_df.copy()) index = model.createIndex(2, 0) assert not model.setData(index, '42') MockQMessageBox.critical.assert_called_with(ANY, "Error", ANY) assert MockQMessageBox.critical.call_count == count try: assert numpy.sum(test_df[0].values == model.df.values) == len(test_df) except AttributeError: assert numpy.sum(test_df[0].as_matrix() == model.df.as_matrix()) == len(test_df)
def test_dataframemodel_set_data_overflow(monkeypatch): """ Test that entry of an overflowing integer is caught and handled properly. Unit regression test for issue #6114 . """ MockQMessageBox = Mock() attr_to_patch = ('spyder_modelx.widgets' + '.mxdataview.QMessageBox') monkeypatch.setattr(attr_to_patch, MockQMessageBox) # Numpy doesn't raise the OverflowError for ints smaller than 64 bits if not os.name == 'nt': int32_bit_exponent = 66 else: int32_bit_exponent = 34 test_parameters = [(1, numpy.int32, int32_bit_exponent), (2, numpy.int64, 66)] for idx, int_type, bit_exponent in test_parameters: test_df = DataFrame(numpy.arange(7, 11), dtype=int_type) model = DataFrameModel(test_df.copy()) index = model.createIndex(2, 0) assert not model.setData(index, str(int(2 ** bit_exponent))) MockQMessageBox.critical.assert_called_with(ANY, "Error", ANY) assert MockQMessageBox.critical.call_count == idx try: assert numpy.sum(test_df[0].values == model.df.values) == len(test_df) except AttributeError: assert numpy.sum(test_df[0].as_matrix() == model.df.as_matrix()) == len(test_df)
def test_dataframeeditor_with_OutOfBoundsDatetime(): # Test for #6177 df = DataFrame([{'DATETIME': datetime.strptime("9999-1-1T00:00", "%Y-%m-%dT%H:%M")}]) model = DataFrameModel(df) try: model.get_value(0, 0) except Exception: assert False
def test_dataframemodel_sort(): """Validate the data in the model.""" df = DataFrame({'colA': [1, 3], 'colB': ['c', 'a']}) dfm = DataFrameModel(df) dfm.sort(1) assert data(dfm, 0, 0) == '3' assert data(dfm, 1, 0) == '1' assert data(dfm, 0, 1) == 'a' assert data(dfm, 1, 1) == 'c'
def test_dataframemodel_basic(): df = DataFrame({'colA': [1, 3], 'colB': ['c', 'a']}) dfm = DataFrameModel(df) assert dfm.rowCount() == 2 assert dfm.columnCount() == 2 assert data(dfm, 0, 0) == '1' assert data(dfm, 0, 1) == 'c' assert data(dfm, 1, 0) == '3' assert data(dfm, 1, 1) == 'a'
def test_dataframemodel_sort_is_stable(): # cf. issue 3010 """Validate the sort function.""" df = DataFrame([[2,14], [2,13], [2,16], [1,3], [2,9], [1,15], [1,17], [2,2], [2,10], [1,6], [2,5], [2,8], [1,11], [1,1], [1,12], [1,4], [2,7]]) dfm = DataFrameModel(df) dfm.sort(1) dfm.sort(0) col2 = [data(dfm, i, 1) for i in range(len(df))] assert col2 == [str(x) for x in [1, 3, 4, 6, 11, 12, 15, 17, 2, 5, 7, 8, 9, 10, 13, 14, 16]]
def test_dataframemodel_set_data_bool(monkeypatch): """Test that bools are editible in df and false-y strs are detected.""" MockQMessageBox = Mock() attr_to_patch = ('spyder_modelx.widgets' + '.mxdataview.QMessageBox') monkeypatch.setattr(attr_to_patch, MockQMessageBox) test_params = [numpy.bool_, numpy.bool, bool] test_strs = ['foo', 'false', 'f', '0', '0.', '0.0', '', ' '] expected_df = DataFrame([1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=bool) for bool_type in test_params: test_df = DataFrame([0, 1, 1, 1, 1, 1, 1, 1, 0], dtype=bool_type) model = DataFrameModel(test_df.copy()) for idx, test_str in enumerate(test_strs): assert model.setData(model.createIndex(idx, 0), test_str) assert not MockQMessageBox.critical.called try: assert numpy.sum(expected_df[0].values == model.df.values[:, 0]) == len(expected_df) except AttributeError: assert numpy.sum(expected_df[0].as_matrix() == model.df.as_matrix()[:, 0]) == len(expected_df)