python-2.7 - 同一个系列中的时间序列日期时间有两种格式,如何将它们更改为一种格式?

标签 python-2.7 pandas datetime timestamp time-series

我想将时间序列分为两组:训练和测试。 这是我的代码:

train = data.iloc[:1100]
test = data.iloc[1101:]

时间序列如下: enter image description here

这是火车系列:索引中没有时间,只有日期。

enter image description here

最后测试一下: enter image description here

如何将索引更改为相同的形式?

最佳答案

考虑简化的系列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/

相关文章:

javascript - 计算两个日期时间之间的时间

python - 如何在 Pystache 中使用 '.' 渲染键?

python - 如何记录 ConfigParser 的内容?

python - 在 Python 中获取硬盘大小

Java 8 日期时间格式化程序行为不端

c++ - boost::locale::date_time 的陷阱

python - 按创建/修改日期移动文件然后使用 Python 移动

python - 将函数应用于带有两个参数的 pandas 数据框中的列

python - 需要找到黑点

python - 根据其中一列中的值类型交换列中的值