stratofortress
stratofortress

Reputation: 453

How to calculate the number of days between two given dates

import csv

f = csv.reader(open('lmt.csv', 'r'))         # open input file for reading
Date, Open, Hihh, mLow, Close, Volume = zip(*f)    # split it into separate columns

Now I want to pick two dates out of the Date column and calculate the number of days between them. Sadly, because they are retrieved from a .csv file, they are in "'\xef\xbb\xbfDate'" format.

So when I attempted to use datetime module, it didn't work.

One thing I can think of is have excel do the calculation by means of =DATEDIF(A2, B2, "d") but I'd love to have a more elegant solution.

Upvotes: 1

Views: 2155

Answers (1)

Cleb
Cleb

Reputation: 25997

As a follow-up of my answer to one of your previous questions (where one also finds the link to the data file), the following will work fine. The code below will calculate the amount of days between df['Date'][0] ('17-Feb-16') and df['Date'][10] ('2-Feb-16'). The output you receive is:

The difference are 15 days.

Here is the code with a couple of inline comments:

import pandas as pd
from datetime import datetime

df = pd.read_csv('lmt.csv')

# get rid of the format issue
df.rename(columns={df.columns[0]: 'Date' }, inplace=True)

# define your format in the Date column
date_format = '%d-%b-%y'

# select the first date
date1 = datetime.strptime(df['Date'][0], date_format)

# select the second date
date2 = datetime.strptime(df['Date'][10], date_format)

# calculate the difference between the dates
diffDates = date1 - date2

# print results in days
print 'The difference are ' + str(diffDates.days) + ' days.'

If you want the output not in days but in hours, you can check this post.

EDIT

For the new file you need:

date_format = '%Y-%m-%d'

Then it works just fine. Check this link for a detailed explanation of the different formats.

Upvotes: 1

Related Questions