df_filter_rows_logical Function

public function df_filter_rows_logical(df, logical_col_index) result(new_df)

Filter rows based on a logical column

Creates a new data frame containing only rows where the specified logical column is .true.

@param[in] df The source data frame @param[in] logical_col_index Index of the logical column to use for filtering @return new_df New data frame with filtered rows

Arguments

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

Return Value type(data_frame)


Source Code

    function df_filter_rows_logical(df, logical_col_index) result(new_df)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: logical_col_index
        type(data_frame) :: new_df

        logical, dimension(:), allocatable :: mask
        integer, allocatable :: selected_rows(:)
        integer :: i, count_true, idx
        type(column) :: col

        col = df % get_data_col(logical_col_index)

        if (col % get_type() /= LOGICAL_NUM) then
            error stop "Column must be logical type for df_filter_rows_logical"
        end if

        mask = col % getl()
        count_true = count(mask)

        if (count_true == 0) then
            call new_df % new(df % get_max_char_len())
            return
        end if

        ! Get indices of true values
        allocate (selected_rows(count_true))
        idx = 1
        do i = 1, size(mask)
            if (mask(i)) then
                selected_rows(idx) = i
                idx = idx + 1
            end if
        end do

        call new_df % new(df % get_max_char_len())

        ! Copy selected rows for each column
        do i = 1, df % ncols()
            call copy_filtered_column(df, new_df, i, selected_rows)
        end do

        deallocate (selected_rows)
    end function df_filter_rows_logical