Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading