dataretrieval.utils
Useful utilities for data munging.
- class dataretrieval.utils.BaseMetadata(response)[source]
Base class for metadata.
- url
Response url
- Type:
str
- query_time
Response elapsed time
- Type:
datetme.timedelta
- header
Response headers
- Type:
requests.structures.CaseInsensitiveDict
- __init__(response) None [source]
Generates a standard set of metadata informed by the response.
- Parameters:
response (Response) – Response object from requests module
- Returns:
md – A
dataretrieval
customdataretrieval.utils.BaseMetadata
object.- Return type:
- __weakref__
list of weak references to the object (if defined)
- exception dataretrieval.utils.NoSitesError(url)[source]
Custom error class used when selection criteria returns no sites/data.
- __weakref__
list of weak references to the object (if defined)
- dataretrieval.utils.format_datetime(df, date_field, time_field, tz_field)[source]
Creates a datetime field from separate date, time, and time zone fields.
Assumes ISO 8601.
- Parameters:
df (
pandas.DataFrame
) – A data frame containing date, time, and timezone fields.date_field (string) – Name of date column in df.
time_field (string) – Name of time column in df.
tz_field (string) – Name of time zone column in df.
- Returns:
df – The data frame with a formatted ‘datetime’ column
- Return type:
pandas.DataFrame
- dataretrieval.utils.query(url, payload, delimiter=',', ssl_check=True)[source]
Send a query.
Wrapper for requests.get that handles errors, converts listed query parameters to comma separated strings, and returns response.
- Parameters:
url (string) – URL to query
payload (dict) – query parameters passed to
requests.get
delimiter (string) – delimiter to use with lists
ssl_check (bool) – If True, check SSL certificates, if False, do not check SSL, default is True
- Returns:
string – The response from the API query
requests.get
function call.- Return type:
query response
- dataretrieval.utils.to_str(listlike, delimiter=',')[source]
Translates list-like objects into strings.
- Parameters:
listlike (list-like object) – An object that is a list, or list-like (e.g.,
pandas.core.series.Series
)delimiter (string, optional) – The delimiter that is placed between entries in listlike when it is turned into a string. Default value is a comma.
- Returns:
listlike – The listlike object as string separated by the delimiter
- Return type:
string
Examples
>>> dataretrieval.utils.to_str([1, 'a', 2]) '1,a,2' >>> dataretrieval.utils.to_str([0, 10, 42], delimiter='+') '0+10+42'