我是python的新手,我正在尝试通过此构建时间序列。我试图将这个csv数据转换成时间序列,但是通过互联网和堆栈研究,结果'应该有
<class 'pandas.tseries.index.DatetimeIndex'>,
但我的输出没有转换时间序列。为什么不转换?我该如何转换它?感谢您的帮助。
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
data = pd.read_csv('somedata.csv')
print data.head()
#selecting specific columns by column name
df1 = data[['a','b']]
#converting the data to time series
dates = pd.date_range('2015-01-01', '2015-12-31', freq='H')
dates #preview
结果:
DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
...
'2015-12-31 23:00:00', '2015-12-31 00:00:00'],
dtype='datetime64[ns]', length=2161, freq='H')
上面工作正常,但我收到以下错误: df1 =系列(df1 [:,2],索引=日期)
输出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Series' is not defined
尝试pd.Series后......
df1 = pd.Series(df1[:,2], index=dates)
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/someid/miniconda2/lib/python2.7/site- packages/pandas/core/frame.py", line 1992, in __getitem__
return self._getitem_column(key)
File "/home/someid/miniconda2/lib/python2.7/site- packages/pandas/core/frame.py", line 1999, in _getitem_column
return self._get_item_cache(key)
File "/home/someid/miniconda2/lib/python2.7/site- packages/pandas/core/generic.py", line 1343, in _get_item_cache
res = cache.get(item)
TypeError: unhashable type
答案 0 :(得分:1)
你需要拥有pd.Series
。但是,你也做错了什么。我假设您要获取所有行,df1的第二列并返回带有日期索引的pd.Series
。
df1 = pd.Series(df1.iloc[:, 1], index=dates)
df1.iloc
用于按行/列发布返回df1
的切片
[:, 1]
获取所有行,第2列
此外,df1.iloc [:,1]返回pd.Series
并可以传递给pd.Series
构造函数。