Module 3 - Nurturing session

You can access live notes from https://live.arcesium-lab.pipal.in

Combined Daily Time Series

%load_problem combined-daily-time-series
Problem: Combined Daily Time Series

Write a function combined_daily_time_series that combines stock data from www.alphavantage.co. for given list of symbols and returns a dataframe. The dataframe should have these numeric columns, open, high, low, close, volume, symbol. Here are some hints. Have a look at these functions and methods pd.concat, DataFrame.rename, pd.to_numeric

Also write a function get_total_volume which takes dataframe generated by above function and returns a series that symbols as row labels and total volume for that symbol as a value.

>>> df = combined_daily_time_series(["AAPL","IBM"])
>>> df
open  high low close volume symbol
2022-08-25 20:00:00 170.290 170.3500 170.2000 170.2000 11160 AAPL
2022-08-25 20:15:00 170.290 170.3500 170.2000 170.2000 11160 AAPL
2022-08-25 20:30:00 170.290 170.3500 170.2000 170.2000 11160 AAPL
.
.
2022-08-25 20:00:00 170.290 170.3500 170.2000 170.2000 11160 IBM
2022-08-25 20:15:00 170.290 170.3500 170.2000 170.2000 11160 IBM

200 rows × 6 columns

>>> get_total_volume(df)
symbol
AAPL    77772389
IBM      7689538
Name: volume, dtype: int64

You can verify your solution using:

%verify_problem combined-daily-time-series

# your code here

def get_daily_timeseries(symbolname):
    pass

def combined_daily_time_series(symbols):
    datasets = []
    for symbol in symbols:
        df = get_daily_timeseries(symbol)
        # rename the columns
        # convert the columns to correct datatype!
        df['symbol'] = symbol
        datasets.append(df) 

    return pd.concat(datasets)



def get_total_volume(df):
    return df.groupby('symbol')['volume'].sum()
    
import pandas as pd
s = pd.Series(["1", "2", "3", "4"])
s
0    1
1    2
2    3
3    4
dtype: object
pd.to_numeric(s)
0    1
1    2
2    3
3    4
dtype: int64
df = pd.DataFrame({"a":range(5),
                   "b":list("12345")})
df
a b
0 0 1
1 1 2
2 2 3
3 3 4
4 4 5
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   a       5 non-null      int64 
 1   b       5 non-null      object
dtypes: int64(1), object(1)
memory usage: 208.0+ bytes
df['b'] = pd.to_numeric(df['b'])
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   a       5 non-null      int64
 1   b       5 non-null      int64
dtypes: int64(2)
memory usage: 208.0 bytes
df['symbol'] = "IBM"
df
a b symbol
0 0 1 IBM
1 1 2 IBM
2 2 3 IBM
3 3 4 IBM
4 4 5 IBM
df.rename(columns={"a":"open", "b":"close"})
open close symbol
0 0 1 IBM
1 1 2 IBM
2 2 3 IBM
3 3 4 IBM
4 4 5 IBM
  • The site allows only 25 calls per day!
  • get your data in one variable after you download it from internet!
  • then till you fix your solution don’t fetch the data again and again …make use of variable

Client for Free Dictionary API

%load_problem dict-client
Problem: Client for Free Dictionary API

Write a program dict.py to find the meaning of a word using the Free Dictionary API.

Expected Usage

The program should take one word as argument and display the phonetics and meaning of that word using the Free Dictionary API.

$ python dict.py obscure
onscure /əbˈskjɔː(ɹ)/

VERB

To render obscure; to darken; to make dim; to keep in the dark; to hide; to make less visible, intelligible, legible, glorious, beautiful, or illustrious.

To hide, put out of sight etc.

To conceal oneself; to hide.

ADJECTIVE

Dark, faint or indistinct.

Hidden, out of sight or inconspicuous.

As you can see, The first line in the word and its phonetic. For each part of speech, the part of speech is shown in uppercase, followed by each of the meaning provided in the response. There will be exactly one new line after each meaning and part-of-speech line.

You can verify your solution using:

%verify_problem dict-client

%%file dict.py
# your code here

import sys
import requests

def get_word_info(word):
    url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
    return requests.get(url).json()

