我有一个字典包含两个numpy数组,一个在日期时间.日期时间另一个是一个隐藏的数据数组。我试着用datetime数组作为DatetimeIndex将其放入Pandas中,但我失败了。在In [62]: dict1
Out[62]:
{'filltimes': array([datetime.datetime(2013, 8, 12, 12, 0, 1),
datetime.datetime(2013, 8, 12, 12, 30, 1),
datetime.datetime(2013, 8, 12, 13, 0, 1), ...,
datetime.datetime(2013, 9, 14, 19, 0, 1),
datetime.datetime(2013, 9, 14, 19, 30, 1),
datetime.datetime(2013, 9, 14, 20, 0, 1)], dtype=object),
'fillvals': masked_array(data = [5.553 2.604 2.604 ..., 16.896 17.271 18.022],
mask = [False False False ..., False False False],
fill_value = 1e+20)
}
In [63]: type(dict1)
Out[63]: dict
In [64]: type(dict1['filltimes'])
Out[64]: numpy.ndarray
In [65]: type(dict1['filltimes'][0])
Out[65]: datetime.datetime
In [66]: pd1=pd.DataFrame.from_dict(dict1)
In [67]: type(pd1)
Out[67]: pandas.core.frame.DataFrame
In [68]: type(pd1['filltimes'])
Out[68]: pandas.core.series.Series
In [69]: type(pd1['filltimes'][0])
Out[69]: pandas.tslib.Timestamp
In [70]: pd1.resample('D', how = 'mean')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 pd1.resample('D', how = 'mean')
/Users/andrew/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base)
2777 fill_method=fill_method, convention=convention,
2778 limit=limit, base=base)
-> 2779 return sampler.resample(self).__finalize__(self)
2780
2781 def first(self, offset):
/Users/andrew/anaconda/lib/python2.7/site-packages/pandas/tseries/resample.pyc in resample(self, obj)
99 return obj
100 else: # pragma: no cover
--> 101 raise TypeError('Only valid with DatetimeIndex or PeriodIndex')
102
103 rs_axis = rs._get_axis(self.axis)
TypeError: Only valid with DatetimeIndex or PeriodIndex
In [71]: pd1.reindex(pd.DatetimeIndex(pd.to_datetime(pd1['filltimes'])))
Out[71]:
filltimes fillvals
2013-08-12 12:00:01 NaT NaN
2013-08-12 12:30:01 NaT NaN
2013-08-12 13:00:01 NaT NaN
2013-08-12 13:30:01 NaT NaN
2013-08-12 14:00:01 NaT NaN
2013-08-12 14:30:01 NaT NaN
2013-08-12 15:00:01 NaT NaN
2013-08-12 15:30:01 NaT NaN
2013-08-12 16:00:01 NaT NaN
2013-08-12 16:30:01 NaT NaN
2013-08-12 17:00:01 NaT NaN
2013-08-12 17:30:01 NaT NaN
2013-08-12 18:00:01 NaT NaN
2013-08-12 18:30:01 NaT NaN
2013-08-12 19:00:01 NaT NaN
2013-08-12 19:30:01 NaT NaN
2013-08-12 20:00:01 NaT NaN
2013-08-12 20:30:01 NaT NaN
2013-08-12 21:00:01 NaT NaN
2013-08-12 21:30:01 NaT NaN
2013-08-12 22:00:01 NaT NaN
2013-08-12 22:30:01 NaT NaN
2013-08-12 23:00:01 NaT NaN
2013-08-12 23:30:01 NaT NaN
2013-08-13 00:00:01 NaT NaN
2013-08-13 00:30:01 NaT NaN
2013-08-13 01:00:01 NaT NaN
2013-08-13 01:30:01 NaT NaN
2013-08-13 02:00:01 NaT NaN
2013-08-13 02:30:01 NaT NaN
2013-08-13 03:00:01 NaT NaN
2013-08-13 03:30:01 NaT NaN
2013-08-13 04:00:01 NaT NaN
2013-08-13 04:30:01 NaT NaN
2013-08-13 05:00:01 NaT NaN
2013-08-13 05:30:01 NaT NaN
2013-08-13 06:00:01 NaT NaN
2013-08-13 06:30:01 NaT NaN
2013-08-13 07:00:01 NaT NaN
2013-08-13 07:30:01 NaT NaN
2013-08-13 08:00:01 NaT NaN
2013-08-13 08:30:01 NaT NaN
2013-08-13 09:00:01 NaT NaN
2013-08-13 09:30:01 NaT NaN
2013-08-13 10:00:01 NaT NaN
2013-08-13 10:30:01 NaT NaN
2013-08-13 11:00:01 NaT NaN
2013-08-13 11:30:01 NaT NaN
2013-08-13 12:00:01 NaT NaN
2013-08-13 12:30:01 NaT NaN
2013-08-13 13:00:01 NaT NaN
2013-08-13 13:30:01 NaT NaN
2013-08-13 14:00:01 NaT NaN
2013-08-13 14:30:01 NaT NaN
2013-08-13 15:00:01 NaT NaN
2013-08-13 15:30:01 NaT NaN
2013-08-13 16:00:01 NaT NaN
2013-08-13 16:30:01 NaT NaN
2013-08-13 17:00:01 NaT NaN
2013-08-13 17:30:01 NaT NaN
... ...
[1601 rows x 2 columns]
In [72]:
正如您所看到的,重新编制索引并没有产生我所期望的或者至少是我希望的结果。不仅丢失了所有数据,而且重新编制索引的操作也没有得到保留:
^{pr2}$
如果你能给我一两个指点,我会非常感激的。和熊猫在一起的整个约会/时间都快疯了。在