Add a sane pandas.DataFrame to recarray converter
parent
0b5af4b590
commit
14bff66ec5
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue