0

I've had problems converting timedeltas to an int (days).

df.FIN = pd.to_datetime(df.FIN, errors = 'coerce')
df.START = pd.to_datetime(df.START, errors = 'coerce')
df["FIN-START"] = df["FIN"] - df["START"]

* input: *

0    0 days 00:00:00
1   89 days 19:41:05
2    0 days 00:00:00
3    0 days 00:00:00
4    0 days 00:00:00
...
Name: FIN-START, dtype: timedelta64[ns]

* error-causing line: *

df["days"] = df["FIN-START"].apply(lambda td: td.days)

* error: *

AttributeError: 'numpy.timedelta64' object has no attribute 'days'
Jason Brown
  • 284
  • 6
  • 19
  • I think the page here answers your question: http://stackoverflow.com/questions/18215317/extracting-days-from-a-numpy-timedelta64-value – mitoRibo Jul 19 '16 at 20:41
  • thanks - that worked. I had previously tried a different portion of that answer. I'm unclear why td.days doesn't work but ok with the solution. – Jason Brown Jul 19 '16 at 21:04

1 Answers1

0

for each of the elements in the Series, convert the timedelta to timedelta[D] in the form of, for example, 94 days. Then, just take it astype int.

df["days"] = df["FIN-START"].apply(lambda td: td.astype('timedelta64[D]').astype(int))
Jason Brown
  • 284
  • 6
  • 19