0

I have a custom column in my dataframe that takes the difference between two other columns (start time and end time). This column returns number of days as timedelta values. When I try to use this data in statistical functions like scatter plotting or pivot tables I find that the data is not useable. Is there any way to convert this data to integers?

I tried something like this to convert the data:

    df['integer'] = []

    for i in df['timedelta']:
        df['integer'].append(i.days)

    print(df['integer']

However, this returned the following error: ValueError: Length of values does not match length of index. Is this because I can't iterate over null values? Is there someway to get around this? Appreciate any clarification.

  • https://stackoverflow.com/questions/40992976/python-convert-datetime-column-into-seconds Maybe this thread could help? Or the timestamp functions in the docs: https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.html – jrbergen May 18 '21 at 22:15
  • for a [timedelta](https://pandas.pydata.org/docs/reference/api/pandas.Timedelta.html) column you can access attributes like days or total seconds via the `dt` accessor, e.g. like `df['timedelta'].dt.total_seconds()` – FObersteiner May 19 '21 at 09:18

0 Answers0