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

0

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)
function pyspark python
2021-11-23 22:05:07
1

0

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.

2021-11-23 22:22:43

Great, worked exactly how needed; thanks!
JSingh1996

You're welcome!
Sakshi Sharma

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................