Home > Software engineering >  What is the correct way to add a function annotation when the def input is a pandas dataframe or a c
What is the correct way to add a function annotation when the def input is a pandas dataframe or a c

Time:09-06

I am creating a class to generate LaTeX tables automatically using pylatex and within it, I have a class function that calculates the number of pandas DataFrame columns (x) and outputs a string with x amount of 'c''s. The x amount of 'c''s will eventually go into LaTeX code similar to this:

\begin{tabularx}{\textwidth}{@{}cccccccccc@{}}

Whilst making this code, I have been trying to follow the PEP guidelines (specifically PEP 8 for style, 257 for docstrings and 3107 for function annotations) and have run into a wall over how to declare a pandas DataFrame for the def function annotation.

For example:

def _columns_calculator(self, df) -> str:
    """ Returns LaTeX table alignment string (centered by default, 'c')."""
    columns = len(df.columns) - 1        # -1 to account for 0 index column
    tabularX_columns = 'c' * columns
    return tabularX_columns

What type do I declare df to be, if any? When using type on a pandas DataFrame it returned the class type <class 'pandas.core.frame.DataFrame'>. I have other classes within pylatex that are passed to def functions too (namely doc = Document(), the base pylatex document class that the other functions need to add too). How in general should I annotate a class in the function definition input (df: class is not correct naturally but shows what the answer this question seeks) and specifically a pandas DataFrame?

There could be other functions not within a class that have a DataFrame as an input so this question is not specific to solely classes. I searched on this site and through the PEP 3107 and could not see an answer, apologies if this has been asked before, I would appreciate a pointer to the original if this is a duplicate or trivial.

CodePudding user response:

This is how I do it:

import pandas as pd


def _columns_calculator(self, df: pd.DataFrame) -> str:
    """ Returns LaTeX table alignment string (centered by default, 'c')."""
    columns = len(df.columns) - 1  # -1 to account for 0 index column
    tabularX_columns = 'c' * columns
    return tabularX_columns
  • Related