0

I have modified my code and commented it for posting purposes, i'm new to numpy and can't figure out why it's not letting me plot this, it's an array of correctly formatted dates vs floats.

If anyone could give any advice it would be really appreciated

import datetime as dt
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import io

#----------------------------------------------------------
#Takes JSON data from online source and saves to .csv
URL_LTC = 'http://cryptocoincharts.info/fast/period.php?pair=LTC-USD&market=bitfinex&time=alltime&resolution=1d'
URL_BTC = 'http://cryptocoincharts.info/fast/period.php?pair=BTC-USD&market=bitfinex&time=alltime&resolution=1d'
data_LTC = pd.read_json(URL_LTC)
data_BTC = pd.read_json(URL_BTC)
data_BTC.to_csv('BTC_USD_PANDAS.csv')
data_LTC.to_csv('LTC_USD_PANDAS.csv')
#----------------------------------------------------------
#reads in data from csv
df_BTC = pd.read_csv('BTC_USD_PANDAS.csv',parse_dates=[0], dayfirst=True)
df_LTC = pd.read_csv('LTC_USD_PANDAS.csv',parse_dates=[0], dayfirst=True)
# turn into a numpy data array 
BTC_data = df_BTC.values 
LTC_data = df_LTC.values
#select the colums 
offset = 6 #LTC starts 6 days before BTC 
BTC_prices = BTC_data[1:269, 5:6]
LTC_prices = LTC_data[1+offset:269+offset, 5:6]
dates = BTC_data[1:269, 1:2]

#concatenate arrays
prices = np.hstack((BTC_prices,LTC_prices))

#plot data 
plt.clf()
plt.plot(dates, prices)
plt.legend(['BTC','LTC'])
plt.ylabel('Close')
plt.xlabel('Date')
plt.savefig('BTC_LTC.pdf', format='pdf')

Results in this error (shortened version):

  File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 575, in recache
    x = np.asarray(xconv, np.float_)
  File "C:\Python27\lib\site-packages\numpy\core\numeric.py", line 462, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for float(): 2014-08-19 00
[Finished in 1.6s with exit code 1] 
1
  • 1
    The error is clear 2014-08-19 00 isn't and cannot be coerced into a float. What are the dtypes of dates here? Also post some data that reproduces the error Commented Jan 25, 2016 at 9:31

1 Answer 1

1

The column number for dates may be wrong, try parse_dates=[1]

df_BTC = pd.read_csv('BTC_USD_PANDAS.csv',parse_dates=[1], dayfirst=True)
df_LTC = pd.read_csv('LTC_USD_PANDAS.csv',parse_dates=[1], dayfirst=True)

In the produced .csv files the dates are in the second field, not the first.

After this modification dates are an array of timestamps instead of strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.