Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DatetimeIndex to datetime

How I can convert DatetimeIndex to datetime to plot the data's in the next step?

I have a DatetimeIndex list, looks like the following example

[<bound method DatetimeIndex.to_datetime of DatetimeIndex(['2016-07-04 16:19:35', '2016-07-04 16:19:35',
           '2016-07-04 16:19:35', '2016-07-04 16:19:34',
           '2016-07-04 16:19:34', '2016-07-04 16:19:34',
           '2016-07-04 16:19:33', '2016-07-04 16:19:33',
           '2016-07-04 16:19:32', '2016-07-04 16:19:32',
           ...
           '2016-07-30 02:59:38', '2016-07-31 03:09:07',
           '2016-07-31 03:09:03', '2016-07-31 03:09:03',
           '2016-07-31 03:09:55', '2016-07-31 03:09:54',
           '2016-07-31 03:09:54', '2016-07-31 02:59:39',
           '2016-07-31 02:59:38', '2016-07-31 02:59:38'],
          dtype='datetime64[ns]', name='event_timestamp', length=3981364, freq=None)>]

and I need it, in this format

[datetime.datetime(2018, 10, 17, 13, 13, 39, 755816), datetime.datetime(2018, 10, 17, 13, 14, 39, 755816), datetime.datetime(2018, 10, 17, 13, 15, 39, 755816), datetime.datetime(2018, 10, 17, 13, 16, 39, 755816), datetime.datetime(2018, 10, 17, 13, 17, 39, 755816), datetime.datetime(2018, 10, 17, 13, 18, 39, 755816), datetime.datetime(2018, 10, 17, 13, 19, 39, 755816), datetime.datetime(2018, 10, 17, 13, 20, 39, 755816), datetime.datetime(2018, 10, 17, 13, 21, 39, 755816), datetime.datetime(2018, 10, 17, 13, 22, 39, 755816), datetime.datetime(2018, 10, 17, 13, 23, 39, 755816), datetime.datetime(2018, 10, 17, 13, 24, 39, 755816)]

My Python Code looks like this example.

timeStamp = [data1[data1.columns[0]].index]
dateTime = []

for i in timeStamp:
    dateTime = i.to_datetime

I hope you can help me, to fix my little problem.

like image 336
ZPascal Avatar asked Oct 17 '18 11:10

ZPascal


1 Answers

matplotlib working with pandas datetimes nice, but if really need convert it to python datetimes use DatetimeIndex.to_pydatetime:

idx = pd.DatetimeIndex(['2016-07-04 16:19:35', '2016-07-04 16:19:35',
           '2016-07-04 16:19:35', '2016-07-04 16:19:34',
           '2016-07-04 16:19:34', '2016-07-04 16:19:34',
           '2016-07-04 16:19:33', '2016-07-04 16:19:33',
           '2016-07-04 16:19:32', '2016-07-04 16:19:32'])

print (idx)
DatetimeIndex(['2016-07-04 16:19:35', '2016-07-04 16:19:35',
               '2016-07-04 16:19:35', '2016-07-04 16:19:34',
               '2016-07-04 16:19:34', '2016-07-04 16:19:34',
               '2016-07-04 16:19:33', '2016-07-04 16:19:33',
               '2016-07-04 16:19:32', '2016-07-04 16:19:32'],
              dtype='datetime64[ns]', freq=None)

print (idx.to_pydatetime())
[datetime.datetime(2016, 7, 4, 16, 19, 35)
 datetime.datetime(2016, 7, 4, 16, 19, 35)
 datetime.datetime(2016, 7, 4, 16, 19, 35)
 datetime.datetime(2016, 7, 4, 16, 19, 34)
 datetime.datetime(2016, 7, 4, 16, 19, 34)
 datetime.datetime(2016, 7, 4, 16, 19, 34)
 datetime.datetime(2016, 7, 4, 16, 19, 33)
 datetime.datetime(2016, 7, 4, 16, 19, 33)
 datetime.datetime(2016, 7, 4, 16, 19, 32)
 datetime.datetime(2016, 7, 4, 16, 19, 32)]
like image 145
jezrael Avatar answered Oct 17 '22 23:10

jezrael