2

I want to use the Forex-python module to convert amounts in various currencies to a specific currency ("DKK") according to a specific date (The last day of a previous month according to a date in the dataframe)

This is the structure of my code:

pd.DataFrame(data={'Date':['2017-4-15','2017-6-12','2017-2-25'],'Amount':[5,10,15],'Currency':['USD','SEK','EUR']})

def convert_rates(amount,currency,PstngDate):
    PstngDate = datetime.strptime(PstngDate, '%Y-%m-%d')
    if currency != 'DKK':
        return c.convert(base_cur=currency,dest_cur='DKK',amount=amount \
                     ,date_obj=PstngDate - timedelta(PstngDate.day))
    else:
        return amount

and the the new column with the converted amount:

df['Amount, DKK'] = np.vectorize(convert_rates)(
    amount=df['Amount'],
    currency=df['Currency'],
    PstngDate=df['Date']
)

I get the RatesNotAvailableError "Currency Rates Source Not Ready" Any idea what can cause this? It has previously worked with small amounts of data, but I have many rows in my real df...

Alex5207
  • 464
  • 2
  • 7
  • 16
  • what library is `c.convert()` calling? If it's calling some external source, it's possible that you're being rate limited because `np.vectorize` is calling too frequently. Check the docs for whatever `c` is, then perhaps you might need to do the lookups serially. – greg_data Aug 22 '17 at 11:23
  • Ah, sorry: It's calling the forex-python library - Do you know how I can get around the limitation? – Alex5207 Aug 22 '17 at 11:25
  • https://github.com/MicroPyramid/forex-python/blob/80290a2b9150515e15139e1a069f74d220c6b67e/forex_python/converter.py#L73 Looks like the library doesn't talk about quotas. Your error just suggests the source returned a non 200 response code. Suggest instead doing the calculation serially, with a retry function and some kind of backoff – greg_data Aug 22 '17 at 11:27

2 Answers2

1

I inserted a small print statement into convert.py (part of forex-python) to debug this.

print(response.status_code)

Currently I receive:

502

Read these threads about the HTTP 502 error:

In HTTP 502, what is meant by an invalid response?

https://www.lifewire.com/502-bad-gateway-error-explained-2622939

These errors are completely independent of your particular setup, meaning that you could see one in any browser, on any operating system, and on any device.

502 indicates that currently there is a problem with the infrastructure this API uses to provide us with the required data. As I am in need of the data myself I will continue to monitor this issue and keep my post on this site updated.

There is already an issue on Github regarding this issue:

https://github.com/MicroPyramid/forex-python/issues/100

Ohumeronen
  • 1,769
  • 2
  • 14
  • 28
0

From the source: https://github.com/MicroPyramid/forex-python/blob/80290a2b9150515e15139e1a069f74d220c6b67e/forex_python/converter.py#L73

Your error means the library received a non 200 response code to your request. This could mean the site is down, or it's blocked you momentarily because you're hammering it with requests.

Try replacing the call to c.convert with something like:

from time import sleep
def try_convert(amount, currency, PstngDate):
    success = False
    while success == False:
        try:
            res = c.convert(base_cur=currency,dest_cur='DKK',amount=amount \
                     ,date_obj=PstngDate - timedelta(PstngDate.day))
        except:
            #wait a while
            sleep(10)
    return res

Or even better, use a library like backoff, to do the retrying for you:

https://pypi.python.org/pypi/backoff/1.3.1

greg_data
  • 2,247
  • 13
  • 20