2017-04-02 109 views
1

我有一个问题,使用当前版本的熊猫向后填充numpy日期向量。相同的代码适用于早期版本。下面演示了我的问题:熊猫DataFrame错误“元组索引超出范围”

旧版本(0.7.3)的作品

C:\WINDOWS\system32>pip show pandas 
Name: pandas 
Version: 0.7.3 
Summary: Powerful data structures for data analysis and statistics 
Home-page: http://pandas.pydata.org 
Author: The PyData Development Team 
Author-email: [email protected] 
License: BSD 
Location: c:\program files\python\python27\lib\site-packages 
Requires: python-dateutil, numpy 

C:\WINDOWS\system32>python 
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 
>>> d=np.array([None, None, None, None, dt.now(), None]) 
>>> b = DataFrame(d) 
>>> b.fillna(method='backfill') 
          0 
0 2017-04-02 12:21:18.175000 
1 2017-04-02 12:21:18.175000 
2 2017-04-02 12:21:18.175000 
3 2017-04-02 12:21:18.175000 
4 2017-04-02 12:21:18.175000 
5      None 
>>> 

目前vesion(0.19.2)不工作:

C:\WINDOWS\system32>pip show pandas 
Name: pandas 
Version: 0.19.2 
Summary: Powerful data structures for data analysis, time series,and statistics 
Home-page: http://pandas.pydata.org 
Author: The PyData Development Team 
Author-email: [email protected] 
License: BSD 
Location: c:\program files\python\python27\lib\site-packages 
Requires: pytz, python-dateutil, numpy 


C:\WINDOWS\system32>python 
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from datetime import datetime as dt 
>>> import numpy as np 
>>> from pandas import DataFrame 
>>> d=np.array([None, None, None, None, dt.now(), None]) 
>>> b = DataFrame(d) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Program Files\Python\Python27\lib\site-packages\pandas\core\frame.py", line 297, in __init__ 
    copy=copy) 
    File "C:\Program Files\Python\Python27\lib\site-packages\pandas\core\frame.py", line 474, in _init_ndarray 
    return create_block_manager_from_blocks([values], [columns, index]) 
    File "C:\Program Files\Python\Python27\lib\site-packages\pandas\core\internals.py", line 4256, in create_block_manager_from_blocks 
    construction_error(tot_items, blocks[0].shape[1:], axes, e) 
    File "C:\Program Files\Python\Python27\lib\site-packages\pandas\core\internals.py", line 4230, in construction_error 
    if block_shape[0] == 0: 
IndexError: tuple index out of range 
>>> 

我在做什么像我认为的那样,错误还是它是熊猫中的一个错误?如果它有一个错误,我该如何报告?

编辑:这是作为提起错误报告与大熊猫,将被固定在一个次RELASE(0.19.3)

回答

2

DataFrame(d)失败,我不知道为什么,但Series(d)作品,所以你可以这样做:

pd.DataFrame({0:d}) 

也就是说,明确地告诉熊猫是d是一个系列叫0,这是它隐含在做古代的0.7版本。

如果你想报告错误,你可以简单地说,这个工程:

pd.DataFrame([None, None, datetime.datetime.now()]) 

但这种失败:

pd.DataFrame([None, None, None, datetime.datetime.now()]) 
+0

感谢您的回答。你能告诉我在哪里报告吗?你还可以解释你的测试中发生了什么。为什么一个工作,另一个不工作? –

+0

@RichardB:熊猫问题追踪器在这里:https://github.com/pandas-dev/pandas/issues –

0

尝试指定(或CAST)的dtype明确:

In [18]: d=np.array([None, None, None, None, pd.datetime.now(), None]) 

In [19]: b = DataFrame(d.astype('datetime64[ms]')) 

In [20]: b 
Out[20]: 
         0 
0      NaT 
1      NaT 
2      NaT 
3      NaT 
4 2017-04-02 20:34:20.381 
5      NaT 

In [21]: b.bfill() 
Out[21]: 
         0 
0 2017-04-02 20:34:20.381 
1 2017-04-02 20:34:20.381 
2 2017-04-02 20:34:20.381 
3 2017-04-02 20:34:20.381 
4 2017-04-02 20:34:20.381 
5      NaT