Aaron Foster
Aaron Foster

Reputation: 31

"NameError: name 'datetime' is not defined" with datetime imported

I know there are a lot of datetime not defined posts but they all seem to forget the obvious import of datetime. I can't figure out why I'm getting this error. When I do each step in iPython it works well, but the method dosen't

import requests
import datetime

def daily_price_historical(symbol, comparison_symbol, limit=1, aggregate=1, exchange='', allData='true'):

    url = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit={}&aggregate={}&allData={}'\
        .format(symbol.upper(), comparison_symbol.upper(), limit, aggregate, allData)
    if exchange:
        url += '&e={}'.format(exchange)
    page = requests.get(url)
    data = page.json()['Data']
    df = pd.DataFrame(data)
    df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]

    datetime.datetime.fromtimestamp()

    return df

This code produces this error:

Traceback (most recent call last):
  File "C:\Users\20115619\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-29-4f015e05113f>", line 1, in <module>
    rv.get_prices(30, 'ETH')
  File "C:\Users\20115619\Desktop\projects\testDash\Revas.py", line 161, in get_prices
    for symbol in symbols:
  File "C:\Users\20115619\Desktop\projects\testDash\Revas.py", line 50, in daily_price_historical
    df = pd.DataFrame(data)
  File "C:\Users\20115619\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py", line 4372, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'time'

Upvotes: 2

Views: 2099

Answers (1)

sniperd
sniperd

Reputation: 5274

df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]

I think that line is the problem.

Your Dataframe df at the end of the line doesn't have the attribute .time

For what it's worth I'm on Python 3.6.0 and this runs perfectly for me:

import requests
import datetime
import pandas as pd

def daily_price_historical(symbol, comparison_symbol, limit=1, aggregate=1, exchange='', allData='true'):
    url = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit={}&aggregate={}&allData={}'\
        .format(symbol.upper(), comparison_symbol.upper(), limit, aggregate, allData)
    if exchange:
        url += '&e={}'.format(exchange)
    page = requests.get(url)
    data = page.json()['Data']
    df = pd.DataFrame(data)
    df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]
    #I don't have the following function, but it's not needed to run this
    #datetime.datetime.fromtimestamp()
    return df

df = daily_price_historical('BTC', 'ETH')
print(df)

Note, I commented out the line that calls an external function that I do not have. Perhaps you have a global variable causing a problem?

Update as per the comments:

I'd use join instead to make the URL:

url = "".join(["https://min-api.cryptocompare.com/data/histoday?fsym=", str(symbol.upper()), "&tsym=", str(comparison_symbol.upper()), "&limit=", str(limit), "&aggregate=", str(aggregate), "&allData=", str(allData)])

Upvotes: 1

Related Questions