Skip to content

Better display of negative Timedelta #17232

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

Open
s-celles opened this issue Aug 11, 2017 · 4 comments
Open

Better display of negative Timedelta #17232

s-celles opened this issue Aug 11, 2017 · 4 comments
Labels
Enhancement Output-Formatting __repr__ of pandas objects, to_string Timedelta Timedelta data type

Comments

@s-celles
Copy link
Contributor

s-celles commented Aug 11, 2017

Code Sample, a copy-pastable example if possible

In [2]: pd.to_timedelta(0, unit='ms') - pd.to_timedelta(123, unit='ms')
Out[2]: Timedelta('-1 days +23:59:59.877000')

Problem description

Timedelta('-1 days +23:59:59.877000') is not a very clear display of a negative Timedelta

It could be better (for many usages) to display negative Timedelta like Timedelta('0 days -00:00:00.123000') or Timedelta('-0 days 00:00:00.123000')

@gfyoung gfyoung added the Timedelta Timedelta data type label Aug 11, 2017
@chris-b1
Copy link
Contributor

xref #15633 We're following python's (confusing) lead here, but I suppose we could break on the repr.

In [2]: import datetime

In [3]: datetime.timedelta() - datetime.timedelta(milliseconds=123)
Out[3]: datetime.timedelta(-1, 86399, 877000)

In [4]: str(datetime.timedelta() - datetime.timedelta(milliseconds=123))
Out[4]: '-1 day, 23:59:59.877000'

@jreback
Copy link
Contributor

jreback commented Aug 12, 2017

I suppose, though not really sure its worth the inconsistency here.

@jreback jreback added Output-Formatting __repr__ of pandas objects, to_string Difficulty Intermediate labels Aug 12, 2017
@jreback jreback added this to the Next Major Release milestone Aug 12, 2017
@Sup3rGeo
Copy link
Contributor

I would say it is really worth it, as the current representation for negative number is really really confusing.

But about breaking consistency, I guess the string representation for negative timedeltas was not meant to have any kind of usability in mind. I found the following discussion (basically all I could found about it):

https://lists.gt.net/python/dev/1129944

Does anyone remember the rationale for this behaviour?

I don't recall any better rationale than what I wrote in the docs: "String
representations of timedelta objects are normalized similarly to their
internal representation."

I guess it would be better to have python's default implementation of the string changed (instead of doing it in an ad-hoc fashion for every different project, including pandas), but as this won't be the case, I believe it is a good idea to add to pandas, because the way it is is very non intuitive.
I can't think of a case where anyone would prefer it when dealing with negative timedeltas.

@veenstrajelmer
Copy link

veenstrajelmer commented Jul 3, 2024

I realize this is an old issue, but this would be amazing. I ran into this issue when plotting timedelta's where I found the negative values to be displayed in a non intuitive form. I also encounter this in TimedeltaIndex and plain pd.Timedelta.

For the axis labels, I have slighly adjusted TimeSeries_TimedeltaFormatter to display timedelta axis labels in a more intuitive format: https://github.com/Deltares-research/kenmerkendewaarden/blob/ff47a2c3f2e575726cb5ef696c6ef266de1ed776/kenmerkendewaarden/utils.py#L15-L48.
I am posting this here just to say that this function might also require an update.

@mroeschke @jbrockmendel if a PR would be contributed, would it still be considered to be added? I now have a workaround in my own code that I would like to discontinue. The workaround only works for plotting, but I guess it also should work for prints of dataframes/series, not sure how yet. Some hint would be helpful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Output-Formatting __repr__ of pandas objects, to_string Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

8 participants