Python time conversion

I have a large CSV file with a TIME column. It is written as 1318, and I would like to use Python / Pandas to convert the data as 13:18 and see it as time instead of int64.

I tried something like this, but this is not what I want:

df['TIME'] = pd.to_datetime(df['TIME'])

Because I get this:

    1970-01-01 00:00:00.000001318                           
    1970-01-01 00:00:00.000001041                              
    1970-01-01 00:00:00.000000853

Any ideas?

+4
source share
3 answers

If you pass the format parameter to to_datetime, you get the following:

In [111]:

t="1318"
pd.to_datetime(t, format='%H%M')
Out[111]:
Timestamp('1900-01-01 13:18:00')

However, I don't know if you want a default date here.

+4
source

Use the keyword argument formatfor the method pd.to_datetime.

This is a line with the usual syntax strftime.

+2
source

This can help:

  def extractTime(l):
    tt = (l.split(' ')[1]).split('.')[1][-4:];
    return tt[:2] + ':' + tt[-2:];

    if __name__ == '__main__':
      l = "1970-01-01 00:00:00.000001318";
      print extractTime(l);

He will print "13:18" for a test case.

+1
source

All Articles