2

I am trying to convert timedelta to integer.

time = (pd.to_datetime(each_date2)-pd.to_datetime(each_date1))
pd.to_numeric(time, downcast='integer')

time has following value:

Timedelta('7 days 00:00:00')

I am getting following error on second line:

TypeError: Invalid object type at position 0

Any help would be greatly appreciated. Thanks in advance.

1
  • 1
    What's your expected output? Commented Jul 25, 2024 at 8:17

1 Answer 1

3

Scalar pandas.Timedelta objects

Assuming you want the time difference in days you can use the pandas.Timedelta.days attribute. For example:

# Create the data
import pandas as pd
each_date1 = '2024-07-17'
each_date2 = '2024-07-24'
time = pd.to_datetime(each_date2) - pd.to_datetime(each_date1)
print(time) # 7 days 00:00:00

Then it's simply:

time.days # 7

This is already an integer so there's no need to downcast. In fact, the downcast seems to have no effect. For example, if you want seconds:

pd.to_numeric(time.total_seconds(), downcast="integer") # 604800.0

The result is still a float. If you want an integer you can do:

int(time.total_seconds()) # 604800

Series of pandas.Timedelta objects

The same principle applies applies if each_date1 and each_date2 are pd.Series. However, you need to use pandas.Series.dt to access the datetime properties of the series. For example:

each_date1 = pd.Series(['2023-07-01 12:00:00', '2023-07-05 08:00:00', '2023-07-10 18:00:00'])
each_date2 = pd.Series(['2023-07-08 12:00:00', '2023-07-10 10:00:00', '2023-07-15 20:00:00'])
time = pd.to_datetime(each_date2) - pd.to_datetime(each_date1)
print(time)
# 0   7 days 00:00:00
# 1   5 days 02:00:00
# 2   5 days 02:00:00
# dtype: timedelta64[ns]

To access the days:

time.dt.days
# 0    7
# 1    5
# 2    5
# dtype: int64

To calculate the seconds and cast to integer:

time.dt.total_seconds().astype(int)
# 0    604800
# 1    439200
# 2    439200
# dtype: int64
Sign up to request clarification or add additional context in comments.

1 Comment

If you want to skip the floating point arithmetic, you can simply use the .value property of a timedelta object, or .astype(int) method on a timedelta series. Both give you signed integers representing nanoseconds. Divide appropriately to get the desired unit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.