-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Timedelta * Series -> TypeError #8813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note that reversing the order does work: pd.Series(range(10))*pd.to_timedelta(10, unit='D')
Out[158]:
0 0 days
1 10 days
2 20 days
3 30 days
4 40 days
5 50 days
6 60 days
7 70 days
8 80 days
9 90 days
dtype: timedelta64[ns] |
I recall that their was no easy way to fix this |
@jreback I can also take a look, I've written a lot of classes that do arithmetic at this point. I'm pretty sure this is fixable. |
oh, this is a buggie. It is intercepted in cython. I don't allow any multiplication of a scalar timedelta with other objects (nothing makes sense). Except for the other way around (e.g. Series/Timedelta) if its an integer Series/Index it makes sense to allow multiple and does (so this is the bug). I think division is right, but maybe check that too. E.g. you can only divide a Timedelta (object/series/index) by a Timedelta object (though I think allow integer division too). |
This doesn't feel right. I mean, set of possible multiplicand types is inherently open, you cannot realistically say that nothing in the infinite set of possibilities makes sense. Which is why python requires |
sorry, misspoke, numpy array/index/series of integers works. (other return a TypeError). could certainly be NotImplemetedError though). @immerrr not much actually makes sense. Maybe you have something that does that was missed? |
First, it's not
Restricting the set of operand types in python's world of duck typing doesn't make sense to me. For the very least think of all the possible types of ndarray-like containers that don't pass the isinstance(ndarray) check, e.g. scipy sparse matrices or |
@immerrr
|
Could you rephrase that? I suppose you mean that you'll have to check for
It is exactly my point that it won't be able to do that. |
ok, i'll rephrase. What I mean is for 99% of the users a If xray wants to implement then they can do it (though I suspect since its much easier to simply push this upstream, e.g. to get @shoyer to convince us that its a valid operation and support it mainstream in pandas). E.g. this is much easier IMHO that e.g. pushing upstream to numpy (TL;DR) the other discussions that we have had. |
In [32]: d = datetime.datetime.now()
In [33]: x = 1
In [34]: d.__add__(x)
Out[34]: NotImplemented
In [35]: x.__add__(d)
Out[35]: NotImplemented
In [36]: d + x
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-aa9ad8fe26a5> in <module>()
----> 1 d + x
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int' Surprisingly, though, strings do raise a TypeError. BUT they also call In [37]: 'foobar'.__add__(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-6c317551e6f0> in <module>()
----> 1 'foobar'.__add__(x)
TypeError: cannot concatenate 'str' and 'int' objects
In [38]: class Foo(object):
....: def __radd__(self, other):
....: return 'xyzzy'
....:
In [39]: 'foobar'.__add__(Foo())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-39-d607980902de> in <module>()
----> 1 'foobar'.__add__(Foo())
TypeError: cannot concatenate 'str' and 'Foo' objects
In [40]: 'foobar' + Foo()
Out[40]: 'xyzzy'
|
I guess i disagree with this spec a bit. pandas just tries to do the right thing and not raise too many |
I'm trying to point out that python allows the other object to have a say in what's allowed or not, too. Raising TypeErrors doesn't. You don't agree to play by that rules? Fine, I rest my case. |
@immerrr are we really disagreeing with whether to raise |
I can dive into this more later, but in brief, I agree with @immerrr that the appropriate thing to do with an invalid operation is always to always return NotImplemented rather than to explicitly raise TypeError. That said, Timedelta should use duck typing for dtype == int rather than isinstance checks. On Fri, Nov 14, 2014 at 8:24 AM, jreback [email protected] wrote:
|
Kind of, yes :) I'm trying to show that the obvious solution (allow series/index only) violates open-closed principle and may force you (us) to get back to this issue in future (to add support for potentially infinite number of types). This particular case is, of course, trivial to fix in either way. However the idea of allowing the second operand to handle the operation really applies to all of the binary operators and may be beneficial for other places in the code, too. |
I encountered this today when rerunning a script that worked prior to the 0.15 arrival of Timedelta:
@jreback I think there might be another issue for this already?
The text was updated successfully, but these errors were encountered: