/// <summary> /// Get a DataFrame with multiple rows specified by an array of indexes /// </summary> /// <param name="indexes">Array of int representing the indexes of the rows to get</param> /// <returns>A DataFrame</returns> public DataFrame ILoc(int[] indexArray) { // Create an array of stings that holds the indexers string[] indexers = new string[indexArray.Length]; for (int i = 0; i < indexArray.Length; i++) { if (Indexers.Length < indexArray[i]) { throw new IndexOutOfRangeException(string.Format("Indexers dose not contain the index {0}", indexArray[i])); } indexers[i] = Indexers[indexArray[i]]; } Series[] series = new Series[indexers.Length]; for (int i = 0; i < indexers.Length; i++) { object[] data = new object[Array.GetLength(1)]; for (int j = 0; j < Array.GetLength(1); j++) { data[j] = Array[indexArray[i]][j]; } series[i] = new Series(data); } SeriesArray seriesArray = new SeriesArray(series); return(new DataFrame(seriesArray, Headers, indexers)); }
public DataFrame Shift(int shift) { if (shift == 0) { throw new ArgumentOutOfRangeException("Shift must be positive or negative, not zero"); } Series[] series = new Series[Array.GetLength(0)]; for (int i = 0; i < Array.GetLength(0); i++) { object[] data = new object[1]; if (shift < 0) { data[0] = (i - shift < Array.GetLength(0)) ? Array[i - shift][0] : 0; } else { data[0] = (i >= shift) ? Array[i - shift][0] : 0; } series[i] = new Series(data); } SeriesArray seriesArray = new SeriesArray(series); return(new DataFrame(seriesArray, Headers, Indexers)); }
public static DataFrame operator /(DataFrame df1, DataFrame df2) { Series[] series = new Series[df1.Array.GetLength(0)]; for (int i = 0; i < df1.Array.GetLength(0); i++) { double df1Value = 0, df2Value = 0; if (df1.Array[i][0] != null) { double.TryParse(df1.Array[i][0].ToString(), out df1Value); } if (df2.Array[i][0] != null) { double.TryParse(df2.Array[i][0].ToString(), out df2Value); } series[i] = new Series(new object[] { Math.Round(df1Value / df2Value, 2) }); } string[] headers = new string[] { "null" }; SeriesArray seriesArray = new SeriesArray(series); return(new DataFrame(seriesArray, headers, df1.Indexers)); }
/// <summary> /// Get a DataFrame with only the columns specified by index /// </summary> /// <param name="indexArray">Array of ints representing the indexes</param> /// <returns>A DataFrame</returns> public DataFrame this[int[] indexArray] { get { string[] headerArray = null; // If headers of this DataFrame not null, we should add the correspoing headers to the new Dataframe if (Headers.Length > 0) { // Instantiates a new array of string // Array should hold the headers headerArray = new string[indexArray.Length]; // There should be an equal number of headers as there are columns selecting // Loop throug all columns selecting // If the index selecting is grader then the length of current headers, throw an IndexOutOfRangeException for (int i = 0; i < indexArray.Length; i++) { if (indexArray[i] > Headers.Length) { throw new IndexOutOfRangeException(string.Format("Headers dose not contain the index {0}", indexArray[i])); } headerArray[i] = Headers[indexArray[i]]; } } // Create an array of series thet should hold the data // The number of Series should equal the number of rows in Array Series[] series = new Series[Array.GetLength(0)]; // For each row in the array for (int i = 0; i < Array.GetLength(0); i++) { // Store the data of the columns we wan't, based on the indexArray passed to function object[] data = new object[indexArray.Length]; for (int j = 0; j < indexArray.Length; j++) { data[j] = Array[i][indexArray[j]]; } // Instanceate a new series(row) with the data(columns), series[i] = new Series(data); } // Instantiates a new SeriesArray with the series. SeriesArray seriesArray = new SeriesArray(series); return(new DataFrame(seriesArray, headerArray, Indexers)); } }
public DataFrame Head(int count = 5) { string[] indexers = new string[count]; Series[] series = new Series[count]; for (int i = 0; i < count; i++) { object[] data = new object[Array.GetLength(1)]; for (int j = 0; j < Array.GetLength(1); j++) { data[j] = Array[i][j]; } series[i] = new Series(data); indexers[i] = Indexers[i]; } SeriesArray seriesArray = new SeriesArray(series); return(new DataFrame(seriesArray, Headers, indexers)); }
public DataFrame(SeriesArray series, string[] headers = null, string[] indexers = null) : base(series, headers, indexers) { }
public DataFrameBase(SeriesArray array, string[] headers = null, string[] indexers = null) { _array = array; _headers = headers ?? new string[0]; _indexers = indexers ?? Enumerable.Range(0, array.GetLength(0)).Select(s => s.ToString()).ToArray(); }