Skip to content

Commit

Permalink
Added pandas dataframe to PriceReader data input.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Frost committed Dec 26, 2022
1 parent 0dd736c commit 69848d5
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions blankly/data/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DataTypes(Enum):
class FileTypes(Enum):
csv = 'csv'
json = 'json'
df = 'df'


class DataReader:
Expand Down Expand Up @@ -83,19 +84,37 @@ def _convert_to_list(file_path, symbol):
Primarily for CSV prices, this allows single or multi sets of symbols to be converted into matching arrays
"""
# Turn it into a list
if isinstance(file_path, str):
if isinstance(file_path, str) or isinstance(file_path, pd.DataFrame):
file_paths = [file_path]
else:
file_paths = file_path

if isinstance(symbol, str):
if isinstance(symbol, str) or isinstance(file_path, pd.DataFrame):
symbols = [symbol]
else:
# Could be None still
symbols = symbol

return file_paths, symbols

def _parse_df_prices(self, file_paths: list, symbols: list, columns: set) -> None:

if symbols is None:
raise LookupError("Must pass one or more symbols to identify the DataFrame")
if len(file_paths) != len(symbols):
raise LookupError(f"Mismatching symbol & file path lengths, got {len(file_paths)} and {len(symbols)} for "
f"file paths and symbol lengths.")

for index in range(len(file_paths)):

self._check_length(file_paths[index], file_paths[index])

# Check if its contained
assert (columns.issubset(file_paths[index].columns))

# Now push it directly into the dataset and sort by time
self._internal_dataset[symbols[index]] = file_paths[index].sort_values('time')

def _parse_csv_prices(self, file_paths: list, symbols: list, columns: set) -> None:
if symbols is None:
raise LookupError("Must pass one or more symbols to identify the csv files")
Expand Down Expand Up @@ -142,7 +161,9 @@ def complain_if_different(ending_: str, type_: str):
raise LookupError("Cannot pass both csv files and json files into a single constructor.")

for file_path in file_paths:
if file_path[-3:] == 'csv':
if isinstance(file_path, pd.DataFrame):
complain_if_different('df', FileTypes.df.value)
elif file_path[-3:] == 'csv':
complain_if_different('csv', FileTypes.csv.value)
elif file_path[-4:] == 'json':
# In this instance the symbols should be None
Expand Down Expand Up @@ -195,7 +216,9 @@ def __init__(self, file_path: [str, list], symbol: [str, list]):

super().__init__(data_type)

if data_type == FileTypes.json.value:
if data_type == FileTypes.df.value:
self._parse_df_prices(file_paths, symbols, {'open', 'high', 'low', 'close', 'volume', 'time'})
elif data_type == FileTypes.json.value:
self._parse_json_prices(file_paths, ('open', 'high', 'low', 'close', 'volume', 'time'))
elif data_type == FileTypes.csv.value:
self._parse_csv_prices(file_paths, symbols, {'open', 'high', 'low', 'close', 'volume', 'time'})
Expand Down

0 comments on commit 69848d5

Please sign in to comment.