为什么断言者拥有numpy.ndarray

2024-06-16 08:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据框看起来像-

             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

我想基于这个数据框架做一些EDA。我的代码如下:

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']

现在我犯了这个错误-

---------------------------------------------------------------------------
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'>

但最重要的是,当我使用windows时,我没有收到任何错误,但这个错误是在我使用MAC os时出现的。有什么具体原因吗? 如何纠正这个错误


Tags: columnsinimportselfpandasdataas错误
1条回答
网友
1楼 · 发布于 2024-06-16 08:49:19

我遇到了类似的问题,我的解决方案是:

  1. 确保索引的类型为“DatetimeIndex”,我将执行以下操作:
df.index = pd.to_datetime(df.index)
  1. 首先对“索引”进行排序:
df = df.sort_index()

然后,操作(如下所示)应起作用:

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

相关问题 更多 >