word = sys.argv[0]
print(get_word_info(word))
Overwriting dict.py
!python dict.py hello
{'title': 'No Definitions Found', 'message': "Sorry pal, we couldn't find definitions for the word you were looking for.", 'resolution': 'You can try the search again at later time or head to the web instead.'}
!python dict.py eat
{'title': 'No Definitions Found', 'message': "Sorry pal, we couldn't find definitions for the word you were looking for.", 'resolution': 'You can try the search again at later time or head to the web instead.'}
import requests
word = "hello"
requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}").json()
[{'word': 'hello',
  'phonetics': [{'audio': 'https://api.dictionaryapi.dev/media/pronunciations/en/hello-au.mp3',
    'sourceUrl': 'https://commons.wikimedia.org/w/index.php?curid=75797336',
    'license': {'name': 'BY-SA 4.0',
     'url': 'https://creativecommons.org/licenses/by-sa/4.0'}},
   {'text': '/həˈləʊ/',
    'audio': 'https://api.dictionaryapi.dev/media/pronunciations/en/hello-uk.mp3',
    'sourceUrl': 'https://commons.wikimedia.org/w/index.php?curid=9021983',
    'license': {'name': 'BY 3.0 US',
     'url': 'https://creativecommons.org/licenses/by/3.0/us'}},
   {'text': '/həˈloʊ/', 'audio': ''}],
  'meanings': [{'partOfSpeech': 'noun',
    'definitions': [{'definition': '"Hello!" or an equivalent greeting.',
      'synonyms': [],
      'antonyms': []}],
    'synonyms': ['greeting'],
    'antonyms': []},
   {'partOfSpeech': 'verb',
    'definitions': [{'definition': 'To greet with "hello".',
      'synonyms': [],
      'antonyms': []}],
    'synonyms': [],
    'antonyms': []},
   {'partOfSpeech': 'interjection',
    'definitions': [{'definition': 'A greeting (salutation) said when meeting someone or acknowledging someone’s arrival or presence.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello, everyone.'},
     {'definition': 'A greeting used when answering the telephone.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello? How may I help you?'},
     {'definition': 'A call for response if it is not clear if anyone is present or listening, or if a telephone conversation may have been disconnected.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello? Is anyone there?'},
     {'definition': 'Used sarcastically to imply that the person addressed or referred to has done something the speaker or writer considers to be foolish.',
      'synonyms': [],
      'antonyms': [],
      'example': 'You just tried to start your car with your cell phone. Hello?'},
     {'definition': 'An expression of puzzlement or discovery.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello! What’s going on here?'}],
    'synonyms': [],
    'antonyms': ['bye', 'goodbye']}],
  'license': {'name': 'CC BY-SA 3.0',
   'url': 'https://creativecommons.org/licenses/by-sa/3.0'},
  'sourceUrls': ['https://en.wiktionary.org/wiki/hello']}]
worddata = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}").json()
worddata
[{'word': 'hello',
  'phonetics': [{'audio': 'https://api.dictionaryapi.dev/media/pronunciations/en/hello-au.mp3',
    'sourceUrl': 'https://commons.wikimedia.org/w/index.php?curid=75797336',
    'license': {'name': 'BY-SA 4.0',
     'url': 'https://creativecommons.org/licenses/by-sa/4.0'}},
   {'text': '/həˈləʊ/',
    'audio': 'https://api.dictionaryapi.dev/media/pronunciations/en/hello-uk.mp3',
    'sourceUrl': 'https://commons.wikimedia.org/w/index.php?curid=9021983',
    'license': {'name': 'BY 3.0 US',
     'url': 'https://creativecommons.org/licenses/by/3.0/us'}},
   {'text': '/həˈloʊ/', 'audio': ''}],
  'meanings': [{'partOfSpeech': 'noun',
    'definitions': [{'definition': '"Hello!" or an equivalent greeting.',
      'synonyms': [],
      'antonyms': []}],
    'synonyms': ['greeting'],
    'antonyms': []},
   {'partOfSpeech': 'verb',
    'definitions': [{'definition': 'To greet with "hello".',
      'synonyms': [],
      'antonyms': []}],
    'synonyms': [],
    'antonyms': []},
   {'partOfSpeech': 'interjection',
    'definitions': [{'definition': 'A greeting (salutation) said when meeting someone or acknowledging someone’s arrival or presence.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello, everyone.'},
     {'definition': 'A greeting used when answering the telephone.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello? How may I help you?'},
     {'definition': 'A call for response if it is not clear if anyone is present or listening, or if a telephone conversation may have been disconnected.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello? Is anyone there?'},
     {'definition': 'Used sarcastically to imply that the person addressed or referred to has done something the speaker or writer considers to be foolish.',
      'synonyms': [],
      'antonyms': [],
      'example': 'You just tried to start your car with your cell phone. Hello?'},
     {'definition': 'An expression of puzzlement or discovery.',
      'synonyms': [],
      'antonyms': [],
      'example': 'Hello! What’s going on here?'}],
    'synonyms': [],
    'antonyms': ['bye', 'goodbye']}],
  'license': {'name': 'CC BY-SA 3.0',
   'url': 'https://creativecommons.org/licenses/by-sa/3.0'},
  'sourceUrls': ['https://en.wiktionary.org/wiki/hello']}]
