df_drop_column Subroutine

public subroutine df_drop_column(df, col_index)

Drop a column from the data frame

Removes the specified column and all its data from the data frame

@param[in,out] df The data frame to modify @param[in] col_index Index of the column to drop

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(inout) :: df
integer, intent(in) :: col_index

Source Code

    subroutine df_drop_column(df, col_index)
        type(data_frame), intent(inout) :: df
        integer, intent(in) :: col_index

        integer :: i
        type(data_frame) :: temp_df

        if (col_index < 1 .or. col_index > df % ncols()) error stop "column index out of range"

        ! Create a new data frame with all columns except the one to drop
        call temp_df % new(df % get_max_char_len())

        do i = 1, df % ncols()
            if (i /= col_index) then
                select case (df % dtype(i))
                case (REAL_NUM)
                    if (df % get_with_headers()) then
                        call df_append_real(temp_df, df_get_col_real(df, i), df % header(i))
                    else
                        call df_append_real(temp_df, df_get_col_real(df, i))
                    end if
                case (INTEGER_NUM)
                    if (df % get_with_headers()) then
                        call df_append_integer(temp_df, df_get_col_integer(df, i), df % header(i))
                    else
                        call df_append_integer(temp_df, df_get_col_integer(df, i))
                    end if
                case (LOGICAL_NUM)
                    if (df % get_with_headers()) then
                        call df_append_logical(temp_df, df_get_col_logical(df, i), df % header(i))
                    else
                        call df_append_logical(temp_df, df_get_col_logical(df, i))
                    end if
                case (CHARACTER_NUM)
                    if (df % get_with_headers()) then
                        call df_append_character(temp_df, df_get_col_character(df, i), df % header(i))
                    else
                        call df_append_character(temp_df, df_get_col_character(df, i))
                    end if
                case (COMPLEX_NUM)
                    if (df % get_with_headers()) then
                        call df_append_complex(temp_df, df_get_col_complex(df, i), df % header(i))
                    else
                        call df_append_complex(temp_df, df_get_col_complex(df, i))
                    end if
                end select
            end if
        end do

        ! Destroy original and move temp_df data to df
        call df % destroy()
        df = temp_df
    end subroutine df_drop_column