public void Test_ProcessMultiple_WithRowIndex_ColumnName()
        {
            // Given
            DataFrameRowIndexColumnNameOperator <double> rowOperator = (rowIdx, colName, currentVal) =>
            {
                if (colName == "Col1")
                {
                    return(Math.Pow(currentVal, 2));
                }

                return(currentVal);
            };

            var expectedMatrix =
                Matrix <double> .Build.DenseOfArray(new double[, ] {
                { 1, 2, 3, 4 }, { 25, 6, 7, 8 }, { 81, 10, 11, 12 }
            });

            var baseDataFrame = TestDataBuilder.BuildSmallDataFrameNumbersOnly();

            // When
            var actualMatrix = baseDataFrame.ProcessMultiple(rowOperator).GetAsMatrix();

            // Then
            Assert.IsTrue(expectedMatrix.Equals(actualMatrix));
        }
Exemple #2
0
        public IDataFrame ProcessMultiple <TValue>(DataFrameRowIndexColumnNameOperator <TValue> rowOperator)
        {
            var newData = DataTable.DefaultView.ToTable();

            Parallel.For(0, newData.Rows.Count, rowIdx =>
            {
                var row = newData.Rows[rowIdx];
                for (int colIdx = 0; colIdx < row.ItemArray.Length; colIdx++)
                {
                    var newVal = rowOperator(rowIdx, ColumnNames[colIdx], (TValue)Convert.ChangeType(row[colIdx], typeof(TValue)));
                    lock (this.locker)
                    {
                        row[colIdx] = newVal;
                    }
                }
            });
            return(new DataFrame(newData, new List <int>(RowIndices)));
        }