-1

I have a string object in this format 2014-12-08 09:30:00.066000 but I want to convert to datetime variable. I also want this to be less granular- I want it to be just in the order of second for example

2014-12-08 09:30:00.066000 to 2014-12-08 09:30:00

I am trying to use pd.to_datetime function but it's not working for me. Anyone know how to do this? Thanks!

  • Which library is pd.to_datetime... ? pandas? – Anshul Goyal Feb 13 '15 at 17:48
  • Can you show how you were using pd.to_datetime? That should work fine. If for some reason it won't work, you can apply a strptime function to the str series. https://docs.python.org/2/library/datetime.html – Liam Foley Feb 13 '15 at 17:52
  • Sorry for not being very clear. pd.to_datetime is from pandas. And what I meant by not working is that I want my timestamp to be less granular. In my example, it is in the order of microseconds, but I want it to show only up to the order of second. My thought was that pd.to_datetime will automatically convert to the order of seconds. Also, what if I want it to show up to the order of minute only, is there any way to do this? This is because in the end, I want to do the aggregate sum of other variables minute by minute. Thanks a lot! –  Feb 13 '15 at 18:02

1 Answers1

0

See this:

How to round a Pandas `DatetimeIndex`?

from pandas.lib import Timestamp

def to_the_second(ts):
    return Timestamp(long(round(ts.value, -9)))

df['My_Date_Column'].apply(to_the_second)
Community
  • 1
  • 1
Liam Foley
  • 7,432
  • 2
  • 26
  • 24