Add a sane pandas.DataFrame to recarray converter

bar_select
Tyler Goodlet 2020-06-17 19:22:37 -04:00
parent 0b5af4b590
commit 14bff66ec5
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:
for name in array.dtype.names: if name not in columns:
array[name] = df[name] del df[name]
df = df.rename(columns=columns)
array[:] = df[:] # df.insert(0, 'id', df.index)
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)