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
Python - Difference Between Two Strings
Can't Install New Packages for Python (Python 3.9.0, Windows 10)
Checking If All Elements in a List Are Unique
Get Last "Column" After .Str.Split() Operation on Column in Pandas Dataframe
Python: Find_Element_By_Css_Selector
Interpolate Nan Values in a Numpy Array
Why Sum on Lists Is (Sometimes) Faster Than Itertools.Chain
Popen with Conflicting Executable/Path
Pandas Fill Missing Values in Dataframe from Another Dataframe
Strange Behavior of Lists in Python
Downloading File to Specified Location with Selenium and Python
How to Do a Not Equal in Django Queryset Filtering
How to Dump a Dict to a JSON File