Skip to content

Commit

Permalink
Merge pull request #263 from hydrosquall/feature/validate-multi-ticke…
Browse files Browse the repository at this point in the history
…r-pandas-arguments

[fix] Validate multi-ticker pandas arguments
  • Loading branch information
hydrosquall authored May 5, 2019
2 parents e39b975 + 03e7b53 commit 762090b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ History
0.10.x (2019-XX-XX)
------------------
* Documentation: Added a "Peer Comparison Analysis" Jupyter Notebook under "/examples" (@i3creations #197)
* Minor: Update error message to clarify multiple tickers only work with single metrics
* Updated development dependencies


0.9.x (2019-01-30)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tiingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import vcr

from tiingo import TiingoClient
from tiingo.api import InvalidFrequencyError
from tiingo.exceptions import InvalidFrequencyError
from tiingo.restclient import RestClientError


Expand Down
6 changes: 5 additions & 1 deletion tests/test_tiingo_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import vcr
from unittest import TestCase
from tiingo import TiingoClient
from tiingo.api import APIColumnNameError, InstallPandasException
from tiingo.exceptions import APIColumnNameError, InstallPandasException, MissingRequiredArgumentError
try:
import pandas as pd
pandas_is_installed = True
Expand Down Expand Up @@ -69,6 +69,10 @@ def test_metric_name_column_error(self):
self._client.get_dataframe(['GOOGL', 'AAPL'], startDate='2018-01-05',
endDate='2018-01-19', metric_name='xopen', frequency='weekly')

def test_metric_name_missing_when_multiple_tickers(self):
with self.assertRaises(MissingRequiredArgumentError):
self._client.get_dataframe(['GOOGL', 'AAPL'], frequency='weekly')

@vcr.use_cassette('tests/fixtures/ticker_price_pandas_single.yaml')
def test_pandas_edge_case(self):
"""Test single price/date being returned as a frame"""
Expand Down
36 changes: 18 additions & 18 deletions tiingo/api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# -*- coding: utf-8 -*-

from collections import namedtuple
import csv
import json
import os
import re
import sys
import pkg_resources
import csv
import json
from collections import namedtuple
from zipfile import ZipFile
from tiingo.restclient import RestClient

import requests
import re

from tiingo.restclient import RestClient
from tiingo.exceptions import (
InstallPandasException,
APIColumnNameError,
InvalidFrequencyError,
MissingRequiredArgumentError)

try:
import pandas as pd
Expand Down Expand Up @@ -49,17 +56,6 @@ def dict_to_object(item, object_name):
object_hook=lambda d:
namedtuple(object_name, fields)(*values))

class InstallPandasException(Exception):
pass


class APIColumnNameError(Exception):
pass


class InvalidFrequencyError(Exception):
pass


class TiingoClient(RestClient):
"""Class for managing interactions with the Tiingo REST API
Expand Down Expand Up @@ -227,12 +223,16 @@ def get_dataframe(self, tickers,
frequency (string): Resample frequency (defaults to daily).
"""

valid_columns = ['open', 'high', 'low', 'close', 'volume', 'adjOpen', 'adjHigh', 'adjLow',
'adjClose', 'adjVolume', 'divCash', 'splitFactor']
valid_columns = {'open', 'high', 'low', 'close', 'volume', 'adjOpen', 'adjHigh', 'adjLow',
'adjClose', 'adjVolume', 'divCash', 'splitFactor'}

if metric_name is not None and metric_name not in valid_columns:
raise APIColumnNameError('Valid data items are: ' + str(valid_columns))

if metric_name is None and isinstance(tickers, list):
raise MissingRequiredArgumentError("""When tickers is provided as a list, metric_name is a required argument.
Please provide a metric_name, or call this method with one ticker at a time.""")

params = {
'format': 'json',
'resampleFreq': frequency
Expand Down
14 changes: 14 additions & 0 deletions tiingo/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Exception Clasess
class InstallPandasException(Exception):
pass


class APIColumnNameError(Exception):
pass


class InvalidFrequencyError(Exception):
pass

class MissingRequiredArgumentError(Exception):
pass

0 comments on commit 762090b

Please sign in to comment.