df_slice_rows Function

private function df_slice_rows(df, start_row, end_row) result(new_df)

Internal: Slice rows to create a new data frame

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(in) :: df
integer, intent(in) :: start_row
integer, intent(in) :: end_row

Return Value type(data_frame)


Source Code

    function df_slice_rows(df, start_row, end_row) result(new_df)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: start_row, end_row
        type(data_frame) :: new_df

        integer :: i, j, new_size
        real(rk), allocatable :: real_slice(:)
        integer(ik), allocatable :: int_slice(:)
        logical, allocatable :: logical_slice(:)
        character(len=:), allocatable :: char_slice(:)
        complex(rk), allocatable :: complex_slice(:)
        type(column) :: col

        if (start_row < 1 .or. end_row > df % nrows() .or. start_row > end_row) then
            error stop "invalid row range for slicing"
        end if

        new_size = end_row - start_row + 1
        call new_df % new(df % get_max_char_len())

        do i = 1, df % ncols()
            col = df % get_data_col(i)

            select case (col % get_type())
            case (REAL_NUM)
                allocate (real_slice(new_size))
                do j = 1, new_size
                    real_slice(j) = col % getr(start_row + j - 1)
                end do
                if (df % get_with_headers()) then
                    call df_append_real(new_df, real_slice, df % header(i))
                else
                    call df_append_real(new_df, real_slice)
                end if
                deallocate (real_slice)
            case (INTEGER_NUM)
                allocate (int_slice(new_size))
                do j = 1, new_size
                    int_slice(j) = col % geti(start_row + j - 1)
                end do
                if (df % get_with_headers()) then
                    call df_append_integer(new_df, int_slice, df % header(i))
                else
                    call df_append_integer(new_df, int_slice)
                end if
                deallocate (int_slice)
            case (LOGICAL_NUM)
                allocate (logical_slice(new_size))
                do j = 1, new_size
                    logical_slice(j) = col % getl(start_row + j - 1)
                end do
                if (df % get_with_headers()) then
                    call df_append_logical(new_df, logical_slice, df % header(i))
                else
                    call df_append_logical(new_df, logical_slice)
                end if
                deallocate (logical_slice)
            case (CHARACTER_NUM)
                allocate (character(len=len(col % getch(1))) :: char_slice(new_size))
                do j = 1, new_size
                    char_slice(j) = col % getch(start_row + j - 1)
                end do
                if (df % get_with_headers()) then
                    call df_append_character(new_df, char_slice, df % header(i))
                else
                    call df_append_character(new_df, char_slice)
                end if
                deallocate (char_slice)
            case (COMPLEX_NUM)
                allocate (complex_slice(new_size))
                do j = 1, new_size
                    complex_slice(j) = col % getc(start_row + j - 1)
                end do
                if (df % get_with_headers()) then
                    call df_append_complex(new_df, complex_slice, df % header(i))
                else
                    call df_append_complex(new_df, complex_slice)
                end if
                deallocate (complex_slice)
            end select
        end do
    end function df_slice_rows