如何将 pandas DataFrame 转换为 TimeSeries?

How to convert a pandas DataFrame into a TimeSeries?(如何将 pandas DataFrame 转换为 TimeSeries?)
本文介绍了如何将 pandas DataFrame 转换为 TimeSeries?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种在不拆分索引和值列的情况下将 DataFrame 转换为 TimeSeries 的方法.有任何想法吗?谢谢.

I am looking for a way to convert a DataFrame to a TimeSeries without splitting the index and value columns. Any ideas? Thanks.

In [20]: import pandas as pd

In [21]: import numpy as np

In [22]: dates = pd.date_range('20130101',periods=6)

In [23]: df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))

In [24]: df
Out[24]:
                   A         B         C         D
2013-01-01 -0.119230  1.892838  0.843414 -0.482739
2013-01-02  1.204884 -0.942299 -0.521808  0.446309
2013-01-03  1.899832  0.460871 -1.491727 -0.647614
2013-01-04  1.126043  0.818145  0.159674 -1.490958
2013-01-05  0.113360  0.190421 -0.618656  0.976943
2013-01-06 -0.537863 -0.078802  0.197864 -1.414924

In [25]: pd.Series(df)
Out[25]:
0    A
1    B
2    C
3    D
dtype: object

推荐答案

我知道这对游戏来说已经晚了,但有几点.

I know this is late to the game here but a few points.

DataFrame 是否被认为是TimeSeries 是索引的类型.在你的情况下,你的索引已经是一个 TimeSeries,所以你很高兴.有关您可以使用 pd.timeseries 索引进行的所有酷切片的更多信息,请查看 http://pandas.pydata.org/pandas-docs/stable/timeseries.html#datetime-indexing

Whether or not a DataFrame is considered a TimeSeries is the type of index. In your case, your index is already a TimeSeries, so you are good to go. For more information on all the cool slicing you can do with a the pd.timeseries index, take a look at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#datetime-indexing

现在,其他人可能会到达这里,因为他们有一个列 'DateTime' 想要创建索引,在这种情况下答案很简单

Now, others might arrive here because they have a column 'DateTime' that they want to make an index, in which case the answer is simple

ts = df.set_index('DateTime')

这篇关于如何将 pandas DataFrame 转换为 TimeSeries?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)