man-c / pycoingecko

Python wrapper for the CoinGecko API
MIT License
1.04k stars 268 forks source link

Support for Python native datetime date object for the api mapped to methods #38

Closed amitgaru2 closed 3 years ago

amitgaru2 commented 3 years ago

While calling any API (method), instead of matching exact string format of date on the API side, lets support for Python native datetime's date instead.

man-c commented 3 years ago

That would be a nice feature, but there seems to be an inconsistency in the date formats of the API. For example in /coins/{id}/history the date is dd-mm-yyyy (eg. 30-12-2017), while in /events (from_date and to_date) the request format is yyyy-mm-dd. I believe it is best to have it as it is. Users can easily work with datetime and if the API changes the format in an endpoint the package would not be required to update.

Cheers

cibic89 commented 3 years ago

Hi, getting this first value as date for bitcoin 1367193600000, what format is this?

UPDATE: found the answer, the last 3 digits need to be removed and the unit read should be seconds on unix time

snippet:

import pandas as pd
import numpy as np
from pycoingecko import CoinGeckoAPI

# Pandas annoyances fix
pd.options.display.max_rows = 1000
pd.options.display.max_columns = 100
pd.options.display.precision = 2
pd.options.display.float_format = '{:,.2f}'.format
pd.options.display.max_colwidth = 0

cg = CoinGeckoAPI()

# The function
def get_historic_data(coin_id: str) -> pd.DataFrame:
    df_out = pd.DataFrame(cg.get_coin_market_chart_by_id(coin_id, vs_currency='usd', days='max'))
    index_series = df_out['prices'].str[0].astype(str)
    index_series = index_series.str[:-3].astype(int)
    df_out = df_out.apply(lambda x: x.str[-1])
    df_out.index = pd.to_datetime(index_series, unit='s')
    return df_out

bitcoin_df = get_historic_data('bitcoin')
man-c commented 3 years ago

Hi! The 1367193600000 is timestamp in milliseconds (you can check it easily in https://www.epochconverter.com/). Removing the last 3 digits converts it to seconds on unix time, as you already found.

cibic89 commented 3 years ago

Ευχαριστώ σε παρέα μου, δυνατο το wrapper σου

Hi! The 1367193600000 is timestamp in milliseconds (you can check it easily in https://www.epochconverter.com/). Removing the last 3 digits converts it to seconds on unix time, as you already found.