OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python function annotation with tuple and N pd DataFrames?

  • Thread starter Thread starter lazarea
  • Start date Start date
L

lazarea

Guest
I have a function that returns N=4 pandas DataFrames. I can clearly specify all types for the function like so:

Code:
import pandas as pd
from typing import Tuple 

def preprocess(df: pd.DataFrame) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
    pass

but it's a bit verbose and I would prefer a more pythonic annotation in case I have to return a tuple with generally N elements. Since the return types are specified as a tuple and for tuple initialization there is a convenient Python feature to replace long initializations like (1,1,1,1,1,1) by simply writing (1,)*6, I thought a similar approach would possibly work here as well:

Code:
import pandas as pd
from typing import Tuple

N = 4 

def preprocess(df: pd.DataFrame) -> Tuple[pd.DataFrame] * N:
    pass

But this throws an error:

Code:
unsupported operand type(s) for *: '_GenericAlias' and 'int'

How can I annotate my function accordingly?

Edit: My goal is not to dynamically set the annotations. I'm only curious if there exists a pythonic way to declare any-length tuples as a return value, instead of having to write long annotations like Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]. Imagine if a function was to return N=20 objects in a tuple, for instance.
<p>I have a function that returns <code>N=4</code> pandas DataFrames. I can clearly specify all types for the function like so:</p>
<pre><code>import pandas as pd
from typing import Tuple

def preprocess(df: pd.DataFrame) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
pass
</code></pre>
<p>but it's a bit verbose and I would prefer a more pythonic annotation in case I have to return a tuple with generally <code>N</code> elements. Since the return types are specified as a tuple and for tuple initialization there is a convenient Python feature to replace long initializations like <code>(1,1,1,1,1,1)</code> by simply writing <code>(1,)*6</code>, I thought a similar approach would possibly work here as well:</p>
<pre><code>import pandas as pd
from typing import Tuple

N = 4

def preprocess(df: pd.DataFrame) -> Tuple[pd.DataFrame] * N:
pass
</code></pre>
<p>But this throws an error:</p>
<pre><code>unsupported operand type(s) for *: '_GenericAlias' and 'int'
</code></pre>
<p>How can I annotate my function accordingly?</p>
<p><strong>Edit:</strong> My goal is not to dynamically set the annotations. I'm only curious if there exists a pythonic way to declare any-length tuples as a return value, instead of having to write long annotations like <code>Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]</code>. Imagine if a function was to return <code>N=20</code> objects in a tuple, for instance.</p>
 
Top