0

Consider the code below, which uses the historical_rates(today) function. I want to take rates for all days except weekends, is there some example on how to accomplish this?

import datetime
from fixerio import Fixerio

today = datetime.date.today()
fxrio = Fixerio()
fxrio.historical_rates(today)
'''
{u'base': u'EUR',
u'date': u'2016-05-27',
u'rates': {u'AUD': 1.5483,
u'BGN': 1.9558,
u'BRL': 4.031,
u'CAD': 1.456,
u'CHF': 1.1068,
u'CNY': 7.3281,
u'CZK': 27.028,
u'DKK': 7.4367,
u'GBP': 0.76245,
u'HKD': 8.6735,
u'HRK': 7.4905,
u'HUF': 314.21,
u'IDR': 15157.25,
u'ILS': 4.2938,
u'INR': 74.867,
u'JPY': 122.46,
u'KRW': 1316.98,
u'MXN': 20.6611,
u'MYR': 4.5554,
u'NOK': 9.282,
u'NZD': 1.6586,
u'PHP': 52.096,
u'PLN': 4.3912,
u'RON': 4.5034,
u'RUB': 73.7516,
u'SEK': 9.2673,
u'SGD': 1.536,
u'THB': 39.851,
u'TRY': 3.2928,
u'USD': 1.1168,
u'ZAR': 17.4504}}
'''
smci
  • 32,567
  • 20
  • 113
  • 146
NeoVe
  • 3,857
  • 8
  • 54
  • 134
  • 1
    The [documentation tells you that `fxrio.historical_rates()` expects a datetime](https://pypi.org/project/fixerio/). So your question reduces to *"How do I generate a range of datetimes and exclude weekends?"* There are tons of existing questions on SO, also please skim the `datetime` documentation. Show us what code you've tried? – smci Aug 13 '18 at 05:46
  • 1
    When you say "get rates all days except weekends", do you mean you want to generate a date range starting on e.g. 2018/01/01, or just skip fetching the rates if `today` falls a weekend. – smci Aug 13 '18 at 05:54
  • Hi, Thanks.Like generating all year rates, every week of the current year, except weekends – NeoVe Aug 13 '18 at 05:56
  • Possible duplicate of [Loop through dates except for weekends](https://stackoverflow.com/questions/10984741/loop-through-dates-except-for-weekends) – smci Aug 13 '18 at 06:08

1 Answers1

1

use freq = 'B' for business day frequency.

import pandas as pd
dt = pd.date_range(start=datetime.date.today(), periods=10, freq='B')
dt

which gives you :

DatetimeIndex(['2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16',
               '2018-08-17', '2018-08-20', '2018-08-21', '2018-08-22',
               '2018-08-23', '2018-08-24'],
              dtype='datetime64[ns]', freq='B')

you check name of day also by:

dt.weekday_name

Index(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Monday',
       'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
      dtype='object')
Krishna
  • 6,107
  • 2
  • 40
  • 43
  • 1
    Please go post this answer at [Loop through dates except for weekends](https://stackoverflow.com/questions/10984741/loop-through-dates-except-for-weekends), it was likely this question was going to be closed as a duplicate of that. – smci Aug 13 '18 at 06:08
  • FYI there's no point in answering obvious duplicates, since they get closed quick so you won't earn rep from them. Better to figure out which existing question they duplicate, reference it, help improve it if necessary. – smci Aug 13 '18 at 06:17