0

I have a JSON file full of unix timestamps in string form. I am trying to convert these timestamps to human readable times to then display in matplotlib.pyplot.

Upon converting the timestamps I am getting the error:

ValueError: unconverted data remains: .1806107

How can I convert my dates to a human readable form YY-MM-DD HH-MM-SS?

contents = json.loads(open("foo.json").read()) 
dates = [ ticker['date'] for ticker in contents ]
data = [ ticker['last'] for ticker in contents ]

# Example of array contents
#dates = [1497918349.3060017, 1497918352.9935713, 1497918358.8218484, 1497918364.0406654, 1497918368.9628277]
#data = [1,2,3,4,5]

# Error occurs here
dates=[dt.datetime.strptime(str(date),'%Y%m%d%H%M') for date in dates]

# Also tried the following but I get the error:
# TypeError: an integer is required (got type datetime.datetime)
dates=[dt.datetime.fromtimestamp(date) for date in dates]

plt.plot(dates, data, 'r-')

plt.show()
sazr
  • 24,984
  • 66
  • 194
  • 362

2 Answers2

0
from datetime import datetime

dates = [1497918349.3060017, 1497918352.9935713, 1497918358.8218484, 1497918364.0406654, 1497918368.9628277]

dates = [datetime.utcfromtimestamp(d).strftime('%F %T') for d in dates]

dates
['2017-06-20 00:25:49',
 '2017-06-20 00:25:52',
 '2017-06-20 00:25:58',
 '2017-06-20 00:26:04',
 '2017-06-20 00:26:08']
amstree
  • 537
  • 1
  • 4
  • 12
  • It would be nice if you could explain in one or two sentences why this code works but OP's code doesn't. This would make this answer useful for others as well. – ImportanceOfBeingErnest Jun 24 '17 at 08:00
0

If you change your

dates=[dt.datetime.strptime(str(date),'%Y%m%d%H%M') for date in dates]

to

dates=[dt.datetime.fromtimestamp(date).strftime('%Y%m%d%H%M') for date in dates]

all will be fine.

E.g.:

from __future__ import print_function

import datetime as dt

dates = [1497918349.3060017, 1497918352.9935713, 1497918358.8218484, 1497918364.0406654, 1497918368.9628277]

# Error used to occur here
#dates=[dt.datetime.strptime(str(date),'%Y%m%d%H%M') for date in dates]
dates=[dt.datetime.fromtimestamp(date).strftime('%Y%m%d%H%M') for date in dates]

print(dates)

gives:

['201706200325', '201706200325', '201706200325', '201706200326', '201706200326']

See this SO answer for elaboration.

boardrider
  • 5,882
  • 7
  • 49
  • 86