Skip to content

Commit

Permalink
PLAT-1361: Allow for pandas timestamps to be passed in API
Browse files Browse the repository at this point in the history
  • Loading branch information
victoreram committed Dec 16, 2024
1 parent f4235f3 commit 73ccc43
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 7 additions & 1 deletion coinmetrics/_data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,20 +983,26 @@ def _helper_to_list(data_collection: DataCollection) -> List[Dict[str, Any]]:
return data_collection.to_list()

@staticmethod
def parse_date(date_input: Union[datetime, date, str]) -> datetime:
def parse_date(date_input: Union[datetime, date, str, pd.Timestamp]) -> datetime:
"""
Parses a datetime object or datetime string into a datetime object. Datetime string must be a valid
ISO 8601 format. Timezone aware objects are converted to UTC
:param date_input: Union[datetime, date, str] date to parse into datetime
:return: datetime
"""
# pd.Timestamp is a subset of datetime
if isinstance(date_input, pd.Timestamp):
date_input = date_input.to_pydatetime()

if isinstance(date_input, datetime):
if date_input.tzname() is None:
return date_input
else:
return date_input.astimezone(timezone.utc).replace(tzinfo=None)

if isinstance(date_input, date):
return datetime(date_input.year, date_input.month, date_input.day)

formats = [
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H%M%S",
Expand Down
15 changes: 13 additions & 2 deletions coinmetrics/_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pathlib
from datetime import date, datetime
from datetime import date, datetime, timezone
from enum import Enum
from functools import wraps
from logging import getLogger
from os.path import expanduser
from time import sleep
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, Set
from coinmetrics._typing import FilePathOrBuffer, UrlParamTypes
from pandas import Timestamp

logger = getLogger("cm_client_utils")

Expand All @@ -18,7 +19,17 @@ def transform_url_params_values_to_str(
for param_name, param_value in params.items():
if param_value is None:
continue
if isinstance(param_value, (datetime, date)):
if isinstance(param_value, (datetime, date, Timestamp)):
if isinstance(param_value, Timestamp):
param_value = param_value.to_pydatetime()

if isinstance(param_value, datetime):
param_value = param_value.astimezone(timezone.utc).replace(tzinfo=None)

if isinstance(param_value, date) and not isinstance(param_value, datetime):
param_value = datetime(param_value.year, param_value.month, param_value.day)

assert isinstance(param_value, datetime)
if param_name.endswith("_time"):
processed_params[param_name] = param_value.isoformat()
else:
Expand Down
17 changes: 17 additions & 0 deletions test/test_to_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,22 @@ def test_transaction_tracker_catalog() -> None:
assert len(data) >= 2


@pytest.mark.skipif(not cm_api_key_set, reason=REASON_TO_SKIP)
def test_pandas_timestamp() -> None:
list_timestamp_objects = [
pd.Timestamp(2024, 1, 1),
pd.Timestamp(2024, 1, 1, 12, 0, 0),
pd.Timestamp(2024, 1, 1, 12, 0, 0).tz_localize('US/Eastern'),
pd.Timestamp(2024, 1, 1, 0, 0, 0).tz_localize('UTC')
]
for ts in list_timestamp_objects:
data = client.get_asset_metrics(
'btc',
'ReferenceRateUSD',
start_time=ts,
limit_per_asset=1
).to_dataframe()


if __name__ == "__main__":
pytest.main()

0 comments on commit 73ccc43

Please sign in to comment.