5

I have a large CSV-file with a column called TIME. It's 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've 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?

Counter10000
  • 525
  • 1
  • 8
  • 25
Donald
  • 145
  • 2
  • 4
  • 14

3 Answers3

7

If you pass a format param to to_datetime you will 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.

EdChum
  • 376,765
  • 198
  • 813
  • 562
2

Use the format keyword argument to pd.to_datetime method.

It is a string with the usual strftime syntax.

wim
  • 338,267
  • 99
  • 616
  • 750
2

This might 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);

It will print out "13:18" for the test case.

Dan
  • 126
  • 1
  • 14