df_tail Function

public function df_tail(df, n) result(tail_df)

Return last n rows as a new dataframe

@param[in] df The data frame to extract from @param[in] n Optional number of rows (default: 6) @return New data frame containing last n rows

Arguments

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

Return Value type(data_frame)


Source Code

    function df_tail(df, n) result(tail_df)
        type(data_frame), intent(in) :: df
        integer, intent(in), optional :: n
        type(data_frame) :: tail_df

        integer :: num_rows, start_row

        if (present(n)) then
            num_rows = min(n, df % nrows())
        else
            num_rows = min(6, df % nrows())  ! Default to 6 rows like pandas
        end if

        if (num_rows == 0 .or. df % nrows() == 0) then
            call tail_df % new(df % get_max_char_len())
            return
        end if

        start_row = df % nrows() - num_rows + 1
        tail_df = df_slice_rows(df, start_row, df % nrows())
    end function df_tail