mypy: error: No overload variant of "..." matches argument types "list[DataFrame]", "str" when using axis="rows"

41 Views Asked by At

I was running

pd.concat(dfs, axis="rows")
pd.median(dfs, axis="rows")

basically any function from pandas which can accept axis and mypy will raise:

error: No overload variant of "concat" matches argument types "list[DataFrame]", "str"  [call-overload]
note: Possible overload variants:

... # Long list of possibilies

How to remove this mypy error on this valid code ?

2

There are 2 best solutions below

0
Oily On

Pandas does not define for the typing overloads for the keyword "rows": For example for the concat method, only the following are allowed.

axis: Literal[0, "index"] = ...,
axis: Literal[1, "columns"],


Using "index" instead of "rows" fix the problem.

Just a note:

pandas defines a Axis type in which "rows" would be a valid option, so this is most likely a bug on their side:

Axis = Union[AxisInt, Literal["index", "columns", "rows"]]
0
InSync On

I believe the types are coming not from Pandas itself, but from pandas-stubs:

AxisInt: TypeAlias = int
AxisIndex: TypeAlias = Literal["index", 0]
AxisColumn: TypeAlias = Literal["columns", 1]
Axis: TypeAlias = AxisIndex | AxisColumn

There's also this disclaimer at the top of the project's README:

The stubs are likely incomplete in terms of covering the published API of pandas. NOTE: The current 2.0.x releases of pandas-stubs do not support all of the new features of pandas 2.0.

The best fixes for this are probably:

  • Either wait until the types are corrected, or
  • Fix it yourself by contributing to the project.

You can also redefine anything you need in your own code only, but that perhaps would not be as fruitful in the long term.