v Calculate Difference Between Dates And Times - Machine Learning

Calculate Difference Between Dates And Times

Preliminaries

# Load library
import pandas as pd

Create Date And Time Data

# Create data frame
df = pd.DataFrame()

# Create two datetime features
df['Arrived'] = [pd.Timestamp('01-01-2017'), pd.Timestamp('01-04-2017')]
df['Left'] = [pd.Timestamp('01-01-2017'), pd.Timestamp('01-06-2017')]

Calculate Difference (Method 1)

# Calculate duration between features
df['Left'] - df['Arrived']
0   0 days
1   2 days
dtype: timedelta64[ns]

Calculate Difference (Method 2)

# Calculate duration between features
pd.Series(delta.days for delta in (df['Left'] - df['Arrived']))
0    0
1    2
dtype: int64