Skip to content

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

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
danielballan opened this issue Nov 1, 2013 · 7 comments · Fixed by #5415
Closed

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

danielballan opened this issue Nov 1, 2013 · 7 comments · Fixed by #5415
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@danielballan
Copy link
Contributor

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
Copy link
Contributor

jreback 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
Copy link
Contributor Author

Understood.

@jreback
Copy link
Contributor

jreback 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
Copy link
Contributor Author

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

@jreback
Copy link
Contributor

jreback commented Nov 1, 2013

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

@danielballan
Copy link
Contributor Author

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

@jreback
Copy link
Contributor

jreback 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 added a commit that referenced this issue Nov 4, 2013
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
Labels
Bug Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants