d2-d1 gives you a datetime.timedelta and when you use days it will only show you the days in the timedelta. In this case it works fine, but if you would have the following.
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 16:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
daysDiff = (d2-d1).days
print daysDiff
> 2
# Convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
> 2880 # that is wrong
It would have still given you the same answer since it still returns 2 for days; it ignores the hour, min and second from the timedelta.
A better approach would be to convert the dates to a common format and then do the calculation. The easiest way to do this is to convert them to Unix timestamps. Here is the code to do that.
from datetime import datetime
import time
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
# Convert to Unix timestamp
d1_ts = time.mktime(d1.timetuple())
d2_ts = time.mktime(d2.timetuple())
# They are now in seconds, subtract and then divide by 60 to get minutes.
print int(d2_ts-d1_ts) / 60
> 3043 # Much better
If your Python version doesn't support td // timedelta; replace it with int(td.total_seconds() // 60).
If the input time is in the local timezone that might have different utc offset at different times e.g., it has daylight saving time then you should make dt1, dt2 into aware datetime objects before finding the difference, to take into account the possible changes in the utc offset.
The portable way to make an aware local datetime objects is to use pytz timezones:
If either dt1 or dt2 correspond to an ambiguous time then the default is_dst=False is used to disambiguate. You could set is_dst=None to raise an exception for ambiguous or non-existent local times instead.
If you can't install 3rd party modules then time.mktime() could be used from @Ken Cochrane's answer that can find the correct utc offset on some platforms for some dates in some timezones -- if you don't need a consistent (but perhaps wrong) result then it is much better than doing dt2 - dt1 with naive datetime objects that always fails if the corresponding utc offsets are different.
If you are trying to find the difference between timestamps that are in pandas columns, the the answer is fairly simple.
If you need it in days or seconds then
# For difference in days:
df['diff_in_days']=(df['timestamp2'] - df['timestamp1']).dt.days
# For difference in seconds
df['diff_in_seconds']=(df['timestamp2'] - df['timestamp1']).dt.seconds
Now minute is tricky as dt.minute works only on datetime64[ns] dtype.
whereas the column generated from subtracting two datetimes has format
AttributeError: 'TimedeltaProperties' object has no attribute 'm8'
So like mentioned by many above to get the actual value of the difference in minute you have to do:
df['diff_in_min']=df['diff_in_seconds']/60
But if just want the difference between the minute parts of the two timestamps then do the following
#convert the timedelta to datetime and then extract minute
df['diff_in_min']=(pd.to_datetime(df['timestamp2']-df['timestamp1'])).dt.minute
You can also read the article https://docs.python.org/3.4/library/datetime.html
and see section 8.1.2 you'll see the read only attributes are only seconds,days and milliseconds. And this settles why the minute function doesn't work directly.