4

I am working on a Data Frame in pandas. I wanted to convert my time column into to_datetime, I am getting 1900-01-01 before my time.

Some sample data,

  Date              hour
01-06-2013      23:00:00
02-06-2013      01:00:00
02-06-2013      21:00:00
02-06-2013      22:00:00
02-06-2013      23:00:00
03-06-2013      01:00:00
03-06-2013      21:00:00
03-06-2013      22:00:00
03-06-2013      23:00:00
04-06-2013      01:00:00

This what I tried,

df['hour'] = pd.to_datetime(df['hour'],format ='%H:%M:%S', errors = 'coerce')

I am getting like this,

print(df['hour'])

0        1900-01-01 23:00:00
1        1900-01-01 01:00:00
2        1900-01-01 21:00:00
3        1900-01-01 00:00:00

I do not need 1900-01-01 in my df. Why is this happening and how can I get rid of this?

  • 1
    It would help if you show an example of your original data – Andrew Aug 10 '17 at 21:43
  • 3
    Take a look here - https://stackoverflow.com/questions/32375471/pandas-convert-strings-to-time-without-date it should solve your issue. – Dror Av. Aug 10 '17 at 21:45
  • @droravr yes, I looked at it, it, gives the time from it, but, when I use that for later filtering purposes it won't allow me to do –  Aug 10 '17 at 21:48
  • You can't post 5 dates? – Andrew Aug 10 '17 at 21:48
  • @droravr this is another one: https://stackoverflow.com/questions/32627450/pandas-datetime-formatting I was using, but no definite answer in this! –  Aug 10 '17 at 21:57
  • 1
    Your problem seems solvable, but doesn't contain much information. If the link posted earlier on this comment thread doesnt solve your problem, then you should demonstrate that. see https://stackoverflow.com/help/mcve – Andrew Aug 10 '17 at 21:57
  • @Andrew done, hopefully, that helps. I see there are many threads out there, but nothing helps to get rid of 1900-01-01. As shown above link, it creates a new column with count! –  Aug 10 '17 at 22:01
  • `pd.to_datetime(df['hour'], format='%H:%M:%S', errors='coerce').dt.time`? –  Aug 10 '17 at 22:02
  • @Evert yes, that works to get rid of 1900-01-01 but when I try to filter df by hour, I am getting an error `TypeError: '<' not supported between instances of 'datetime.time' and 'str'` I guess that's a separate question! –  Aug 10 '17 at 22:05
  • 1
    I think that's the actual question you're asking, but it wasn't clear from your previous comment in reply to a linked question. The thing is, if you want a datetime object, you will have to carry along some year-month-day information. If you won't want the 1900 default, combine it with your other column to get the proper datetime. –  Aug 10 '17 at 22:10

1 Answers1

6

Very close, just set the column to time at the end of to_datetime()

df['hour'] = pd.to_datetime(df.hour,format ='%H:%M:%S', errors = 'coerce').dt.time

         Date      hour
0  01-06-2013  23:00:00
1  02-06-2013  01:00:00
2  02-06-2013  21:00:00
3  02-06-2013  22:00:00
4  02-06-2013  23:00:00
5  03-06-2013  01:00:00
6  03-06-2013  21:00:00
7  03-06-2013  22:00:00
8  03-06-2013  23:00:00
9  04-06-2013  01:00:00
DJK
  • 8,924
  • 4
  • 24
  • 40