From 14bff66ec5841fb9c67b7b5bc2da1f71e9fafd82 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 17 Jun 2020 19:22:37 -0400 Subject: [PATCH] Add a sane pandas.DataFrame to recarray converter --- piker/ui/_source.py | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/piker/ui/_source.py b/piker/ui/_source.py index a23c9f41..f0380b72 100644 --- a/piker/ui/_source.py +++ b/piker/ui/_source.py @@ -63,30 +63,24 @@ def from_df( ): """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.insert(0, 'id', df.index) - array['time'] = np.array([d.timestamp().time for d in df.Date]) + df['Date'] = [d.timestamp() for d in df.Date] # try to rename from some camel case - df = df.rename( - columns={ - # 'Date': 'time', - 'Open': 'open', - 'High': 'high', - 'Low': 'low', - 'Close': 'close', - 'Volume': 'volume', - } - ) - for name in array.dtype.names: - array[name] = df[name] - - array[:] = df[:] - + columns={ + 'Date': 'time', + 'Open': 'open', + 'High': 'high', + 'Low': 'low', + 'Close': 'close', + 'Volume': 'volume', + } + for name in df.columns: + if name not in columns: + del df[name] + df = df.rename(columns=columns) + # df.insert(0, 'id', df.index) + array = df.to_records() _nan_to_closest_num(array) # self._set_time_frame(default_tf)