简体   繁体   中英

Python Pandas data frame format Index issue

RangeIndex error:

Using two data source trying to fetch the data, quandl and x source (the data fetching is not a problem but using this for logic throws error)for quandl no issues but for x source this RangeIndex error comes.

I feel this is because of incorrect indexing problem.

What needs to be changed for proper alignment for x source in dataframe df1

df=quandl.get("{0}".format(Ticker),start_date="2014-01-01", end_date="2018-01-26")
print(df.head(30))

df1 = get_history(symbol="{0}".format(Ticker),
            start=dt.date(2015,1,1), 
            end=dt.date(2018,1,19))
df.reset_index(inplace=True)
print(df.head(30))

month_index =df.index.to_period('M')

The Error: month_index =df.index.to_period('M') AttributeError: 'RangeIndex' object has no attribute 'to_period' During handling of the above exception, another exception occurred:

Some analysis i did shows the below The Differrence of list out is as below where df index Date is oneline below First row (Which is working fine) if the df1 the index and open low close everything placed in same row which is throwing the error.

Example of the output is given below for quick view

The quandl output is df :(working one with no error)

 Date       Open     High      Low     Last    Close  Total Trade Quantity  

2017-12-14  1005.25  1015.00   999.30  1012.30  1013.10             3011786.0   
2017-12-15  1013.00  1026.40  1011.85  1019.00  1022.90             5192067.0   
2017-12-18  1011.90  1030.70  1005.80  1014.00  1016.60             2017724.0   
2017-12-19  1017.45  1017.95   999.55  1006.65  1003.95             3559449.0   
2017-12-20  1005.10  1018.60  1001.00  1016.35  1015.85             2093163.0   
2017-12-21  1014.80  1028.95   998.40  1021.50  1022.05             5529581.0 

The x source out put is df1: ( i see differrence here Date OHLC all in one row which is causing issue)

    Date Symbol Series  Prev Close     Open     High      Low     Last
728  2017-12-08   INFY     EQ      999.80  1001.00  1007.00   995.00   999.40   
729  2017-12-11   INFY     EQ     1001.85   994.95  1006.90   993.95  1005.40   
730  2017-12-12   INFY     EQ     1005.30  1002.00  1014.00  1002.00  1010.50   
731  2017-12-13   INFY     EQ     1010.50  1010.90  1019.00   997.65  1005.00   
732  2017-12-14   INFY     EQ     1003.75  1005.25  1015.00   999.30  1012.30   
733  2017-12-15   INFY     EQ     1013.10  1013.00  1026.40  1011.85  1019.00

How to correct this ? to make the df1 dataframe as same as df interms of index to make is look same.

Edit to the original post and update: from the df=quandl.get if i print the month_index the following is the result ,i suspect the df1=get_history is not returning same hence the error.Any other alternative way to get it through?

PeriodIndex(['2014-01', '2014-01', '2014-01', '2014-01', '2014-01', '2014-01',
             '2014-01', '2014-01', '2014-01', '2014-01',
             ...
             '2018-01', '2018-01', '2018-01', '2018-01', '2018-01', '2018-01',
             '2018-01', '2018-01', '2018-01', '2018-01'],
            dtype='period[M]', name='Date', length=1006, freq='M')

Comment out

df.reset_index(inplace=True)

This is happening as the index is of type string . Convert the index to datetime type and then apply operations on it.

df.index = pd.to_datetime(df.index)
month_index = df.index.to_period('M')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM