1

If I create a negative Timedelta for e.g. 0.5 hours, the internal representation looks as follow:

In [2]: pd.Timedelta('-0.5h')
Out[2]: Timedelta('-1 days +23:30:00')

How can I get back a (str) representation of this Timedelta in the form -00:30?

I want to display these deltas and requiring the user to calculate the expression -1 day + something is a bit award.

languitar
  • 6,554
  • 2
  • 37
  • 62

4 Answers4

1

I can't add comment to you so adding it here. Don't know if this helps but I think you can use python humanize.

import humanize as hm
hm.naturaltime((pd.Timedelta('-0.5h')))

Out:

'30 minutes from now'
  • That would be an interesting thing, however, the "some time ago" metaphor doesn't work in this case as it is just an offset that I am representing. – languitar Mar 22 '17 at 13:18
0

Ok, I will live with a hack going trough a date:

sign = ''
date = pd.to_datetime('today')
if delta.total_seconds() < 0:
    sign = '-'
    date = date - delta
else:
    date = date + delta

print '{}{:%H:%M}'.format(sign, date.to_pydatetime())
languitar
  • 6,554
  • 2
  • 37
  • 62
0

You can use the components of a Pandas timedelta

import pandas as pd
t = pd.Timedelta('-0.5h')
print t.components

>> Components(days=-1L, hours=23L, minutes=30L, seconds=0L, milliseconds=0L, microseconds=0L, nanoseconds=0L)

You can access each component with

print t.components.days
>> -1
print t.components.hours
>> 23
print t.components.minutes
>> 30

The rest is then formatting.

source


This is a total hack that won't work for Series data, but....

import pandas as pd
import numpy as np
t = pd.Timedelta('-0.5h').components
mins =  t.days*24*60 + t.hours*60 + t.minutes

print str(np.sign(mins))[0]+str(divmod(abs(mins), 60)[0]).zfill(2)+':'+str(divmod(abs(mins), 60)[1]).zfill(2)

>> -00:30
philshem
  • 24,761
  • 8
  • 61
  • 127
0

I was looking for something similar (see https://github.com/pandas-dev/pandas/issues/17232 )

I'm not sure if it will be implemented in Pandas, so here is a workaround

import pandas as pd


def timedelta2str(td, display_plus=False, format=None):
    """

    Parameters
    ----------
    format : None|all|even_day|sub_day|long
    Returns
    -------
    converted : string of a Timedelta

    >>> td = pd.Timedelta('00:00:00.000')
    >>> timedelta2str(td)
    '0 days'

    >>> td = pd.Timedelta('00:01:29.123')
    >>> timedelta2str(td, display_plus=True, format='sub_day')
    '+ 00:01:29.123000'

    >>> td = pd.Timedelta('-00:01:29.123')
    >>> timedelta2str(td, display_plus=True, format='sub_day')
    '- 00:01:29.123000'

    """
    td_zero = pd.Timedelta(0)
    sign_sep = ' '
    if td >= td_zero:
        s = td._repr_base(format=format)
        if display_plus:
            s = "+" + sign_sep + s
        return s
    else:
        s = timedelta2str(-td, display_plus=False, format=format)
        s = "-" + sign_sep + s
        return s


if __name__ == "__main__":
    import doctest
    doctest.testmod()
scls
  • 16,591
  • 10
  • 44
  • 55