r"""Convert the market data frame to canonical format. Parameters ---------- df : pandas.DataFrame The intraday dataframe. index_column : str The name of the index column. intraday_data : bool Flag set to True if the frame contains intraday data. Ret
(df, index_column, intraday_data)
| 295 | # |
| 296 | |
| 297 | def convert_data(df, index_column, intraday_data): |
| 298 | r"""Convert the market data frame to canonical format. |
| 299 | |
| 300 | Parameters |
| 301 | ---------- |
| 302 | df : pandas.DataFrame |
| 303 | The intraday dataframe. |
| 304 | index_column : str |
| 305 | The name of the index column. |
| 306 | intraday_data : bool |
| 307 | Flag set to True if the frame contains intraday data. |
| 308 | |
| 309 | Returns |
| 310 | ------- |
| 311 | df : pandas.DataFrame |
| 312 | The canonical dataframe with date/time index. |
| 313 | |
| 314 | """ |
| 315 | |
| 316 | # Standardize column names |
| 317 | df = df.rename(columns = lambda x: x.lower().replace(' ','')) |
| 318 | |
| 319 | # Create the time/date index if not already done |
| 320 | |
| 321 | if not isinstance(df.index, pd.DatetimeIndex): |
| 322 | df.reset_index(inplace=True) |
| 323 | if intraday_data: |
| 324 | dt_column = df['date'] + ' ' + df['time'] |
| 325 | else: |
| 326 | dt_column = df['date'] |
| 327 | df[index_column] = pd.to_datetime(dt_column) |
| 328 | df.set_index(pd.DatetimeIndex(df[index_column]), |
| 329 | drop=True, inplace=True) |
| 330 | del df['date'] |
| 331 | if intraday_data: |
| 332 | del df['time'] |
| 333 | |
| 334 | # Make the remaining columns floating point |
| 335 | |
| 336 | cols_float = ['open', 'high', 'low', 'close', 'volume'] |
| 337 | df[cols_float] = df[cols_float].astype(float) |
| 338 | |
| 339 | # Order the frame by increasing date if necessary |
| 340 | df = df.sort_index() |
| 341 | |
| 342 | return df |
| 343 | |
| 344 | |
| 345 | # |