Search for specific data based on provided criteria. Allows searching the database based on column names and queries, with optional case sensitivity. Args: **kwargs: Column names and search queries. For example, symbol="TSLA" or sector="
(self, **kwargs: Any)
| 79 | ) from error |
| 80 | |
| 81 | def search(self, **kwargs: Any) -> pd.DataFrame: |
| 82 | """ |
| 83 | Search for specific data based on provided criteria. |
| 84 | |
| 85 | Allows searching the database based on column names and queries, |
| 86 | with optional case sensitivity. |
| 87 | |
| 88 | Args: |
| 89 | **kwargs: Column names and search queries. |
| 90 | For example, symbol="TSLA" or sector="Technology". |
| 91 | case_sensitive (bool): Whether the search should be case-sensitive. |
| 92 | Defaults to False. |
| 93 | only_primary_listing (bool): Whether to exclude secondary listings. |
| 94 | Defaults to False. |
| 95 | index (str): Search within the DataFrame index. |
| 96 | Defaults to None. |
| 97 | exclude_delisted (bool): Whether to exclude delisted entries. |
| 98 | Defaults to True. |
| 99 | |
| 100 | Returns: |
| 101 | DataFrame with filtered data based on the input criteria. |
| 102 | """ |
| 103 | data_filter = self.data.copy() |
| 104 | |
| 105 | if "case_sensitive" in kwargs: |
| 106 | case_sensitive = bool(kwargs["case_sensitive"] in [True, "True"]) |
| 107 | kwargs = {k: v for k, v in kwargs.items() if k != "case_sensitive"} |
| 108 | else: |
| 109 | case_sensitive = False |
| 110 | |
| 111 | for key, value in kwargs.items(): |
| 112 | if key == "only_primary_listing": |
| 113 | if value is True: |
| 114 | # Filter data if exclude exchanges is set to True |
| 115 | data_filter = data_filter[ |
| 116 | ~data_filter.index.str.contains(r"\.", na=False) |
| 117 | ] |
| 118 | elif key == "index": |
| 119 | # Look into the index of the DataFrame and search accordingly |
| 120 | if isinstance(value, list | pd.Index): |
| 121 | data_filter = data_filter[data_filter.index.isin(value)] |
| 122 | else: |
| 123 | data_filter = data_filter[ |
| 124 | data_filter.index.str.contains(value, na=False) |
| 125 | ] |
| 126 | elif key == "exclude_delisted": |
| 127 | if value is True and "delisted" in data_filter.columns: |
| 128 | data_filter = data_filter[~data_filter["delisted"]] |
| 129 | elif key not in data_filter.columns: |
| 130 | print(f"{key} is not a valid column.") |
| 131 | elif isinstance(value, list): |
| 132 | if case_sensitive: |
| 133 | # For case-sensitive search, use string comparison that preserves case |
| 134 | data_filter = data_filter[data_filter[key].isin(value)] |
| 135 | else: |
| 136 | # For case-insensitive search, convert both sides to lowercase |
| 137 | # Create a mask that matches if any value in the list is found in the column |
| 138 | mask = ( |