Skip to content

Commit

Permalink
Add support for polars dataframes
Browse files Browse the repository at this point in the history
  • Loading branch information
gw-moore committed Jan 20, 2025
1 parent 597360a commit d451081
Show file tree
Hide file tree
Showing 71 changed files with 17,245 additions and 571,209 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@

## Documentation

The [documentation](https://pyfredapi.readthedocs.io/en/latest/) is made with [Sphinx](https://www.sphinx-doc.org/en/master/) and hosted on [Read the Docs](https://readthedocs.org/).
The [documentation](https://pyfredapi.readthedocs.io/en/latest/) is made with [MkDocs](https://www.mkdocs.org/) and hosted on [Read the Docs](https://readthedocs.org/).

## Installation

Install the latest version with pip:

```bash
pip install pyfredapi
```

Install pyfredapi with all optional dependencies.

```bash
pip install 'pyfredapi[all]'
```

# install with plotting dependencies
pip install 'pyfredapi[plot]'
You can also install a subset of all optional dependencies.

```bash
pip install 'pyfredapi[polars]'
```

## Quick Start
Expand Down
10 changes: 10 additions & 0 deletions docs/references/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Version 0.9.2 - 2024-11-03

### Added

- Support for [polars](https://pola.rs/) as a return format.

### Changed

- Updated optional dependencies.

## Version 0.9.1 - 2024-11-02

### Removed
Expand Down
150 changes: 26 additions & 124 deletions docs/tutorials/category.ipynb

Large diffs are not rendered by default.

327 changes: 131 additions & 196 deletions docs/tutorials/series_collection.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyfredapi/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Define the package metadata."""

__version__ = "0.9.1"
__version__ = "0.9.2.dev0"
29 changes: 17 additions & 12 deletions pyfredapi/category.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""The category module provides functions to request data from the [FRED API Categories endpoints](https://fred.stlouisfed.org/docs/api/fred/#Categories)."""

from __future__ import annotations

from typing import Dict, Literal, Optional

from pydantic import BaseModel, ConfigDict, PositiveInt

from ._base import _get_request
from .series import SeriesInfo
from .utils import _convert_pydantic_model_to_frozenset
from .utils._convert_to_df import _convert_to_pandas, _convert_to_polars
from .utils.enums import ReturnFormat

from .utils._common_type_hints import (
ApiKeyType,
JsonOrPdType,
ReturnTypes,
JsonType,
KwargsType,
ReturnFmtType,
ReturnFormats,
)
from .utils._convert_to_pandas import _convert_to_pandas
from .utils.enums import ReturnFormat


class CategoryApiParameters(BaseModel):
Expand Down Expand Up @@ -190,9 +193,9 @@ def get_category_series(
def get_category_tags(
category_id: Optional[int] = None,
api_key: ApiKeyType = None,
return_format: ReturnFmtType = "json",
return_format: ReturnFormats = "json",
**kwargs: KwargsType,
) -> JsonOrPdType:
) -> ReturnTypes:
"""Get the FRED tags for a category by category ID. [Endpoint documentation](https://fred.stlouisfed.org/docs/api/fred/category_tags.html).
Parameters
Expand All @@ -208,8 +211,7 @@ def get_category_tags(
Returns
-------
dict | pd.DataFrame
Either a dictionary representing the json response or a pandas dataframe.
dict | pd.DataFrame | pl.DataFrame
"""
return_format = ReturnFormat(return_format)
Expand All @@ -225,15 +227,17 @@ def get_category_tags(

if return_format == ReturnFormat.pandas:
return _convert_to_pandas(response["tags"])
if return_format == ReturnFormat.polars:
return _convert_to_polars(response["tags"])
return response


def get_category_related_tags(
category_id: Optional[int] = None,
api_key: ApiKeyType = None,
return_format: ReturnFmtType = "json",
return_format: ReturnFormats = "json",
**kwargs: KwargsType,
) -> JsonOrPdType:
) -> ReturnTypes:
"""Get the related FRED tags for a category by category ID. [Endpoint documentation](https://fred.stlouisfed.org/docs/api/fred/category_related_tags.html).
Parameters
Expand All @@ -249,8 +253,7 @@ def get_category_related_tags(
Returns
-------
dict | pd.DataFrame
Either a dictionary representing the json response or a pandas dataframe.
dict | pd.DataFrame | pl.DataFrame
"""
return_format = ReturnFormat(return_format)
Expand All @@ -266,4 +269,6 @@ def get_category_related_tags(

if return_format == ReturnFormat.pandas:
return _convert_to_pandas(response["tags"])
if return_format == ReturnFormat.polars:
return _convert_to_polars(response["tags"])
return response
4 changes: 2 additions & 2 deletions pyfredapi/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from ._base import _get_request
from .utils import _convert_pydantic_model_to_frozenset
from .utils._common_type_hints import ApiKeyType, JsonType, ReturnFmtType
from .utils._common_type_hints import ApiKeyType, JsonType, ReturnFormats
from .utils.enums import ReturnFormat

_geo_fred_url = "https://api.stlouisfed.org/geofred/"
Expand Down Expand Up @@ -133,7 +133,7 @@ def get_geoseries(
api_key: ApiKeyType = None,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
return_format: ReturnFmtType = "pandas",
return_format: ReturnFormats = "pandas",
) -> GeoseriesData:
"""Request a cross section of regional data for a specified release date. If no date is specified, the most recent data available are returned.
Expand Down
Loading

0 comments on commit d451081

Please sign in to comment.