Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Added generic input support for Pandas DataFrames and List Structures #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

flipdazed
Copy link

@flipdazed flipdazed commented Sep 5, 2017

This allows users to easily take advantage of arbitrary data structures as inputs if they are already loaded into memory e.g. for creating custom stressed scenarios

Example

import pandas as pd
import numpy as np
vals = np.array([['2015-08-14 09:06:00', 0.0069, 0.0069, 0.0069, 0.0069, 1.34611713,
        9567],
       ['2015-08-14 09:10:00', 0.0069, 0.0069, 0.0069, 0.0069, 0.37540654,
        8751],
       ['2015-08-14 09:13:00', 0.0069, 0.0069, 0.0069, 0.0069, 0.18775374,
        7758],
       ['2015-08-14 09:23:00', 0.0069, 0.006959999999999999,
        0.006959999999999999, 0.0069, 0.09382613, 4662],
       ['2015-08-14 09:25:00', 0.006959999999999999, 0.00716, 0.00716,
        0.006959999999999999, 4.98869811, 9409]], dtype=object)
cols = [u'Date Time', u'Open', u'Close', u'High', u'Low',
       u'Volume', u'Adj Close']
df = pd.DataFrame(vals, columns=cols)
df = df.convert_objects()

# resample irregular data to regular time series
df['Date Time'] = pd.to_datetime(df['Date Time'])
df = df.set_index('Date Time').resample('s').interpolate().resample('5T').asfreq().dropna().reset_index()

from pyalgotrade.barfeed import customfeed
feed = customfeed.Feed(Frequency.MINUTE*5)
feed.addBarsFromDataFrame('test', df)

@flipdazed
Copy link
Author

It seems that the failures aren't related to my code as indicated by the node coverage report

pyalgotrade/barfeed/customfeed.py 82 82 0%

Also I've improved the code-quality by removing the two libraries that were not longer needed
Let me know your thoughts!

Copy link

@9590 9590 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@flipdazed
Copy link
Author

is there anything more I need to do on this to get it merged?



# Interface for csv row parsers.
class RowParser(object):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This RowParser class is not used, and it can be removed.

if missing_columns:
raise ValueError('Missing required columns: {}'.format(repr(missing_columns)))

df = df[self.__columnNames.values()]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will prevent extra columns from being included on each bar.

@gbeced
Copy link
Owner

gbeced commented Mar 1, 2018

  • I'd recommend creating two separate modules:
    • pandasfeed
    • listfeed
  • The dataframe seems to be getting converted to strings, and then it will be converted back to floats and datetimes in csvfeed.GenericRowParser.
  • Please add testcases

@reijmer
Copy link

reijmer commented Dec 21, 2019

Any chance this will be merged in the near future?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants