Merge branch 'develop' into feature/remove-pyupgrade

This commit is contained in:
Colin Delahunty
2023-02-07 14:07:24 -05:00
committed by GitHub
4 changed files with 2859 additions and 3136 deletions

View File

@@ -1335,6 +1335,8 @@ def ask_file_overwrite(file_path: str) -> Tuple[bool, bool]:
# Jeroen asked for a flag to overwrite no matter what
if obbff.FILE_OVERWITE:
return False, True
if os.environ.get("TEST_MODE") == "True":
return False, True
if os.path.exists(file_path):
overwrite = input("\nFile already exists. Overwrite? [y/n]: ").lower()
if overwrite == "y":
@@ -1415,6 +1417,9 @@ def export_data(
df.reset_index(drop=True, inplace=True)
df.to_json(saved_path)
elif exp_type.endswith("xlsx"):
# since xlsx does not support datetimes with timezones we need to remove it
df = remove_timezone_from_dataframe(df)
if sheet_name is None:
exists, overwrite = ask_file_overwrite(saved_path)
if exists and not overwrite:
@@ -1461,9 +1466,9 @@ def export_data(
return
plt.savefig(saved_path)
else:
console.print("\nWrong export file specified.")
console.print("Wrong export file specified.")
console.print(f"\nSaved file: {saved_path}")
console.print(f"Saved file: {saved_path}")
def get_rf() -> float:
@@ -2017,3 +2022,45 @@ def request(url: str, method="GET", **kwargs) -> requests.Response:
url, headers=headers, timeout=cfg.REQUEST_TIMEOUT, **kwargs
)
raise ValueError("Method must be GET or POST")
def remove_timezone_from_dataframe(df: pd.DataFrame) -> pd.DataFrame:
"""
Remove timezone information from a dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to remove timezone information from
Returns
-------
pd.DataFrame
The dataframe with timezone information removed
"""
date_cols = []
index_is_date = False
# Find columns and index containing date data
if (
df.index.dtype.kind == "M"
and hasattr(df.index.dtype, "tz")
and df.index.dtype.tz is not None
):
index_is_date = True
for col, dtype in df.dtypes.items():
if dtype.kind == "M" and hasattr(df.index.dtype, "tz") and dtype.tz is not None:
date_cols.append(col)
# Remove the timezone information
for col in date_cols:
df[col] = df[col].dt.date
if index_is_date:
index_name = df.index.name
df.index = df.index.date
df.index.name = index_name
return df

View File

@@ -1,4 +1,5 @@
# IMPORTATION STANDARD
import datetime
from pathlib import Path
# IMPORTATION THIRDPARTY
@@ -6,7 +7,11 @@ import pandas as pd
import pytest
# IMPORTATION INTERNAL
from openbb_terminal.helper_funcs import check_start_less_than_end, export_data
from openbb_terminal.helper_funcs import (
check_start_less_than_end,
export_data,
remove_timezone_from_dataframe,
)
# pylint: disable=E1101
# pylint: disable=W0603
@@ -107,3 +112,47 @@ def test_start_less_than_end():
assert check_start_less_than_end(None, "2022-01-01") is False
assert check_start_less_than_end("2022-01-02", None) is False
assert check_start_less_than_end(None, None) is False
@pytest.mark.parametrize(
"df, df_expected",
[
(
pd.DataFrame(
{
"date1": pd.date_range("2021-01-01", periods=5, tz="Europe/London"),
"date2": pd.date_range("2022-01-01", periods=5, tz="Europe/London"),
"value1": [1, 2, 3, 4, 5],
"value2": [6, 7, 8, 9, 10],
}
),
pd.DataFrame(
{
"date1": [
datetime.date(2021, 1, 1),
datetime.date(2021, 1, 2),
datetime.date(2021, 1, 3),
datetime.date(2021, 1, 4),
datetime.date(2021, 1, 5),
],
"date2": [
datetime.date(2022, 1, 1),
datetime.date(2022, 1, 2),
datetime.date(2022, 1, 3),
datetime.date(2022, 1, 4),
datetime.date(2022, 1, 5),
],
"value1": [1, 2, 3, 4, 5],
"value2": [6, 7, 8, 9, 10],
}
),
),
],
)
def test_remove_timezone_from_dataframe(df, df_expected):
# set date1 as index
df.set_index("date1", inplace=True)
df_expected.set_index("date1", inplace=True)
df_result = remove_timezone_from_dataframe(df)
assert df_result.equals(df_expected)