简体   繁体   中英

why AssertionError having numpy.ndarray

My data frame is look like -

             can    sbi      bjf    srt      acc    amb     tata     jsw    hdfcl   sbil
Date                                        
2018-02-07  248.65  259.10  2298.40 1284.65 1313.75 200.30  560.95  313.25  457.65  667.90
2018-03-07  244.70  257.75  2321.50 1298.45 1336.80 200.85  566.50  311.80  459.65  669.90
2018-04-07  244.75  257.50  2331.95 1144.85 1352.65 201.15  568.90  312.00  459.60  664.60
2018-05-07  243.75  256.60  2329.45 1151.90 1388.95 207.30  553.50  310.40  462.05  663.15
2018-06-07  247.80  257.45  2341.70 1110.55 1373.45 207.35  554.15  309.30  468.90  667.40
2018-09-07  249.55  261.35  2346.60 1139.95 1375.90 206.40  556.85  312.95  474.30  664.90
2018-10-07  250.75  263.50  2366.30 1206.35 1371.50 204.10  568.60  316.75  476.70  660.00
2018-11-07  244.15  258.90  2355.20 1205.80 1360.70 202.45  555.85  312.65  469.05  660.95
2018-12-07  241.40  262.75  2414.30 1214.00 1349.15 202.05  556.30  316.80  478.00  664.35
2018-07-13  231.95  257.60  2460.35 1253.55 1335.95 197.15  558.15  316.75  471.70  660.45

I want to do some EDA based on this data frame. My code is given below -

import numpy as np
import pandas as pd
# import pandas_datareader.data as web
import matplotlib.pyplot as plt
import statsmodels.regression.linear_model as rg
import arch.unitroot as at
import mxnet as mx

data = pd.read_csv('/Users/XXXX/Downloads/Pairs-Trading-Analysis-Data.csv', index_col='Date', parse_dates=True)
data.head(10)

int1 = data.loc[:, ['bjf', 'srt']]
tint1 = int1[:'2018-12-31']
tint1.columns = ['taus', 'tcan']
fint1 = int1['2019-01-02':]
fint1.columns = ['faus', 'fcan']

Now I got this error -

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-30-b374f43b65a9> in <module>
      1 # 2.2. Training and Testing Ranges Delimiting
----> 2 tint1 = int1[:'2018-12-31']
      3 tint1.columns = ['taus', 'tcan']
      4 fint1 = int1['2019-01-02':]
      5 fint1.columns = ['faus', 'fcan']

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2880             # either we have a slice or we have a string that can be converted
   2881             #  to a slice for partial-string date indexing
-> 2882             return self._slice(indexer, axis=0)
   2883 
   2884         # Do we have a (boolean) DataFrame?

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in _slice(self, slobj, axis)
   3546         Slicing with this method is *always* positional.
   3547         """
-> 3548         assert isinstance(slobj, slice), type(slobj)
   3549         axis = self._get_block_manager_axis(axis)
   3550         result = self._constructor(self._mgr.get_slice(slobj, axis=axis))

AssertionError: <class 'numpy.ndarray'>

But the main thing is when I am using windows I am not getting any error but this error comes when I am using MAC os. Any specific reason for that? how to rectify this error?

I came across the similar problem, my solution was:

  1. make sure the index is type of 'DatetimeIndex', I do this:
df.index = pd.to_datetime(df.index)
  1. sorted the 'index' first:
df = df.sort_index()

And then operations (below) should work:

df_sub = df['2018']  # select the whole year of 2018

df_sub2 = df['2018-02']  # select the given month

df_sub3 = df['2018-01':'2018-03']  # select between dates

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