Convert dataframe to 2D real array
Converts all numeric columns to a 2D real array. Only works if all columns are real or integer type. Integer values are converted to real.
@param df The data frame to convert @return 2D real array with shape (nrows, ncols)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(data_frame), | intent(in) | :: | df |
function df_to_array_real(df) result(array) type(data_frame), intent(in) :: df real(rk), dimension(:, :), allocatable :: array integer :: i, j, n_rows, n_cols real(rk), dimension(:), allocatable :: col_real integer(ik), dimension(:), allocatable :: col_int n_rows = df % nrows() n_cols = df % ncols() if (n_rows == 0 .or. n_cols == 0) then allocate (array(0, 0)) return end if ! Check that all columns are numeric do i = 1, n_cols if (df % dtype(i) /= REAL_NUM .and. df % dtype(i) /= INTEGER_NUM) then error stop "df_to_array_real: all columns must be real or integer type" end if end do allocate (array(n_rows, n_cols)) do i = 1, n_cols if (df % dtype(i) == REAL_NUM) then col_real = df_get_col_real(df, i) array(:, i) = col_real deallocate (col_real) else ! INTEGER_NUM col_int = df_get_col_integer(df, i) array(:, i) = real(col_int, rk) deallocate (col_int) end if end do end function df_to_array_real