saremisona
saremisona

Reputation: 369

Converting a csv file to Datetime

I've searched thoroughly for an answer before asking, but all I've been able to find was how to convert one column of my csv file into a datetime object.

My problem is, my file has a separate column for year, month, day, hour, minute, and I've been struggling for a while to convert and combine them.

Any help would be much appreciated.

head of file content: viewed in excel

Upvotes: 0

Views: 4411

Answers (3)

jezrael
jezrael

Reputation: 863791

You can use read_csv with parameters parse_dates and date_parser. See also docs:

import pandas as pd
from pandas.compat import StringIO

temp=u"""year,month,day,hour,minute,a
2017,09,01,10,20,1
2017,10,01,10,20,2
2017,11,01,10,20,3"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
parser = lambda x: pd.datetime.strptime(x, '%Y %m %d %H %M')
df = pd.read_csv(StringIO(temp), 
                 parse_dates={'date':['year','month','day','hour','minute']},
                 date_parser=parser)
print (df)
                 date  a
0 2017-09-01 10:20:00  1
1 2017-10-01 10:20:00  2
2 2017-11-01 10:20:00  3

Upvotes: 3

tell k
tell k

Reputation: 615

I'll write a simple way.

Suppose there is a CSV file like the one below.

year,month,day,hour,minute
2017,1,1,23,59
2017,2,1,23,59
2017,3,1,23,59

You can parse csv and create a datetime object.

import csv
from datetime import datetime

with open('test.csv') as fp:
    reader = csv.reader(fp)
    next(reader) # skip header
    for row in reader:
        row = [int(r) for r in row]
        print(datetime(row[0], row[1], row[2], row[3], row[4]))

The result is like this.

2017-01-01 23:59:00
2017-02-01 23:59:00
2017-03-01 23:59:00

thx:)

Upvotes: 2

FuzzyAmi
FuzzyAmi

Reputation: 8157

this might be a little manual but you could:

  1. read each line, split it into your various items (year, month etc) as string.
  2. then recompose into a datetime object with datetime.datetime.strptime (which will convert a string with a given format to a datetime).

Upvotes: 0

Related Questions