Files
OpenBB/gamestonk_terminal/cryptocurrency/dataframe_helpers.py
jp 28e3970912 Refactoring crypto menu 2nd part (#724)
* Add separate view for finbrain for crypto curreny sentiment analysis

* Move json with symbols to separate directory, little refactoring, update readme

* Add screenshot of finbrain for PolkaDot

* Remove not used methods, remove binance_view, binance_model from discovery menu (It's not needed, as we use all_coins view for finding coins from all sources

* Refactoring Due Diligence menu. Changing views, moving argparsers to controller

* Refactor cryptomenu. Overview, Discovery, DD menu - move argparsers to controllers, add typing hints, change names of views/models, adjust tests

* Adjust tests

* Move finbrain and find/coins method to controller

* Move report argparser into controller. Adjust tests

* Adjust docstrings

* Add helper method to take care about retry/wait for coingecko scraping

* Fix failing test

* Adjust newlines, docstrings, cmd descriptions

* Add use tabulate df flag for all crypto views

* Add status code for coingecko

* Add note, that some coingecko commands can fail

Co-authored-by: Jakub Pluta <jakub.pluta@cdq.com>
2021-09-05 17:15:35 +01:00

89 lines
2.0 KiB
Python

"""Dataframe helpers"""
__docformat__ = "numpy"
from typing import Union, Any
import textwrap
import pandas as pd
from gamestonk_terminal.helper_funcs import long_number_format
def wrap_text_in_df(df: pd.DataFrame, w: int = 55) -> pd.DataFrame: # pragma: no cover
"""
Parameters
----------
df: pd.DataFrame
Data Frame with some data
w: int
length of text in column after which text is wrapped into new line
Returns
-------
pd.DataFrame
"""
return df.applymap(
lambda x: "\n".join(textwrap.wrap(x, width=w)) if isinstance(x, str) else x
)
def percent_to_float(s: str) -> float:
"""Helper method to replace string pct like "123.56%" to float 1.2356
Parameters
----------
s: string
string to replace
Returns
-------
float
"""
s = str(float(s.rstrip("%")))
i = s.find(".")
if i == -1:
return int(s) / 100
if s.startswith("-"):
return -percent_to_float(s.lstrip("-"))
s = s.replace(".", "")
i -= 2
if i < 0:
return float("." + "0" * abs(i) + s)
return float(s[:i] + "." + s[i:])
def create_df_index(df: pd.DataFrame, name: str = "rank") -> None:
"""Helper method that creates new index for given data frame, with provided index name
Parameters
----------
df:
pd.DataFrame
name: str
index name
"""
df.index = df.index + 1
df.reset_index(inplace=True)
df.rename(columns={"index": name}, inplace=True)
def long_number_format_with_type_check(x: Union[int, float]) -> Union[str, Any]:
"""Helper which checks if type of x is int or float and it's smaller then 10^18.
If yes it apply long_num_format
Parameters
----------
x: int/float
number to apply long_number_format method
Returns
-------
Union[str, Any]
"""
if isinstance(x, (int, float)) and x < 10 ** 18:
return long_number_format(x)
return x
def replace_underscores_in_column_names(string: str) -> str:
return string.title().replace("_", " ")