How to Read Csv With Panda That Has No Headers
Pandas DataFeed Example
Note
pandas
and its dependencies have to be installed
Supporting Pandas Dataframes seems to be of business to lots of people, who rely on the already bachelor parsing lawmaking for different data sources (including CSV) and other functionalities offered by Pandas.
The of import declarations for the Datafeed.
Note
These are ONLY declarations. Don't re-create this lawmaking blindly. See the actual usage in the example below
class PandasData ( feed . DataBase ): ''' The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas DataFrame ''' params = ( # Possible values for datetime (must always be present) # None : datetime is the "index" in the Pandas Dataframe # -1 : autodetect position or case-wise equal proper noun # >= 0 : numeric index to the colum in the pandas dataframe # string : column proper name (as index) in the pandas dataframe ( 'datetime' , None ), # Possible values below: # None : column not nowadays # -one : autodetect position or case-wise equal proper noun # >= 0 : numeric index to the colum in the pandas dataframe # cord : cavalcade name (as index) in the pandas dataframe ( 'open' , - 1 ), ( 'high' , - 1 ), ( 'low' , - 1 ), ( 'close' , - 1 ), ( 'book' , - 1 ), ( 'openinterest' , - one ), )
The to a higher place excerpt from the PandasData
class shows the keys:
-
The
dataname
parameter to the class during instantiation holds the Pandas DataframeThis parameter is inherited from the base class
feed.DataBase
-
The new parameters take the names of the regular fields in the
DataSeries
and follow these conventions-
datetime
(default: None) -
None : datetime is the "alphabetize" in the Pandas Dataframe
-
-ane : autodetect position or case-wise equal name
-
= 0 : numeric alphabetize to the colum in the pandas dataframe
-
string : cavalcade name (as alphabetize) in the pandas dataframe
-
open
,loftier
,low
,high
,shut
,volume
,openinterest
(default: -1 for all of them) -
None : column non nowadays
-
-1 : autodetect position or example-wise equal name
-
= 0 : numeric index to the colum in the pandas dataframe
-
string : column name (as index) in the pandas dataframe
-
A small sample should be able to load the standar 2006 sample, having been parsed by Pandas
, rather than directly past backtrader
Running the sample to use the exiting "headers" in the CSV information:
$ ./panda-test.py -------------------------------------------------- Open High Low Close Volume OpenInterest Date 2006-01-02 3578.73 3605.95 3578.73 3604.33 0 0 2006-01-03 3604.08 3638.42 3601.84 3614.34 0 0 2006-01-04 3615.23 3652.46 3615.23 3652.46 0 0
The aforementioned but telling the script to skip the headers:
$ ./panda-test.py --noheaders -------------------------------------------------- i two 3 4 5 half-dozen 0 2006-01-02 3578.73 3605.95 3578.73 3604.33 0 0 2006-01-03 3604.08 3638.42 3601.84 3614.34 0 0 2006-01-04 3615.23 3652.46 3615.23 3652.46 0 0
The 2nd run is using tells pandas.read_csv
:
-
To skip the first input row (
skiprows
keyword statement set to 1) -
Non to wait for a headers row (
header
keyword argument set to None)
The backtrader
support for Pandas tries to automatically detect if column names have been used or else numeric indices and acts accordingly, trying to offer a best lucifer.
The following chart is the tribute to success. The Pandas Dataframe has been correctly loaded (in both cases)
The sample code for the test.
from __future__ import ( absolute_import , sectionalisation , print_function , unicode_literals ) import argparse import backtrader as bt import backtrader.feeds as btfeeds import pandas def runstrat (): args = parse_args () # Create a cerebro entity cerebro = bt . Cerebro ( stdstats = Imitation ) # Add a strategy cerebro . addstrategy ( bt . Strategy ) # Get a pandas dataframe datapath = ( '../../datas/2006-day-001.txt' ) # Simulate the header row isn't there if noheaders requested skiprows = 1 if args . noheaders else 0 header = None if args . noheaders else 0 dataframe = pandas . read_csv ( datapath , skiprows = skiprows , header = header , parse_dates = True , index_col = 0 ) if not args . noprint : print ( '--------------------------------------------------' ) print ( dataframe ) print ( '--------------------------------------------------' ) # Pass information technology to the backtrader datafeed and add it to the cerebro data = bt . feeds . PandasData ( dataname = dataframe ) cerebro . adddata ( data ) # Run over everything cerebro . run () # Plot the result cerebro . plot ( style = 'bar' ) def parse_args (): parser = argparse . ArgumentParser ( clarification = 'Pandas exam script' ) parser . add_argument ( '--noheaders' , action = 'store_true' , default = False , required = False , help = 'Exercise not use header rows' ) parser . add_argument ( '--noprint' , action = 'store_true' , default = False , assist = 'Print the dataframe' ) return parser . parse_args () if __name__ == '__main__' : runstrat ()
Source: https://www.backtrader.com/docu/pandas-datafeed/pandas-datafeed/
Postar um comentário for "How to Read Csv With Panda That Has No Headers"