Skip to content

API: timedelta64[ns] elements operate with timedelta/offsets #5436

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

Closed
jreback opened this issue Nov 4, 2013 · 0 comments · Fixed by #8884
Closed

API: timedelta64[ns] elements operate with timedelta/offsets #5436

jreback opened this issue Nov 4, 2013 · 0 comments · Fixed by #8884
Labels
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Nov 4, 2013

have to return timedelta64[ns] scalars

type: timedelta64[ns]

In [23]: s = pd.to_timedelta(np.arange(5),unit='d')

In [24]: s
Out[24]: 
0           00:00:00
1   1 days, 00:00:00
2   2 days, 00:00:00
3   3 days, 00:00:00
4   4 days, 00:00:00
dtype: timedelta64[ns]

In [25]: s + timedelta(hours=1)
Out[25]: 
0           01:00:00
1   1 days, 01:00:00
2   2 days, 01:00:00
3   3 days, 01:00:00
4   4 days, 01:00:00
dtype: timedelta64[ns]

In [26]: s + pd.offsets.Hour(1)
Out[26]: 
0           01:00:00
1   1 days, 01:00:00
2   2 days, 01:00:00
3   3 days, 01:00:00
4   4 days, 01:00:00
dtype: timedelta64[ns]

In [27]: s.iloc[2] + pd.offsets.Hour(1)
TypeError: ufunc add cannot use operands with types dtype('<m8[ns]') and dtype('O')

In [29]: s.iloc[2] + timedelta(hours=1)
TypeError: ufunc add cannot use operands with types dtype('<m8[ns]') and dtype('O')

This works (wrapping in a np.timedeltat64

In [31]: s.iloc[2] += np.timedelta64(timedelta(hours=1))

In [32]: s
Out[32]: 
0           00:00:00
1   1 days, 00:00:00
2   2 days, 01:00:00
3   3 days, 00:00:00
4   4 days, 00:00:00
dtype: timedelta64[ns]

Reevaluate whether summary ops should just return the np.timdelta64 scalar
(not as pretty though)

In [33]: s.mean()
Out[33]: 
0   2 days, 00:12:00
dtype: timedelta64[ns]

In [34]: s.mean().iloc[0]
Out[34]: numpy.timedelta64(173520000000000,'ns')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant