2013-04-04 40 views
0

制成列表的数据帧。创建一个时间戳,允许部分索引在我遇到的时间戳选择在大熊猫问题大熊猫

对于我看到的是一些其他人有的问题(Selecting a subset of a Pandas DataFrame indexed by DatetimeIndex with a list of TimeStamps),但Pandas的开发人员不幸拒绝接受它作为错误(https://github.com/pydata/pandas/issues/2437)。

在任何情况下,我不能跟随周边提出了SO后我引用上面,因为我的数据不会在CSV文件来工作,但在一些名单(其实我是从互联网通过JSON并将其转换为列表)。

我得到的数据是这样的:

the_dataTransactions 
[{u'date': u'1365100630', u'tid': 240264, u'price': u'132.58', u'amount': u'1.28309000'}, {u'date': u'1365100630', u'tid': 240263, u'price': u'132.58', u'amount': u'1.20294000'}, {u'date': u'1365100629', u'tid': 240262, u'price': u'132.58', u'amount': u'0.90893940'}] 

我将其转换为:

transactionsDate 
[datetime.datetime(2013, 4, 4, 19, 37, 10), datetime.datetime(2013, 4, 4, 19, 37, 10), datetime.datetime(2013, 4, 4, 19, 37, 9)] 

而且我也试过,但在结果的错误,当我尝试选择一个数据范围是一样的:

transactionsDate 
[<Timestamp: 2013-04-04 19:37:10>, <Timestamp: 2013-04-04 19:37:10>, <Timestamp: 2013-04-04 19:37:09>] 

而且这个tid,价格和金额也被添加到数据框中,例如:

>>> transactionsDF.info() 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 73 entries, 2013-04-04 19:37:10 to 2013-04-04 19:22:49 
Data columns: 
tid  73 non-null values 
price  73 non-null values 
amount 73 non-null values 
dtypes: float64(2), int64(1) 

>>> transactionsDF.head() 
         tid price amount 
2013-04-04 19:37:10 240264 132.58 1.283090 
2013-04-04 19:37:10 240264 132.58 1.283090 
2013-04-04 19:37:10 240263 132.58 1.202940 
2013-04-04 19:37:09 240262 132.58 0.908939 
2013-04-04 19:37:09 240261 132.59 0.213051 

但是,当我尝试使用正常符号选择数据范围,我得到了在其他职位报道了同样的错误:

>>> transactionsDF['2013-04-03 18:00:00':'2013-04-04 19:00:00'] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 1951, in __getitem__ 
    indexer = self.ix._convert_to_indexer(key, axis=0) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/indexing.py", line 478, in _convert_to_indexer 
    i, j = labels.slice_locs(start, stop) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tseries/index.py", line 1153, in slice_locs 
    start_loc = self._get_string_slice(start).start 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tseries/index.py", line 1143, in _get_string_slice 
    loc = self._partial_date_slice(reso, parsed) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tseries/index.py", line 1041, in _partial_date_slice 
    raise TimeSeriesError('Partial indexing only valid for ordered ' 
pandas.tseries.index.TimeSeriesError: Partial indexing only valid for ordered time series. 

我的数据似乎是在一个有序时间序列。你可以考虑在这个特殊情况下解决这个熊猫故障吗?

UPDATE(解决): 我发现了一个方式,就是这么简单,我不能完全肯定它会给出正确的答案每次,但至少对于一个小的数据帧它的工作。 的代码,它只是:

transactionsDF = transactionsDF.sort_index() 

而经过这似乎是工作的罚款,让我选择一个数据范围不像我以前与其他数据: transactionsDF ['2013年4月4日19:30 “:” 2013年4月4日19:35' ]

也许有人更了解可以验证或unvalidate此解决办法。

+1

这是固定的0.11 DEV(试试看)。是一个问题! https://开头github上。COM/pydata /熊猫/拉/ 3136 – Jeff 2013-04-04 19:35:38

+0

FYI看你的数据不会出现头部被责令 – Jeff 2013-04-04 20:05:51

+0

@Jeff它是有序的,但它从最近下令最古老的。令我惊讶的是,相当意外的是,我用的是引起了我的眼睛熊猫功能:)sort_index(似乎这是后就像我在熊猫写其他例程工作,让我的选择。 – jbssm 2013-04-04 21:00:07

回答

1

我觉得没有真正优雅的解决方案。熊猫不喜欢重复的索引。 (至少是我的旧版本。)您可以创建带有重复索引的DataFrame,但无法轻松访问其内容。

因此,你应该把日期,一个单独的列。然后你在日期比较操作,和花式索引访问有趣行:

In [1]: import pandas as pd 

In [5]: import datetime 

In [15]: f1 = pd.DataFrame([{u'date': u'1365100630', u'tid': 240264, u'price': u'132.58', u'amount': u'1.28309000'}, {u'date': u'1365100630', u'tid': 240263, u'price': u'132.58', u'amount': u'1.20294000'}, {u'date': u'1365100629', u'tid': 240262, u'price': u'132.58', u'amount': u'0.90893940'}]) 

In [16]: f1["dates"] = [datetime.datetime(2013, 4, 4, 19, 37, 10), datetime.datetime(2013, 4, 4, 19, 37, 10), datetime.datetime(2013, 4, 4, 19, 37, 9)] 

In [17]: f1 
Out[17]: 
     amount  date price  tid    dates 
0 1.28309000 1365100630 132.58 240264 2013-04-04 19:37:10 
1 1.20294000 1365100630 132.58 240263 2013-04-04 19:37:10 
2 0.90893940 1365100629 132.58 240262 2013-04-04 19:37:09 

In [25]: matching = (f1["dates"] >= datetime.datetime(2013, 4, 4, 19, 37, 10)) & (f1["dates"] < datetime.datetime(2013, 4, 4, 20, 00, 00)) 

In [26]: f1.ix[matching] 
Out[26]: 
     amount  date price  tid    dates 
0 1.28309000 1365100630 132.58 240264 2013-04-04 19:37:10 
1 1.20294000 1365100630 132.58 240263 2013-04-04 19:37:10 

您还可以使用f1[matching]访问有趣行,但我觉得不太清楚,因为f1["foo"]用于访问列。

+0

谢谢你的帮助。我发现了一个非常简单的方法来做到这一点...尽管我不确定它是否会一直给出正确的值。我只是使用transactionsDF = transactionsDF.sort_index(),现在它的工作方式与其他示例中的一样。就像我说的,我不确定这是否每次都给出正确的答案,但到目前为止,我选择时间范围时似乎给了我正确的数据。 – jbssm 2013-04-04 21:02:22