Python: Convert Timedelta to Int in a Dataframe

Convert a timedelta to int

if u want to convert only days in int then use new_date.days

How to convert timedelta to integer in pandas?

This seems to work:

# If the day counts are actual integers:
m = ~df.Time.apply(lambda x: isinstance(x, int))

# OR, in case the day counts are strings:
m = ~df.Time.str.isdigit()

df.loc[m, 'Time'] = df.Time[m].apply(lambda x: pd.Timedelta(x).days)

Results in:

  Time
1 91
2 28
3 509
4 341
5 250

Python: convert datedelta to int value of time difference

Just reference the days attribute of the timedelta object you have there:

print(date1.days)

There are also timedelta.seconds and timedelta.microseconds attributes, modeling the complete delta state.

Convert Pandas TimeDelta to integer

The following worked for me:

In [24]:

t="""index,timestamp
2015-02-01 00:00:04,00:00:04
2015-02-01 00:00:08,00:00:04
2015-02-01 00:00:12,00:00:04"""
s = pd.read_csv(io.StringIO(t),parse_dates=[0,1], squeeze=True, index_col=[0])
In [26]:

s.dt.second
Out[26]:
index
2015-02-01 00:00:04 4
2015-02-01 00:00:08 4
2015-02-01 00:00:12 4
dtype: int64

datetime dtype values have a dt accessor where you can access the seconds attribute.

pandas convert from datetime to integer timestamp

You can typecast to int using astype(int) and divide it by 10**9 to get the number of seconds to the unix epoch start.

import pandas as pd
df = pd.DataFrame({'time': [pd.to_datetime('2019-01-15 13:25:43')]})
df_unix_sec = pd.to_datetime(df['time']).astype(int)/ 10**9
print(df_unix_sec)

Conversion of a timedelta to int very slow in python

You may find a marginal massive speed-up dropping down to NumPy, bypassing the overhead associated with pd.Series objects.

See also pd.Timestamp versus np.datetime64: are they interchangeable for selected uses?.

# Python 3.6.0, Pandas 0.19.2, NumPy 1.11.3

def days_lambda(dfx):
return (dfx['y']-dfx['x']).apply(lambda x: x.days)

def days_pd(dfx):
return (dfx['y']-dfx['x']).dt.days

def days_np(dfx):
return (dfx['y'].values-dfx['x'].values) / np.timedelta64(1, 'D')

# check results are identical
assert (days_lambda(dfx).values == days_pd(dfx).values).all()
assert (days_lambda(dfx).values == days_np(dfx)).all()

dfx = pd.concat([dfx]*100000)

%timeit days_lambda(dfx) # 5.02 s per loop
%timeit days_pd(dfx) # 5.6 s per loop
%timeit days_np(dfx) # 4.72 ms per loop


Related Topics



Leave a reply



Submit