worddata[0]['word']
'hello'
worddata[0].keys()
dict_keys(['word', 'phonetics', 'meanings', 'license', 'sourceUrls'])
worddata[0]['phonetics']
[{'audio': 'https://api.dictionaryapi.dev/media/pronunciations/en/hello-au.mp3',
  'sourceUrl': 'https://commons.wikimedia.org/w/index.php?curid=75797336',
  'license': {'name': 'BY-SA 4.0',
   'url': 'https://creativecommons.org/licenses/by-sa/4.0'}},
 {'text': '/həˈləʊ/',
  'audio': 'https://api.dictionaryapi.dev/media/pronunciations/en/hello-uk.mp3',
  'sourceUrl': 'https://commons.wikimedia.org/w/index.php?curid=9021983',
  'license': {'name': 'BY 3.0 US',
   'url': 'https://creativecommons.org/licenses/by/3.0/us'}},
 {'text': '/həˈloʊ/', 'audio': ''}]
for p in worddata[0]['phonetics']:
    if 

Frankfurter Exchange Rates

%load_problem frankfurter
Problem: Frankfurter Exchange Rates

Write a program frankfurter.py to list the historical currency rate of a currency against a base currency using Frankfurter Exchange Rate API.

The program should take the following command-line arguments.

  -c CURRENCY, --currency CURRENCY
                        target currency, default INR
  -b BASE, --base BASE  base currency, default USD
  -d DATE, --date DATE  First date to consider, default yesterday
  -n DAYS, --days DAYS  number of days to display

The program should display the curreny rate between the base currency and the target currency for n days starting from yesterday. Optionally, the start date could be provided as a command-line argument.

Please note that there convertion data is not available on weekends. So the number of rows of data shown may be less than n.

The Output Format

The output needs to be properly tabulated. Please use Python library tabulate for doing this.

Please refer to Printing Tables with Tabulate in the Python Cookbook to learn how to the the tabulate library.

Usage

$ python frankfurter.py
Date          USD    INR
----------  -----  -----
2023-10-18      1  83.25
2023-10-17      1  83.22
2023-10-16      1  83.25
2023-10-13      1  83.27
2023-10-12      1  83.24
2023-10-11      1  83.18
2023-10-10      1  83.24
2023-10-09      1  83.3
$ python frankfurter.py -n 2
Date          USD    INR
----------  -----  -----
2023-10-18      1  83.25
2023-10-17      1  83.22
$ python frankfurter.py -b GBP
Date          GBP     INR
----------  -----  ------
2023-10-18      1  101.55
2023-10-17      1  101.31
2023-10-16      1  101.37
2023-10-13      1  101.41
2023-10-12      1  102.47
2023-10-11      1  102.24
2023-10-10      1  101.96
2023-10-09      1  101.39
$ python frankfurter.py -b GBP -c USD
Date          GBP     USD
----------  -----  ------
2023-10-18      1  1.2198
2023-10-17      1  1.2173
2023-10-16      1  1.2176
2023-10-13      1  1.2178
2023-10-12      1  1.231
2023-10-11      1  1.2292
2023-10-10      1  1.2249
2023-10-09      1  1.2172
$ python frankfurter.py -d 2023-01-31
Date          USD    INR
----------  -----  -----
2023-01-31      1  81.82
2023-01-30      1  81.53
2023-01-27      1  81.61
2023-01-26      1  81.53
2023-01-25      1  81.57
2023-01-24      1  81.62
2023-01-23      1  81.36

Hints

You can verify your solution using:

%verify_problem frankfurter

%%file frankfurter.py
# your code here


import requests

def get_exchange_rate(start, end):
    url = f"https://api.frankfurter.app/{start}..{end}"
    params = {"base" : "USD", "to":"INR"}
    r = requests.get(url, params=params)
    return r.json()
    
get_exchange_rate("2024-01-01", "2024-01-31")
{'amount': 1.0,
 'base': 'USD',
 'start_date': '2024-01-02',
 'end_date': '2024-01-31',
 'rates': {'2024-01-02': {'INR': 83.32},
  '2024-01-03': {'INR': 83.3},
  '2024-01-04': {'INR': 83.24},
  '2024-01-05': {'INR': 83.15},
  '2024-01-08': {'INR': 83.11},
  '2024-01-09': {'INR': 83.11},
  '2024-01-10': {'INR': 83.03},
  '2024-01-11': {'INR': 83.02},
  '2024-01-12': {'INR': 82.93},
  '2024-01-15': {'INR': 82.87},
  '2024-01-16': {'INR': 83.09},
  '2024-01-17': {'INR': 83.14},
  '2024-01-18': {'INR': 83.15},
  '2024-01-19': {'INR': 83.08},
  '2024-01-22': {'INR': 83.13},
  '2024-01-23': {'INR': 83.12},
  '2024-01-24': {'INR': 83.11},
  '2024-01-25': {'INR': 83.13},
  '2024-01-26': {'INR': 83.13},
  '2024-01-29': {'INR': 83.16},
  '2024-01-30': {'INR': 83.13},
  '2024-01-31': {'INR': 83.05}}}
  1. You will need typer moduke to get various command line option
  2. https://notes.pipal.in/2023/perfios-python/cookbook/tabulate.html
  3. https://notes.pipal.in/2023/perfios-python/cookbook/dates.html
  4. baseurl/hosturl in frankfurter api is https://api.frankfurter.app