Something like:
1 + pd.Series([1])
will yield error: Unsupported operand types for + ("int" and "Series")
I assume because our arithmetic ops are added at runtime and not statically. Would be nice to support this somehow though
Comment From: jorisvandenbossche
@WillAyd How can you ever know this "statically" ?
To know if two types can be added up like that, you could need to know if Series.__add__
returns NotImplemented
for that type or not, which always seems something run-time dependent?
Comment From: WillAyd
It’s not the definition as much as the declaration. So something like add would have to exist statically for my py to recognize. right now we use factory methods at runtime to add that
Comment From: jbrockmendel
This comes up often enough i wouldn't mind changing our runtime-pinning to something like:
def __add__(self, other)
return factory_func(self, other, operator.add)
Comment From: WillAyd
I don't mind adding these statically. I think talked with @jreback in the past though who may have disagreed so pinging for his input, otherwise would take a PR
Comment From: jreback
yeah ok with doing more static things as it will help with typing
Comment From: jorisvandenbossche
And apart from typing, it will also result in easier to understand code, I think
Comment From: simonjayhawkins
the last part of https://github.com/pandas-dev/pandas/issues/34869#issuecomment-646539490 is related
yeah ok with doing more static things as it will help with typing
+1
And apart from typing, it will also result in easier to understand code, I think
+1
have started down this route on a few commonly used dynamically added methods, to avoid ignores.
https://github.com/pandas-dev/pandas/blob/6648439ecc44baf9a783a19ff1d6226347c76dd0/pandas/core/indexes/datetimes.py#L222-L240
https://github.com/pandas-dev/pandas/blob/a0e0571ba4d843b0b462f1894d06a81b77925af2/pandas/core/indexes/period.py#L150-L158
when I did the first one I thought it was clearer to include the actual code for clarity, but after adding a couple more, it does look more like boilerplate and @jbrockmendel solution may be better https://github.com/pandas-dev/pandas/issues/31160#issuecomment-699193016 Will look again.
Comment From: jbrockmendel
Our arithmetic ops are no longer added dynamically. Does that solve this issue?