Reputation: 5531
I have a column of type datetime64 , which already keep in days
In [88]: print df.days.head()
0 756 days
1 262 days
2 72 days
3 173 days
4 12 days
Name: days, dtype: timedelta64[ns]
I want to cast it as int64, I do the following:
df['days'] = df['days'].astype(int)/(24*3600*10**9)
this works, but curious is there a faster/better way of doing it?
Upvotes: 4
Views: 5011
Reputation: 128948
This is frequency conversion
In [3]: s = Series(pd.to_timedelta(['756 days','2 days', '3 days 5 min']))
In [4]: s
Out[4]:
0 756 days 00:00:00
1 2 days 00:00:00
2 3 days 00:05:00
dtype: timedelta64[ns]
In [5]: s.astype('timedelta64[D]')
Out[5]:
0 756
1 2
2 3
dtype: float64
In [6]: s / np.timedelta64(1,'D')
Out[6]:
0 756.000000
1 2.000000
2 3.003472
dtype: float64
Upvotes: 3