Code Monkey home page Code Monkey logo

tcmb-py's Introduction

tcmb

PyPI Latest Release License Downloads Python Version Code style: black Package workflow

tcmb is a Python API wrapper around the Central Bank of the Republic of Türkiye (TCMB) Web Service. It is an unofficial open-source Python package intended for personal use (Disclaimer).


tcmb, Türkiye Cumhuriyeti Merkez Bankası (TCMB) Web Servisi'ne Python aracılığıyla erişimi sağlayan resmi olmayan API uygulamasıdır. Kişisel kullanım ve araştırma maksadıyla hazırlanmıştır (Elektronik Veri Dağıtım Sistemi (EVDS) Kullanım Koşulları).

Quickstart

pip install tcmb
import tcmb

client = tcmb.Client(api_key="...")

data = client.read(series="TP.DK.USD.S.YTL")

Overview

Data Hierarchy

  1. Categories:

Categories are at the top level of the TCMB data hierarchy.

client = tcmb.Client(api_key="...")

# show categories
Client.categories

{'CATEGORY_ID': 1.0,
 'TOPIC_TITLE_ENG': 'MARKET STATISTICS',
 'TOPIC_TITLE_TR': 'PİYASA VERİLERİ'}

It is also possible to get the same information using the client.get_categories_metadata() method.

  1. Data Groups:

Each category consists of a number of data groups.

client = tcmb.Client(api_key="...")

# show data groups
Client.datagroups

[{'DATAGROUP_CODE': 'bie_pyrepo',
  'CATEGORY_ID': 1,
  'DATAGROUP_NAME': 'Açık Piyasa Repo ve Ters Repo İşlemleri',
  'DATAGROUP_NAME_ENG': 'Open Market Repo and Reverse Repo Transactions',
  ...}
 {'DATAGROUP_CODE': 'bie_mkbral',
  'CATEGORY_ID': 0,
  'DATAGROUP_NAME': 'Altın Borsası İşlemleri-İstanbul (TL-ABD Doları)(Arşiv)',
  'DATAGROUP_NAME_ENG': 'Istanbul Gold Exchange (TRY-USD)(Archive)',
  ...}]

It is also possible to filter the datagroups metadata using the client.get_datagroups_metadata() method.

  1. Series

Datagroups consist of time series, each having a series key such as TP.YSSK.A1 or TP.DK.USD.S.YTL. Series is read using the .read() method.

import tcmb

client = tcmb.Client(api_key="...")

# read one time series
data = client.read("TP.YSSK.A1")

# read multiple time series
data = client.read(["TP.YSSK.A1", "TP.YSSK.A2", "TP.YSSK.A3"])

A convenient way to read time series without initializing the Client instance is using the read() function in the core.py module.

import tcmb

# read one time series
data = tcmb.read("TP.YSSK.A1", api_key="...")

# read multiple time series
data = tcmb.read(["TP.YSSK.A1", "TP.YSSK.A2", "TP.YSSK.A3"], api_key="...")

Series metadata can be fetched with .get_series_metadata() method.

# show metadata of each series within a data group
client.get_series_metadata(datagroup="bie_yssk")

[{'SERIE_CODE': 'TP.YSSK.A1',
  'DATAGROUP_CODE': 'bie_yssk',
  'SERIE_NAME': '1-2 Yıl(ABD doları)',
  'SERIE_NAME_ENG': '1-2 Years(USD)',
  ...},
  {'SERIE_CODE': 'TP.YSSK.A2',
  'DATAGROUP_CODE': 'bie_yssk',
  'SERIE_NAME': '3 Yıl(ABD doları)',
  'SERIE_NAME_ENG': '3 Years(USD)',
  ...}]

# show metadata of a specific time series
client.get_series_metadata(series="TP.YSSK.A1")

[{'SERIE_CODE': 'TP.YSSK.A1',
  'DATAGROUP_CODE': 'bie_yssk',
  'SERIE_NAME': '1-2 Yıl(ABD doları)',
  'SERIE_NAME_ENG': '1-2 Years(USD)',
  ...}]

Wildcard Characters

The wildcard characters are represented as an asterisk * or a question mark ?. The asterisk * represents any number of characters, while the question mark ? represents a single character. Additionally, omitting the value has the same effect as using an asterisk. Note that, wildcard character option is not a feature of TCMB web service. Wildcard pattern search is implemented within the tcmb package and depends on the package data.

>>> data = tcmb.read("TP.DK.USD.*.YTL")
>>> print(data.columns)

Index(['TP_DK_USD_A_YTL', 'TP_DK_USD_S_YTL', 'TP_DK_USD_C_YTL',
       'TP_DK_USD_A_EF_YTL', 'TP_DK_USD_S_EF_YTL'],
      dtype='object')

Installation

pip install tcmb

Authentication

An API key is required to access the Web Service. Users can sign up from the login page. Once logged in, API Key is generated from the Profile page.

There are two ways of providing API key to the tcmb client.

  • Using environment variables:
$ export TCMB_API_KEY="..."
import os
os.environ["TCMB_API_KEY"] = "..."
  • Passing api_key when initializing the Client class.
client = Client(api_key="...")

Disclaimer

tcmb is an unofficial open-source package intended for personal use and research purposes. Please see TCMB's EVDS Disclaimer for the official terms of use of the EVDS Web Service.

tcmb-py's People

Contributors

kaymal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

stkayl ogorkem

tcmb-py's Issues

ValueError when requesting weekly interest rates

Hi,

In 0.4.0 version I didn't encounter any problem while getting monthly and daily series but weeklies raise this ValueError:

>>> import tcmb
>>> clien = tcmb.Client(api_key = MY_API_KEY)
>>> clien.read('TP.KTFTUK')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\anaconda3\Lib\site-packages\tcmb\core.py", line 294, in read
    df = read(
         ^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\tcmb\core.py", line 159, in read
    data = utils.to_dataframe(res.json()["items"])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\tcmb\utils.py", line 71, in to_dataframe
    df = df.astype(float)
         ^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\generic.py", line 6324, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\internals\managers.py", line 451, in astype
    return self.apply(
           ^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\internals\managers.py", line 352, in apply
    applied = getattr(b, f)(**kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\internals\blocks.py", line 511, in astype
    new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\dtypes\astype.py", line 242, in astype_array_safe
    new_values = astype_array(values, dtype, copy=copy)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\dtypes\astype.py", line 187, in astype_array
    values = _astype_nansafe(values, dtype, copy=copy)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\dtypes\astype.py", line 138, in _astype_nansafe
    return arr.astype(dtype, copy=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: '1970-1'

Thanks

TypeError while importing tcmb 0.4.0

import tcmb

raises this error in Python 3.9.12;


def wildcard_search(
pattern: str, items: list | None = None, use_package_data: bool = True
) -> list:
 """Search for items using regex pattern that can contain wildcard characters.
 
Parameters
(...)

 """
if items is None:

TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

This syntax is only supported for Python 3.10 or later.

Can you please replace this function parts with typing.Optional for Python version compatibility ? (example)

Since I got a live app that works depending on your package, I can at least create a PR if you let me know.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.