mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 04:52:07 +00:00
Merge pull request #4193 from joey-walker/feature/search-return-df
Return a dataframe from stocks search, removed export to file system (#3923)
This commit is contained in:
@@ -242,10 +242,6 @@ class StocksController(StockBaseController):
|
||||
exchange_country=exchange,
|
||||
all_exchanges=ns_parser.all_exchanges,
|
||||
limit=ns_parser.limit,
|
||||
export=ns_parser.export,
|
||||
sheet_name=" ".join(ns_parser.sheet_name)
|
||||
if ns_parser.sheet_name
|
||||
else None,
|
||||
)
|
||||
|
||||
@log_start_end(log=logger)
|
||||
|
||||
@@ -27,7 +27,6 @@ from scipy import stats
|
||||
|
||||
from openbb_terminal import config_terminal as cfg
|
||||
from openbb_terminal.helper_funcs import (
|
||||
export_data,
|
||||
lambda_long_number_format_y_axis,
|
||||
plot_autoscale,
|
||||
print_rich_table,
|
||||
@@ -106,9 +105,7 @@ def search(
|
||||
exchange_country: str = "",
|
||||
all_exchanges: bool = False,
|
||||
limit: int = 0,
|
||||
export: str = "",
|
||||
sheet_name: Optional[str] = "",
|
||||
) -> None:
|
||||
) -> pd.DataFrame:
|
||||
"""Search selected query for tickers.
|
||||
|
||||
Parameters
|
||||
@@ -127,8 +124,12 @@ def search(
|
||||
Whether to search all exchanges, without this option only the United States market is searched
|
||||
limit : int
|
||||
The limit of companies shown.
|
||||
export : str
|
||||
Export data
|
||||
|
||||
Returns
|
||||
-------
|
||||
df: pd.DataFrame
|
||||
Dataframe of search results.
|
||||
Empty Dataframe if none are found.
|
||||
|
||||
Examples
|
||||
--------
|
||||
@@ -157,10 +158,10 @@ def search(
|
||||
console.print(
|
||||
"[red]No companies were found that match the given criteria.[/red]\n"
|
||||
)
|
||||
return
|
||||
return pd.DataFrame()
|
||||
if not data:
|
||||
console.print("No companies found.\n")
|
||||
return
|
||||
return pd.DataFrame()
|
||||
|
||||
if query:
|
||||
d = fd.search_products(
|
||||
@@ -180,23 +181,18 @@ def search(
|
||||
|
||||
if not d:
|
||||
console.print("No companies found.\n")
|
||||
return
|
||||
return pd.DataFrame()
|
||||
|
||||
df = pd.DataFrame.from_dict(d).T[
|
||||
["long_name", "short_name", "country", "sector", "industry", "exchange"]
|
||||
]
|
||||
if exchange_country:
|
||||
if exchange_country in market_coverage_suffix:
|
||||
suffix_tickers = [
|
||||
ticker.split(".")[1] if "." in ticker else ""
|
||||
for ticker in list(df.index)
|
||||
]
|
||||
df = df[
|
||||
[
|
||||
val in market_coverage_suffix[exchange_country]
|
||||
for val in suffix_tickers
|
||||
]
|
||||
]
|
||||
if exchange_country and exchange_country in market_coverage_suffix:
|
||||
suffix_tickers = [
|
||||
ticker.split(".")[1] if "." in ticker else "" for ticker in list(df.index)
|
||||
]
|
||||
df = df[
|
||||
[val in market_coverage_suffix[exchange_country] for val in suffix_tickers]
|
||||
]
|
||||
|
||||
exchange_suffix = {}
|
||||
for k, v in market_coverage_suffix.items():
|
||||
@@ -230,13 +226,7 @@ def search(
|
||||
title=title,
|
||||
)
|
||||
|
||||
export_data(
|
||||
export,
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"search",
|
||||
df,
|
||||
sheet_name,
|
||||
)
|
||||
return df
|
||||
|
||||
|
||||
def load(
|
||||
|
||||
@@ -263,8 +263,6 @@ def test_call_func_expect_queue(expected_queue, func, queue):
|
||||
industry="",
|
||||
all_exchanges=False,
|
||||
exchange_country="",
|
||||
export="csv",
|
||||
sheet_name=None,
|
||||
),
|
||||
),
|
||||
(
|
||||
|
||||
@@ -50,7 +50,6 @@ def test_search(mocker, use_tab):
|
||||
exchange_country="",
|
||||
all_exchanges=False,
|
||||
limit=5,
|
||||
export="",
|
||||
)
|
||||
|
||||
|
||||
|
||||
25
website/content/sdk/reference/stocks/search.md
vendored
25
website/content/sdk/reference/stocks/search.md
vendored
@@ -10,29 +10,30 @@ Search selected query for tickers.
|
||||
Source Code: [[link](https://github.com/OpenBB-finance/OpenBBTerminal/tree/main/openbb_terminal/stocks/stocks_helper.py#L98)]
|
||||
|
||||
```python
|
||||
openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "")
|
||||
openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Parameters
|
||||
|
||||
| Name | Type | Description | Default | Optional |
|
||||
| ---- | ---- | ----------- | ------- | -------- |
|
||||
| query | str | The search term used to find company tickers | | True |
|
||||
| country | str | Search by country to find stocks matching the criteria | | True |
|
||||
| sector | str | Search by sector to find stocks matching the criteria | | True |
|
||||
| industry | str | Search by industry to find stocks matching the criteria | | True |
|
||||
| exchange_country | str | Search by exchange country to find stock matching | | True |
|
||||
| limit | int | The limit of companies shown. | 0 | True |
|
||||
| export | str | Export data | | True |
|
||||
|
||||
| Name | Type | Description | Default | Optional |
|
||||
|------------------|------|---------------------------------------------------------|---------|----------|
|
||||
| query | str | The search term used to find company tickers | | True |
|
||||
| country | str | Search by country to find stocks matching the criteria | | True |
|
||||
| sector | str | Search by sector to find stocks matching the criteria | | True |
|
||||
| industry | str | Search by industry to find stocks matching the criteria | | True |
|
||||
| exchange_country | str | Search by exchange country to find stock matching | | True |
|
||||
| limit | int | The limit of companies shown. | 0 | True |
|
||||
|
||||
---
|
||||
|
||||
## Returns
|
||||
|
||||
This function does not return anything
|
||||
| Type | Description |
|
||||
|--------------|----------------|
|
||||
| pd.DataFrame | Search results |
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user