简体   繁体   English

在for循环内创建数据帧的字典引发AssertionError [Pandas for Python]

[英]creating dictionary of dataframes within for-loop throws AssertionError [Pandas for Python]

I am trying to build a set of dataframes from a folder full of csv files. 我正在尝试从充满csv文件的文件夹构建一组数据帧。 I first create a dictionary of dataframes using the following for loop 我首先使用以下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

I then pull them into a panel using 然后我将它们拉入面板

mypanel = pd.Panel(dframes) # create panel

From that panel, I pull the oldest and the latest date, round the oldest date to the nearest 20 minutes, create a DateTimeIndex for that timespan in 20 minute intervals 在该面板中,我拉出最早的日期和最新的日期,将最早的日期四舍五入到最接近的20分钟,并以20分钟的间隔为该时间跨度创建一个DateTimeIndex

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

Then, I am trying to reindex my irregularly timestamped data to the 20 minute interval series I created before, idx 然后,我试图将我不定期加时间戳的数据重新索引到我之前创建的20分钟间隔系列idx

df2 = df.reindex(idx)

Problem is, I am getting the following error 问题是,我收到以下错误

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

In dubugging this problem, I have discovered that the following works just fine. 在调试这个问题时,我发现以下工作正常。 I have tried to recreate every difference I can think of, short of the dataframes being created inside of the loop 我试图重新创建我能想到的所有差异,但缺少在循环内部创建的数据帧

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)

Here is the output of the previous block of code (I am losing some data to rounding, to be solved later) 这是上一个代码块的输出(我正在舍入一些数据,稍后再解决)

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

So, I know it should be working. 因此,我知道它应该正常工作。 I can't think of anything else that would be causing this Assertion Error. 我想不出任何其他会导致此断言错误的原因。

Anything I could try? 有什么我可以尝试的吗?

You should be using resample rather than reindexing with a date_range: 您应该使用重新采样,而不是使用date_range重新编制索引:

idx = pd.date_range(t3, last_date, freq="20min")
df2 = df.reindex(idx)

Might rather be: 可能是:

df.resample('20min', 'last')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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