Search code examples
pythondataframeinttimedelta

int too big to convert in python


I have a calculated 'duration' value from a simulated data. This is nothing but the 'hours' value calculated. I have written the below code:

Duration = 16571835.2920344
Dyn_ETA = pd.Timedelta(hours=Duration)
Dyn_ETA1 = Dyn_ETA.total_seconds()
hours = Dyn_ETA1/3600
result_Dyn = '{0:02.0f}:{1:02.0f}'.format(*divmod(hours * 60, 60))

but I am getting below error:

OverflowError: int too big to convert 
for
Dyn_ETA = pd.Timedelta(hours=Duration)

I wont get this 'duration' value in real time data. But as a safety measure, how can I resolve this?


Solution

  • You are using a wrong tool here. pd.Timedelta handles durations as an int64 values of nano seconds. This gives more or less a maximum of 5124095 hours (roughly 584 years).

    Your initial duration is close to twice that maximum and cannot be expressed that way. Full stop.

    But you do not need it:

    hour = Duration # taking the total_seconds() from the Timedelta in hour divided by 3600 is a no-op
    result_Dyn = '{0:02.0f}:{1:02.0f}'.format(*divmod(hours * 60, 60))