Skip to content
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

np.ndarray[object] - Timedelta raises #21980

Closed
jbrockmendel opened this issue Jul 19, 2018 · 10 comments · Fixed by #22054
Closed

np.ndarray[object] - Timedelta raises #21980

jbrockmendel opened this issue Jul 19, 2018 · 10 comments · Fixed by #22054
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@jbrockmendel
Copy link
Member

arr = np.array([pd.Timestamp.now(), pd.Timedelta('2D')])

>>> arr - pd.Timedelta('1D')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'Timedelta'

It should attempt to operate element-wise.

@jbrockmendel jbrockmendel added Bug Timedelta Timedelta data type labels Jul 20, 2018
@jorisvandenbossche
Copy link
Member

I suppose this is because of

elif hasattr(other, 'dtype'):
# nd-array like
if other.dtype.kind not in ['m', 'M']:
# raise rathering than letting numpy return wrong answer
return NotImplemented
return op(self.to_timedelta64(), other)

Any idea what the "wrong" answer would be? (with timedelta.timedelta instead of Timedelta that seems to work just fine, so I assume with Timedelta it will be the same)

@jbrockmendel
Copy link
Member Author

No idea what the wrong answer would be. This should be easy to fix; if no one else picks it up I'll take care of it once the current PR queue settles down.

@jorisvandenbossche
Copy link
Member

Yes, PR with a fix is certainly welcome I think

@illegalnumbers
Copy link
Contributor

Is this still an issue? I wasn't able to repro from master.

@jbrockmendel
Copy link
Member Author

Is this still an issue? I wasn't able to repro from master.

What platform etc? I still get it on OSX in both py27 and py37.

@illegalnumbers
Copy link
Contributor

OSX 10.11.6 with Python 3.6. I just pulled up a REPL and imported pandas from a compile I did yesterday from master and didn't get an exception from the example code posted. Specifically Python 3.6.6 (default, Jul 23 2018, 11:08:18) [GCC 4.2.1 Compatible Clang 6.0.0 (tags/RELEASE_600/final)] on darwin

@illegalnumbers
Copy link
Contributor

illegalnumbers commented Jul 24, 2018

I also didn't see the issue from the latest install from pip either. Both times I just got

>>> arr = np.array([pd.Timestamp.now(), pd.Timedelta('2D')])
>>> arr
array([Timestamp('2018-07-24 10:49:41.898067'),
       Timedelta('2 days 00:00:00')], dtype=object)

@jbrockmendel
Copy link
Member Author

Did you try subtracting a Timedelta from arr?

@illegalnumbers
Copy link
Contributor

Ah! 🤦‍♂️ yea I missed that part in the example. I repro'd the bug with that on master and latest pip. So with this then how should I go about the fix? It's not operating element wise on the array because the timedeltas.pyx isn't returning that it is a timedelta correctly? or...?

@jbrockmendel
Copy link
Member Author

how should I go about the fix?

Take a look at the code block Joris quoted above. At the moment that lets 'm' and 'M' dtypes through but stops everything else. The fix will involve letting 'o' dtypes through (and making sure they are handled correctly)

illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 25, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 25, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 25, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 25, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 26, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 26, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 26, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 26, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 26, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Jul 26, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Aug 17, 2018
@jreback jreback added this to the 0.24.0 milestone Aug 20, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Aug 23, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Sep 4, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Sep 5, 2018
illegalnumbers added a commit to illegalnumbers/pandas that referenced this issue Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants