Add a sane pandas.DataFrame to recarray converter

its_happening
Tyler Goodlet 2020-06-17 19:22:37 -04:00
parent cdb70d25f3
commit feccadc331
1 changed files with 15 additions and 21 deletions

View File

@ -63,30 +63,24 @@ def from_df(
): ):
"""Cast OHLC ``pandas.DataFrame`` to ``numpy.structarray``. """Cast OHLC ``pandas.DataFrame`` to ``numpy.structarray``.
""" """
# shape = (len(df),)
# array.resize(shape, refcheck=False)
array = np.array([], dtype=OHLC_dtype)
df.reset_index(inplace=True) df.reset_index(inplace=True)
df.insert(0, 'id', df.index) df['Date'] = [d.timestamp() for d in df.Date]
array['time'] = np.array([d.timestamp().time for d in df.Date])
# try to rename from some camel case # try to rename from some camel case
df = df.rename( columns={
columns={ 'Date': 'time',
# 'Date': 'time', 'Open': 'open',
'Open': 'open', 'High': 'high',
'High': 'high', 'Low': 'low',
'Low': 'low', 'Close': 'close',
'Close': 'close', 'Volume': 'volume',
'Volume': 'volume', }
} for name in df.columns:
) if name not in columns:
for name in array.dtype.names: del df[name]
array[name] = df[name] df = df.rename(columns=columns)
# df.insert(0, 'id', df.index)
array[:] = df[:] array = df.to_records()
_nan_to_closest_num(array) _nan_to_closest_num(array)
# self._set_time_frame(default_tf) # self._set_time_frame(default_tf)