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

pd.to_timedelta(single_string) returns a Series, which ruins broadcasting. #5410

Closed
danielballan opened this issue Nov 1, 2013 · 7 comments · Fixed by #5415

Comments

@danielballan
Copy link
Contributor

commented Nov 1, 2013

Currently

In [10]: pd.to_timedelta(Series(['00:03:37', '00:05:05'])) - pd.to_timedelta('00:03:00')
Out[10]: 
0   00:00:37
1        NaT
dtype: timedelta64[ns]

because

In [13]: pd.to_timedelta('00:03:00')
Out[13]: 
0   00:03:00
dtype: timedelta64[ns]

More expected and consistent behavior, in my opinion, would be

In [12]: pd.to_timedelta(Series(['00:03:37', '00:05:05'])) - pd.to_timedelta('00:03:00')
Out[12]: 
0   00:00:37
1   00:02:05
dtype: timedelta64[ns]

If, instead, pd.to_timedelta('00:03:00') gave numpy.timedelta64(180000000000,'ns'), it would broadcast properly, as demonstrated by this work-around

In [12]: pd.to_timedelta(Series(['00:03:37', '00:05:05'])) - pd.to_timedelta('00:03:00')[0]
Out[12]: 
0   00:00:37
1   00:02:05
dtype: timedelta64[ns]

I have not regularly used timedeltas until now. Am I misjudging the expected usage?

Update, OK, one can also get the expected result by setting box=False. But box=False should be default for a scalar, no?

@jreback

This comment has been minimized.

Copy link
Contributor

commented Nov 1, 2013

this is correct (and on purpose); we don't have a timedelta scalar ATM (like Timestamp), so only option is to return a timedelta64, which works but is not friendly

@danielballan

This comment has been minimized.

Copy link
Contributor Author

commented Nov 1, 2013

Understood.

@jreback

This comment has been minimized.

Copy link
Contributor

commented Nov 1, 2013

this particular usage is easy to fix though (this is a_bug_ I guess in the timedelta subtraction) operation....marking for 0.14

@danielballan

This comment has been minimized.

Copy link
Contributor Author

commented Nov 1, 2013

So, until Timedelta scalars come along, worth submitting a PR that returns timedelta64 when input is not list-like?

@jreback

This comment has been minimized.

Copy link
Contributor

commented Nov 1, 2013

or...could just have a single element pd.to_timedelta(...) return the underlying value...

@danielballan

This comment has been minimized.

Copy link
Contributor Author

commented Nov 1, 2013

Yeah, I think we are saying the same thing. I'll make a PR.

@jreback

This comment has been minimized.

Copy link
Contributor

commented Nov 1, 2013

you can try it and see what breaks...it should work (I think)....and I think that is right actually...(to return a single-element as a timedelta64, bascially just don't box it), to_datetime DOES return a Timestamp for a single element

danielballan added a commit to danielballan/pandas that referenced this issue Nov 4, 2013

@jreback jreback closed this in #5415 Nov 4, 2013

jreback added a commit that referenced this issue Nov 4, 2013
Merge pull request #5415 from danielballan/to-timedelta-scalar
BUG: to_timedelta of a scalar returns a scalar, closes #5410.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.