在forloop中创建数据帧字典会引发AssertionError[Pandas for Python]

2024-06-29 00:52:38 发布

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

我试图从一个充满csv文件的文件夹中构建一组数据帧。我首先使用下面的for循环创建一个数据帧字典

mydir = os.getcwd()
pdatahistorypath = os.path.join(mydir, pdatahistoryfolder)
currentcsvfilenames = os.listdir(pdatahistorypath)
dframes = {}
for filey in currentcsvfilenames:
    thispath = os.path.join(pdatahistorypath, filey)
    sitedata = pd.read_csv(thispath, header=4)
    sitedata = sitedata.drop('Unnamed: 16', axis=1) # drops waste column
    sitedata['Date'] = pd.to_datetime(sitedata['Date'])
    sitedata.index = sitedata['Date']               # reasign the index to the date column
    dframes[filey[:-4]] = sitedata

然后我用

^{pr2}$

从该面板中,我提取最早和最晚的日期,将最早的日期四舍五入到最接近的20分钟,以20分钟的间隔为该时间跨度创建一个日期时间索引

first_date = mypanel.major_axis[0]
last_date = mypanel.major_axis[-1]             # the very last date in series
multiplier = (1e9)*60*20                       # round (floor) to 20 minute interval
t3 = first_date.value - first_date.value % multiplier
idx = pd.date_range(t3, last_date, freq="20min")
df = dframes['Naka-1.csv']

然后,我尝试将不规则的时间戳数据重新索引到我之前创建的20分钟间隔序列,idx

df2 = df.reindex(idx)

问题是,我得到了以下错误

Traceback (most recent call last):
  File "C:/Users/ble1usb/Dropbox/Git/ers-dataanalyzzer/pandasdfmaker.py", line 50, in <module>
    df2 = df.reindex(idx)#, method=None)#, method='pad', limit=None) # reindex to the datetimeindex built from first/last dates
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2625, in reindex
    fill_value, limit, takeable)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2707, in _reindex_index
    copy, fill_value)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2723, in _reindex_with_indexers
    fill_value=fill_value)
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 1985, in reindex_indexer
    return BlockManager(new_blocks, new_axes)
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 1001, in __init__
    self._verify_integrity()
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 1236, in _verify_integrity
    raise AssertionError("Block ref_items must be BlockManager "
AssertionError: Block ref_items must be BlockManager items

在质疑这个问题的过程中,我发现下面的方法很管用。我试着重新创建我能想到的每一个差异,除了在循环内创建的数据帧

dframes = {}
dfpath = 'C:\Users\\ble1usb\Dropbox\Git\ers-dataanalyzzer\datahistoryPandas\Naka-1.csv'
sitedata = pd.read_csv(dfpath, header=4)
sitedata = sitedata.drop('Unnamed: 16', axis=1) # drops waste column
sitedata['Date'] = pd.to_datetime(sitedata['Date'])
sitedata.index = sitedata['Date']  # reasign the index to the date column
dframes['Naka-1'] = sitedata
dframes['myOtherSite'] = sitedata[sitedata['Out ppm'] > 3]
mypanel = pd.Panel(dframes)
first_date = mypanel.major_axis[0]
last_date = mypanel.major_axis[-1]             # the very last date in series
multiplier = (1e9)*60*20                       # round (floor) to 20 minute interval
t3 = first_date.value - first_date.value % multiplier
idx = pd.date_range(t3, last_date, freq="20min")
df = dframes['Naka-1.csv']
df2 = df.reindex(idx)

这是前一段代码的输出(我丢失了一些数据,需要舍入,稍后再解决)

>> print df2.tail(15)

                                   Date   Status Alarms Present    RPM  Hours  Oil Pres. Out ppm Ratio   In  Out  Inlet psi  Bag psi  Disch. psi  Hi Pres  Coolant Temp    Comm
2013-12-10 16:40:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 17:00:00 2013-12-10 17:00:00  Running           none  2,820  9,384         53       0     0  469  473       5.56     0.72       268.1        0             1  Normal
2013-12-10 17:20:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 17:40:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 18:00:00 2013-12-10 18:00:00  Running           none  2,820  9,385         54       0     0  462  470      12.28     0.82       259.1        0             1  Normal
2013-12-10 18:20:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 18:40:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 19:00:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 19:20:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 19:40:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 20:00:00 2013-12-10 20:00:00  Running           none  2,880  9,387         55       0     0  450  456      10.91     0.73       249.9        0             1  Normal
2013-12-10 20:20:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 20:40:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN
2013-12-10 21:00:00 2013-12-10 21:00:00  Running           none  2,820  9,388         54       0     0  440  449       8.16     0.62       243.1        0             1  Normal
2013-12-10 21:20:00                 NaT      NaN            NaN    NaN    NaN        NaN     NaN   NaN  NaN  NaN        NaN      NaN         NaN      NaN           NaN     NaN

所以,我知道它应该有用。我认为这不会造成任何错误。在

有什么我可以试试的吗?


Tags: thetoindatevaluenannatfile