Reputation: 6171
I have data in a table as presented below:
YEAR DOY Hour
2015 1 0
2015 1 1
2015 1 2
2015 1 3
2015 1 4
2015 1 5
This is how I'm reading the file:
df = pd.read_table('data2015.lst', sep='\s+')
lines = len(df)
To convert it to a datetime
object I do:
dates = []
for l in range(0,lines):
date = str(df.ix[l,0])[:-2] +' '+ str(df.ix[l,1])[:-2] +' '+ str(df.ix[l,2])[:-2]
d = pd.to_datetime(date, format='%Y %j %H')
dates.append(d)
But this is taking a lot of time.
Is there some way to do it (more directly) without the loop?
Upvotes: 1
Views: 79
Reputation: 11895
You can do it in one line when reading it:
df = pd.read_csv('file.txt', sep='\s+', index_col='Timestamp',
parse_dates={'Timestamp': [0,1,2]},
date_parser=lambda x: pd.datetime.strptime(x, '%Y %j %H'))
Timestamp
2015-01-01 00:00:00
2015-01-01 01:00:00
2015-01-01 02:00:00
2015-01-01 03:00:00
2015-01-01 04:00:00
2015-01-01 05:00:00
Upvotes: 1