Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Pandas datetime
#1
I have some data which I read in pandas like this
data = pd.read_table('filename.txt', sep='  ',index_col=None,engine='python')
The first column is date time in the format 120631135243(YYMMDDhhmmss).
I would like to convert this to a proper date time in the format YY MM DD hh:mm:ss

I tried this
date1=datetime.datetime.fromtimestamp(data2/1e3)
where data2 is data.iloc[1:,0], the first column of the file
but resulted in this error
Error:
TypeError                                 Traceback (most recent call last) <ipython-input-74-a215186a1d9c> in <module>() ----> 1 date1=datetime.datetime.fromtimestamp(data2/1e3) /home/nuncio/anaconda2/lib/python2.7/site-packages/pandas/core/series.pyc in wrapper(self)     78             return converter(self.iloc[0])     79         raise TypeError("cannot convert the series to " ---> 80                         "{0}".format(str(converter)))     81     82     return wrapper TypeError: cannot convert the series to <type 'float'>
Any suggestions
thankyou
Reply
#2
datetime.fromtimestamp takes as a parameter the number of seconds since the epoch. That's not what you have, you have the digits of the various parts of the date/time concatenated together to make an integer.

I would break apart the number you have and feed it into the standard datetime constructor, which takes parameters like year, month, day, hour, minutes, and seconds. You could do that either with mod (%) and integer division (//), or you could make the number a string, use slices to get the parts you want, and convert them back into integers. My guess is that the number you have is already a string, and that's why you're getting a conversion error instead of an incorrect date.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Quote:The first column is date time in the format 120631135243(YYMMDDhhmmss).

Dos raw data look like this?
Give a example of input,that is raw.
Pandas has a heavy build in Time Series / Date functionality.
Reply
#4
(Jan-06-2017, 04:48 PM)ichabod801 Wrote: datetime.fromtimestamp takes as a parameter the number of seconds since the epoch. That's not what you have, you have the digits of the various parts of the date/time concatenated together to make an integer.

I would break apart the number you have and feed it into the standard datetime constructor, which takes parameters like year, month, day, hour, minutes, and seconds. You could do that either with mod (%) and integer division (//), or you could make the number a string, use slices to get the parts you want, and convert them back into integers. My guess is that the number you have is already a string, and that's why you're getting a conversion error instead of an incorrect date.

THanks  Its working
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas datetime: add timedelta ju21878436312 3 6,671 Jul-05-2021, 06:16 PM
Last Post: eddywinch82
  itertuples, datetime, pandas, groupby, in range karlito 0 2,425 Nov-29-2019, 11:35 AM
Last Post: karlito
  itertuples, new column, datetime, pandas karlito 6 3,890 Nov-29-2019, 11:07 AM
Last Post: karlito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020