Thursday, 20 April 2023

Type hinting Pandas DataFrame content and columns

I am writing a function that returns a Pandas DataFrame object. I would like to have some kind of a type hinting what columns this DataFrame contains, outside mere specification in the documentation, as I feel this will make it much easier for the end user to read the data.

Is there a way to type hint DataFrame content that different tools like Visual Studio Code and PyCharm would support, when editing Python files and when editing Jupyter Notebooks?

An example function:


def generate_data(bunch, of, inputs) -> pd.DataFrame:
      """Massages the input to a nice and easy DataFrame.

      :return:
           DataFrame with columns a(int), b(float), c(string), d(us dollars as float)
      """


from Type hinting Pandas DataFrame content and columns

No comments:

Post a Comment