1

I got the following error running my code :

    import pandas as pd
    data = pd.read_csv('file.csv')
    data['time'] = pd.to_datetime(data['UNIX time'],unit='s')
    data['time_min'] = (data['time'] - data['time'].min()).astype(int)

"cannot astype a timedelta from [timedelta64[ns]] to [int32]"

vbx8977
  • 57
  • 1
  • 4

1 Answers1

2

You can extract the day from the result using Timedelta.days, which is saved as an int:

sol = (data['time'] - data['time'].min()).apply(lambda x: x.days)

print(sol)
0     0
1     0
2    14
3    14

print(sol.apply(type))
0    <class 'int'>
1    <class 'int'>
2    <class 'int'>
3    <class 'int'>
yatu
  • 86,083
  • 12
  • 84
  • 139