我想将时间序列分为两组:训练和测试。 这是我的代码:
train = data.iloc[:1100]
test = data.iloc[1101:]
这是火车系列:索引中没有时间,只有日期。
如何将索引更改为相同的形式?
最佳答案
考虑简化的系列s
s = pd.Series(1, pd.date_range('2010-08-16', periods=5, freq='12H'))
s
2010-08-16 00:00:00 1
2010-08-16 12:00:00 1
2010-08-17 00:00:00 1
2010-08-17 12:00:00 1
2010-08-18 00:00:00 1
Freq: 12H, dtype: int64
但是当我子集 s
只留下不需要时间元素的 Timestamp
时,pandas
给了我不显示 a 的“好处”无缘无故的一堆零。
s.iloc[::2]
2010-08-16 1
2010-08-17 1
2010-08-18 1
Freq: 24H, dtype: int64
但请放心,这些值是相同的:
s.iloc[::2].index[0] == s.index[0]
True
并且具有相同的数据类型和精度
print(s.iloc[::2].index.values.dtype)
dtype('<M8[ns]')
还有
print(s.index.values.dtype)
dtype('<M8[ns]')
关于python-2.7 - 同一个系列中的时间序列日期时间有两种格式,如何将它们更改为一种格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45751891/