PythonException: 'TypeError: unsupported operand type(s) for divmod(): 'NoneType' and 'int''

I have this function which converts seconds to dd:hh:mm:ss (string) – however, when there is a null instance from the input column, I receive the error PythonException: ‘TypeError: unsupported operand type(s) for divmod(): ‘NoneType’ and ‘int”.

is there a fix that can be put inside the function, below –

  def to_hms(s):
 m, s = divmod(s, 60)
 h, m = divmod(m, 60)
 d, h = divmod(h, 24)
 return '{}:{:0>2}:{:0>2}:{:0>2}'.format(d, h, m, s)

>Solution :

Maybe this can help:

def to_hms(s):
    if s:
        m, s = divmod(s, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 24)
        return '{}:{:0>2}:{:0>2}:{:0>2}'.format(d, h, m, s)
    return '0:00:00:00'

This one will return empty value if it does not find value for s.

Leave a Reply